Presentation is loading. Please wait.

Presentation is loading. Please wait.

Tree Traversal.

Similar presentations


Presentation on theme: "Tree Traversal."— 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 public void inOrder () { inOrder (root); }
private void inOrder (Node current) { if (current != null) { inOrder(current.getLeft()); visit(current); inOrder(current.getRight());

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

12 Post Order traversal public void postOrder () { postOrder (root); }
private void 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) public void 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 private int depth (Node node) { if (current == null) { return 0;
} else { return Math.max(depth(node.getLeft()), depth(node.getRight())) + 1;

21 private void printLevel (Node current, int level) {
if (current != null) { if (level == 0) { visit (current); } else { printLevel (current.getLeft(), level - 1); printLevel (current.getRight(), level - 1); public void 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) / \ / \ * / \ / \ / \ 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.


Download ppt "Tree Traversal."

Similar presentations


Ads by Google