Trees Tree Consists of a set of nodes

  • Slides: 24
Download presentation
Trees

Trees

Tree • Consists of a set of nodes and directed edges connecting them •

Tree • Consists of a set of nodes and directed edges connecting them • Rooted trees – One node is distinguished as root – Every node c, except root, has exactly one incoming edge from another node p. p is c’s parent. – There exists a unique path from root to any node

Tree Properties and Definitions • A tree with N nodes has N-1 edges •

Tree Properties and Definitions • A tree with N nodes has N-1 edges • Siblings – Nodes with same parent • Ancestor & descendant – If there is a path from u to v, v is the descendant of u & u is the ancestor of v – If u ≠ v, v is proper descendant of u & u is proper ancestor of v

Definitions (Contd. ) • Depth of a node – Length of path from root

Definitions (Contd. ) • Depth of a node – Length of path from root to node – Depth of root is 0 – Depth of any node is one more than its parent’s depth • Height of a node – Length of path from node to deepest descendant leaf • Size of a node – Number of descendants of the node (including itself) • Height of a tree – Height of the root of the tree

Example

Example

Recursive Definition • A tree is – Either empty – Or it consists of

Recursive Definition • A tree is – Either empty – Or it consists of root and zero or more of subtrees • Roots of subtrees connected by an edge to root of tree

Illustration

Illustration

Implementing Trees • Straightforward method – Every node in the tree has link to

Implementing Trees • Straightforward method – Every node in the tree has link to each of its children – Number of children can vary greatly – Child count may not be known before hand – Leads to space wastage • First Child – Next Sibling method – Children are stored as linked lists – Node stores link to leftmost child and right sibling

Illustration

Illustration

Binary Tree • A tree in which each node has at most two children

Binary Tree • A tree in which each node has at most two children – Left child and right child • Recursive definition – A binary tree is either empty, or it consists of a root, a left tree and a right tree • Applications – Expression trees in compilers – Huffman coding tree – Binary search trees

Applications

Applications

Binary Tree Implementation • Binary node – Stores an individual node in a binary

Binary Tree Implementation • Binary node – Stores an individual node in a binary tree – Consists of data (object to store), references to right child node and left child node (both binary nodes) • Binary tree – Stores the root node of the tree

Binary. Node and Binary. Tree public class Binary. Node{ private Object element; private Binary.

Binary. Node and Binary. Tree public class Binary. Node{ private Object element; private Binary. Node left; private Binary. Node right; } public class Binary. Tree{ private Binary. Node root; }

Methods in Binary. Node • Two constructors – First one takes in element, right

Methods in Binary. Node • Two constructors – First one takes in element, right node, and left node – Second one does not take in any parameters (all are set to null) • • Setters for element, left, and right Getters for element, left, and right Computing size and height of nodes Tree traversal methods (Preorder, Inorder & Postorder)

Computing Size • Recursive method that terminates at leaf nodes public int size(){ if(left

Computing Size • Recursive method that terminates at leaf nodes public int size(){ if(left == null && right == null) return(1); if(left == null) return(right. size() + 1); if(right == null) return(left. size() + 1); return(left. size() + right. size() + 1); }

Computing Height • Recursive method terminating in leaves public int height(){ if(left == null

Computing Height • Recursive method terminating in leaves public int height(){ if(left == null && right == null) return(0); if(left == null) return(right. height() + 1); if(right == null) return(left. height() + 1); if(right. height() => left. height()) return(right. height() + 1); return(left. height() + 1);

Tree Traversals • Accessing (or processing) individual nodes of a tree • Usually implemented

Tree Traversals • Accessing (or processing) individual nodes of a tree • Usually implemented recursively • Three kinds of tree traversal – Preorder – Inorder – Postorder • Our example considers printing node information

Preorder Traveral • Process current node • Process left subtree • Process right subtree

Preorder Traveral • Process current node • Process left subtree • Process right subtree public void print. Pre. Order(){ System. out. println(element. to. String()); if (left != null) left. print. Pre. Order(); if (right != null) right. print. Pre. Order(); return; }

print. Pre. Order Trace A C B E D A B D C E

print. Pre. Order Trace A C B E D A B D C E

Inorder Traveral • Process left subtree • Process current node • Process right subtree

Inorder Traveral • Process left subtree • Process current node • Process right subtree public void print. In. Order(){ if (left != null) left. print. Pre. Order(); System. out. println(element. to. String()); if (right != null) right. print. Pre. Order(); return; }

print. In. Order Trace A C B E D D B A C E

print. In. Order Trace A C B E D D B A C E

Postorder Traveral • Process left subtree • Process right subtree • Process current node

Postorder Traveral • Process left subtree • Process right subtree • Process current node public void print. Post. Order(){ if (left != null) left. print. Pre. Order(); if (right != null) right. print. Pre. Order(); System. out. println(element. to. String()); return; }

print. Post. Order Trace A C B E D D B E C A

print. Post. Order Trace A C B E D D B E C A

Methods in Binary. Tree • Two constructors – First takes in the element corresponding

Methods in Binary. Tree • Two constructors – First takes in the element corresponding to root (left and right of root set to null) – Second does not take in any parameters (empty tree) • Setter and getter for root • size () and height () implemented as root. size() and root. height() • Traversal methods – Invoking the corresponding methods on root