1 / 27

Tree Traversal

Tree Traversal. Pre Order Tree Traversal. Visit the root Traverse the left sub-tree, Traverse the right sub-tree. In Order Tree Traversal. Traverse the left sub-tree, Visit the root Traverse the right sub-tree. Post Order Tree Traversal. Traverse the left sub-tree,

rmichaud
Télécharger la présentation

Tree Traversal

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Tree Traversal

  2. Pre Order Tree Traversal • Visit the root • Traverse the left sub-tree, • Traverse the right sub-tree

  3. In Order Tree Traversal • Traverse the left sub-tree, • Visit the root • Traverse the right sub-tree

  4. Post Order Tree Traversal • Traverse the left sub-tree, • Traverse the right sub-tree • Visit the root

  5. Post Order Tree Traversal • A*(((B+C)*(D*E))+F) • A B C + D E * * F + *

  6. Tree Traversal

  7. Example • Print the pre, in and post order form of the following tree DAICBHEGF – in HBDIACEFG – pre ACIDBGFEH – post H E B F D I G A C

  8. Example class Node { private Object value; private Node left, right; public Node (Object value) { this.value = value; } ... }

  9. Example class Tree { private Node root; public Tree () { root = null; } ... }

  10. In Order traversal publicvoid inOrder () { inOrder (root); } privatevoid inOrder (Node current) { if (current != null) { inOrder(current.getLeft()); visit(current); inOrder(current.getRight()); } }

  11. Pre Order traversal publicvoid preOrder () { preOrder (root); } privatevoid preOrder (Node current) { if (current != null) { visit(current); preOrder(current.getLeft()); preOrder(current.getRight()); } }

  12. Post Order traversal publicvoid postOrder () { postOrder (root); } privatevoid postOrder (Node current) { if (current != null) { postOrder(current.getLeft()); postOrder(current.getRight()); visit(current); } }

  13. Example • Can we reconstruct a tree from it’s InOrder traversal ? • Are there two different trees with the same InOrder traversal • What about pre and post order ? D A

  14. Example • Can we reconstruct a tree given both it’s in order and pre order traversals ? in order - DAICBHEGF pre order - HBDIACEFG (find the root) Left sub tree DAICB BDIAC Right sub tree EGF EFG

  15. Reconstructing a binary tree ReconstructTree (inOrder in[] preOrder pre[]) if (n==0) return null root (T)  pre[1] find index i s.t in[i] = pre[1] left(T)  Reconstruct (in[1…i-1], pre [2…i]) right(T)  Reconstruct (in[i+1..n], pre [i+1…n]) return T

  16. Reconstructing a binary tree • Time complexity • This is the same recurrence relation of quick sort

  17. Binary Search Trees • In a BST the in order output is a sorted list of the tree nodes. • If we obtain a pre order path of a BST, we can sort the nodes and use the output to reconstruct the tree using the previous algorithm

  18. Reconstructing a binary tree ReconstructTree (inOrder in[] postOrder post[]) if (n==0) return null root (T)  post[n] find index i s.t in[i] = post[n] left(T)  Reconstruct (in[1…i-1], post [1…i-1]) right(T)  Reconstruct (in[i+1..n], post[i…n-1]) return T

  19. Level traversal (BFS) publicvoid BFS () { Queue q = new Queue (); if {root != null) q.enqueue (root); while (! q.isEmpty()) { Node current = (Node)queue.dequeue(); visit (current); if (current.getLeft() != null) q.enqueue(current.getLeft()); if (current.getRight() != null) q.enqueue(current.getRight()); } }

  20. Depth privateint depth (Node node) { if (current == null) { return0; } else { return Math.max(depth(node.getLeft()), depth(node.getRight())) + 1; } }

  21. privatevoid printLevel (Node current, int level) { if (current != null) { if (level == 0) { visit (current); } } else { printLevel (current.getLeft(), level - 1); printLevel (current.getRight(), level - 1); } } publicvoid printAllLevels () { int depth = depth (root); for (int i = 0; i < depth; i++); printLevel(root, i); } }

  22. Questions • 1. Draw a single Binary Tree such that each node contains a single character and: • a.  The pre-order traversal results in EXAMFUN • b.  The in-order traversal results in MAFXUEN

  23. Questions • 2. An in-order traversal of a Binary search tree yields a sorted list of elements in O(n) time. Does this fact contradict our comparison-based lower bound of nlogn time? Can something similar be done using a binary heap?

  24. Questions • 3. Is there a heap T storing seven distinct elements such that a preorder traversal of T yields the elements of T in sorted order? • How about an in-order traversal? • How about a post-order traversal? • How about a post-order traversal to yield the elements in reverse sorted order?

  25. Questions • 4. Let T be a binary search tree with more than a single node. Is it possible that the preorder traversal of T visits the nodes in the same order as the post-order traversal of T? • If so, give an example; otherwise, argue why this cannot occur. Likewise, is it possible that the preorder traversal of T visits the nodes in the reverse order of the post-order traversal of T? • If so, give an example; otherwise, argue why this cannot occur.

  26. Questions • 5. Given the following binary tree, print out the order of the nodes for an in-order traversal, preorder traversal, and post-order traversal. / (division operator) / \ / \ - * / \ / \ / \ 2 a ^ * / \ / \ b 2 * c / \ 4 a

  27. Questions • 6. Write a method that given a TreeNode N , returned the number of elements contained at the tree rooted by N.

More Related