Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2.

Similar presentations


Presentation on theme: "COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2."— Presentation transcript:

1 COMP 103 Traversing Trees

2 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2

3 3 Depth first traversal – a "maze walk"  To get out of a maze: keep your left hand on the wall Variations:  pre-order: ABCDEFRoot, then children  post-order: CDBFEAChildren, then root  in-order (binary): CBDAFE Left child, root, right child A B E FD C

4 4 Depth First Traversal  Visit the subtree under each child before going to next child  Pre-order: process the node, then process the subtrees  Post-order: process the subtrees, then process the node. Easy to do recursively: the activation stack records where you are up to. Justine JeremyJulie John Julia Jesse Jordan Jenna JackJacobJules James JadaJenny Jackie Jenny Jake Jed Jude Jiles JoanJimJade

5 5 Depth-first traversal, recursively  eg: List all employee names  Use recursion, but use a loop to step through the children private void listAll(OrgTreeNode node) { System.out.println(node.employee); for (OrgTreeNode ch : node.children) listAll(ch); } OR private void listAll(OrgTreeNode node) { for (OrgTreeNode ch : node.children) listAll(ch); System.out.println(node.employee); } PRE-ORDER POST-ORDER

6 6 Count nodes  This involves doing a traversal and returning a value: private int count(TreeNode node) { int ans = 1; for (TreeNode ch : node.children) ans += count(ch); return ans; }

7 7 Finding a node  Same as a traversal, but “jump out” once you find it... private TreeNode find(TreeNode node, String name) { if (node.employee.hasName(name) ) return node; for (TreeNode ch : node.children) { TreeNode ans = find(ch, name); if (ans != null) return ans; } return null; } Search is a traversal with an early exit Search is a traversal with an early exit.

8 8 Arithmetic expression trees (5 x 2 ) + ( ( 7 – 3) / 14) + x 5 2 / - 7 3 14 "plus( times(5, 2), divide( minus(7, 3), 14) )" 5 2 x 7 3 – 14 / + + x / -2514 73 in-order traversal of the tree! "Polish notation": pre-order traversal “Reverse Polish notation” post-order traversal

9 9  We need to remember what nodes we are still working on, and which of their children we have processed already  In a recursive depth-first traversal (eg. "ListAll" from earlier), the activation stack remembers it all, automatically Justine 1.print own name 2.process children... 3.return Tracking State during Depth-first traversal Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake Jeremy 1.print own name 2.process children... 3.return John 1.print own name 2.process children... 3.return Refer to the “onion” example we looked at in the lecture on recursion

10 10 Breadth-first Traversal  What about printing the nodes by layer?  root first  then second layer,  then third layer, …. Justine Jeremy Julie Jenny John Julia Jada Jordan Jenny Jude Jiles Jules…. Justine JeremyJulie John Julia Jesse Jordan Jenna JackJacobJules James JadaJenny Jackie Jenny Jake Jed Jude Jiles JoanJimJade

11 11 Tracking State during Breadth-first traversal  Breadth first traversal:  What nodes do we have to remember?  Which order do we work on them? Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake

12 12 private void BreadthFirstTraversal(TreeNode root) { Queue todo = new LinkedList (); todo.offer(root); // enqueue while (!todo.isEmpty()) { TreeNode node = todo.poll(); // dequeue System.out.println(node.employee()); for (TreeNode child : node.getChildren()) todo.offer(child); } } Breadth-first Traversal: use a Queue! Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake Iterative

13 13 Depth First, iteratively: same code, but use Stack! private void DepthFirstTraversal(TreeNode root) { Stack todo = new Stack (); todo.push(root); while (!todo.isEmpty()) { TreeNode node = todo.pop(); System.out.println(node.employee()); for (TreeNode child : node.getChildren()) todo.push(child); } Justine Jeremy John Julia Jordan Jenna Jack JacobJules James Jada Jackie Jake pre- ? post- ? in- ?

14 14 In-order Traversal, iteratively?  What do we need to remember? + x / -2514 73

15 15 Summary: Traversals  Depth First  pre-order : in-order : post-order  Recursive (easy)  Iterative pre-order, with a Stack  Iterative in-order ??  Breadth First  can’t do it recursively  Iterative with a Queue.  Traversal vs Search  Basically the same thing, but search can exit in the middle.

16 16 Summary: Kinds of trees  Binary trees... Ternary trees … General trees  Ordered children (List)... Unordered children (Set)  Nodes with just children... children & parent... just parents  Tree structures to store structured data to keep track of an algorithm to store unstructured data efficiently (coming next…)  Ex: Compare two trees to see if they’re the same, or whether one is a subtree of the other.


Download ppt "COMP 103 Traversing Trees. 2 RECAP-TODAY RECAP  Building and Traversing Trees TODAY  Traversing Trees  Chapter 16, especially 16.2."

Similar presentations


Ads by Google