Trees
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 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 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
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
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
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
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
BinaryNode and BinaryTree public class BinaryNode{ private Object element; private BinaryNode left; private BinaryNode right; } public class BinaryTree{ private BinaryNode root;
Methods in BinaryNode 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 == 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 && 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 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 public void printPreOrder(){ System.out.println(element.toString()); if (left != null) left.printPreOrder(); if (right != null) right.printPreOrder(); return; }
printPreOrder Trace A C B E D A B D C E
Inorder Traveral Process left subtree Process current node Process right subtree public void printInOrder(){ if (left != null) left.printPreOrder(); System.out.println(element.toString()); if (right != null) right.printPreOrder(); return; }
printInOrder Trace A C B E D D B A C E
Postorder Traveral Process left subtree Process right subtree Process current node public void printPostOrder(){ if (left != null) left.printPreOrder(); if (right != null) right.printPreOrder(); System.out.println(element.toString()); return; }
printPostOrder Trace A C B E D D B E C A
Methods in BinaryTree Two constructors Setter and getter for root 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