Upload
leo-mcgee
View
218
Download
0
Embed Size (px)
Citation preview
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
DAICBHEGF – in
HBDIACEFG – pre
ACIDBGFEH – post
H
EB
D F
I G
CA
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 inOrder () { inOrder (root);
}private void inOrder (Node current) {if (current != null) {
inOrder(current.getLeft());visit(current);inOrder(current.getRight());
} }
Pre Order traversal
public void preOrder () { preOrder (root);
}private void preOrder (Node current) { if (current != null) {
visit(current);preOrder(current.getLeft());preOrder(current.getRight());
}}
Post Order traversal
public void postOrder () { postOrder (root);
}private void postOrder (Node current) {if (current != null) {
postOrder(current.getLeft()); postOrder(current.getRight());
visit(current); }
}
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
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
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
Reconstructing a binary tree
• Time complexity
• This is the same recurrence relation of quick sort
( ) ( 1) ( ) ( )T n T i T n i O n
2( ) ( )
( ) ( log )
T n n worst case
T n n n averagecase
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
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
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());}
}
Depth
private int depth (Node node) { if (current == null) {
return 0; }else {
return Math.max(depth(node.getLeft()), depth(node.getRight())) + 1;
} }
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); }
}
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?
• 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.
/ (division operator) / \
/ \ - * / \ / \ / \ 2 a ^ * / \ / \ b 2 * c / \ 4 a
Questions
• 6. Write a method that given a TreeNode N , returned the number of elements contained at the tree rooted by N.