Presentation is loading. Please wait.

Presentation is loading. Please wait.

Trees.

Similar presentations


Presentation on theme: "Trees."— Presentation transcript:

1 Trees

2 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

3 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

4 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

5 Example

6 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

7 Illustration

8 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

9 Illustration

10 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

11 Applications

12 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

13 BinaryNode and BinaryTree
public class BinaryNode{ private Object element; private BinaryNode left; private BinaryNode right; } public class BinaryTree{ private BinaryNode root;

14 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)

15 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); }

16 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);

17 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

18 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; }

19 printPreOrder Trace A C B E D A B D C E

20 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; }

21 printInOrder Trace A C B E D D B A C E

22 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; }

23 printPostOrder Trace A C B E D D B E C A

24 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


Download ppt "Trees."

Similar presentations


Ads by Google