Tree Traversal Pre Order Tree Traversal 1 Visit

  • Slides: 27
Download presentation
Tree Traversal

Tree Traversal

Pre Order Tree Traversal 1. Visit the root 2. Traverse the left sub-tree, 3.

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.

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

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 *

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

Tree Traversal

Tree Traversal

Example • Print the pre, in and post order form of the following tree

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

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;

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

In Order traversal public void in. Order () { in. Order (root); } private

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

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

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 ? •

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

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)

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

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

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)

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

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;

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

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

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

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

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

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

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

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