Tree Traversal Pre Order Tree Traversal 1 Visit
- Slides: 27
Tree Traversal
Pre Order Tree Traversal 1. Visit the root 2. Traverse the left sub-tree, 3. Traverse the right sub-tree
In Order Tree Traversal 1. Traverse the left sub-tree, 2. Visit the root 3. Traverse the right sub-tree
Post Order Tree Traversal 1. Traverse the left sub-tree, 2. Traverse the right sub-tree 3. Visit the root
Post Order Tree Traversal • A*(((B+C)*(D*E))+F) • A B C + D E * * F + *
Tree Traversal
Example • Print the pre, in and post order form of the following tree H DAICBHEGF – in HBDIACEFG – pre ACIDBGFEH – post E B F D I A G C
Example class Node { private Object value; private Node left, right; public Node (Object value) { this. value = value; }. . . }
Example class Tree { private Node root; public Tree () { root = null; } . . . }
In Order traversal public void in. Order () { in. Order (root); } private void in. Order (Node current) { if (current != null) { in. Order(current. get. Left()); visit(current); in. Order(current. get. Right()); } }
Pre Order traversal public void pre. Order () { pre. Order (root); } private void pre. Order (Node current) { if (current != null) { visit(current); pre. Order(current. get. Left()); pre. Order(current. get. Right()); } }
Post Order traversal public void post. Order () { post. Order (root); } private void post. Order (Node current) { if (current != null) { post. Order(current. get. Left()); post. Order(current. get. Right()); visit(current); } }
Example • Can we reconstruct a tree from it’s In. Order traversal ? • Are there two different trees with the same In. Order traversal D • What about pre and post order ? A
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
Reconstructing a binary tree Reconstruct. Tree (in. Order in[] pre. Order 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
Reconstructing a binary tree • Time complexity • This is the same recurrence relation of quick sort
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
Reconstructing a binary tree Reconstruct. Tree (in. Order in[] post. Order 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
Level traversal (BFS) public void BFS () { Queue q = new Queue (); if {root != null) q. enqueue (root); while (! q. is. Empty()) { Node current = (Node)queue. dequeue(); visit (current); if (current. get. Left() != null) q. enqueue(current. get. Left()); if (current. get. Right() != null) q. enqueue(current. get. Right()); } }
Depth private int depth (Node node) { if (current == null) { return 0; } else { return Math. max(depth(node. get. Left()), depth(node. get. Right())) + 1; } }
private void print. Level (Node current, int level) { if (current != null) { if (level == 0) { visit (current); } } else { print. Level (current. get. Left(), level - 1); print. Level (current. get. Right(), level - 1); } } public void print. All. Levels () { int depth = depth (root); for (int i = 0; i < depth; i++); print. Level(root, i); } }
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
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?
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 to yield the elements in reverse sorted order?
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.
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. / / - * / / 2 a ^ * / b 2 * c / 4 a (division operator)
Questions • 6. Write a method that given a Tree. Node N , returned the number of elements contained at the tree rooted by N.
- Pre order
- Pre order traversal
- Complexity of bellman ford
- Post order traversal
- In order traversal
- Via optica
- Reverse binary tree
- Which of the following tree traversal holds for clr
- Binary tree traversal techniques
- Huffman tree visualization
- Tree traversal visualization
- Dom tree
- Graph traversal in data structure
- Tree traversal
- Tree traversal in data structure
- Explain about threaded binary tree
- Cyclomatic complexity of binary search
- Root node
- Recover a tree from preorder traversal
- Bfs in discrete mathematics
- Osnowa dokumentu
- Traversal wave
- Graph traversal in data structure
- Euler tour traversal
- Nat traversal problem solution
- Iterative inorder traversal
- Graph traversal methods
- Understanding the efficiency of ray traversal on gpus