Download presentation
Presentation is loading. Please wait.
1
7.3 Tree Searching
2
As each vertex of the tree is encountered, you may want to perform a task. You may want to perform a calculation or display something. Performing tasks at a vertex is called visiting. The process of visiting each vertex in some order is called searching the tree or performing a tree search. If a root has a child to the left then it has a left subtree VL. If a root ahs a child to the right then it has a right subtree VR We will review 3 ways to search a tree: Pre order, In order and Post order.
3
Name Rules Example Pre Order Root, Left, Right 9, 4, 2, 3, 8, 6, 7
The name of the search indicates when the root is visited. (The root goes at the beginning. To start, go to the left side and apply the rule to the first vertex you come to.) (The root goes in the middle. To start, first find the lowest left most vertex and apply the rule.) (The root goes at the end. To start, on the left, find the lowest left most vertex and apply the rule. When done with the left, go to the right. To start the right side, find the first left most vertex and apply the rule.) Name Rules Example Pre Order Root, Left, Right 9, 4, 2, 3, 8, 6, 7 Name Rules Example In order Left, Root, Right 2, 4, 3, 9 ,6, 8, 7 Name Rules Example Post order Left, Right, Root 2, 3, 4, 6, 7, 8, 9
4
Break everything down to a subset and apply the rule.
9 LEFT SIDE RIGHT SIDE 8 4 7 3 6 2
5
Expression: (a-b)*(c+(d ÷ e)) Digraph:
Applying pre order (root, left, right(root at the beginning. To start, go to the left side and apply the rule to the first vertex you come to)): *- a b + c ÷ d e This is called the Polish form. We have the operational symbol before the arguments. * - + c ÷ a b d e
6
For pre order search, if we replace the letters with numbers:
* - a b + c ÷ d e a = 6, b = 4, c = 5, d = 2 e = 2 * ÷ 2 2 We move from left to right. Look at the sign closest to the first pair of numbers: put the – between the 6 and 4: 6 – 4 6 – 4 = 2. Replace – 6 4 with 2 * ÷ 2 2 The next pair of numbers with a sign in front of them is ÷ 2 2 2 ÷ 2 = 1 Replace ÷ 2 2 with 1 * The next pair of numbers with a sign in front of them is + 5 1 5 + 1 = 6. Replace with 6. *26 2 * 6 = 12 The result is 12.
7
In order search produces:
a – b * c + d ÷ e Replacing the variables with numbers a = 6, b = 4, c = 5, d = 2 e = 2 6 - * ÷ 2 We don’t know where the parentheses go, so this method doesn’t work.
8
Post order (left, right, root ), Reverse Polish
Root goes at the end. To start, first find the lowest left most vertex and apply the rule. To start the right side, find the first left most vertex and apply the rule.) It’s a little tricky on the right side. a b – c d e ÷ + * The operator is listed after the arguments. a = 2, b = 1, c = 3, d = 4, e = 2 2 1 – ÷ + * Reading left to right, we first see = 1 replace 2 1 – with 1 ÷ + * Next, we see 4 2 ÷ 4 ÷ 2 = 2 replace 4 2 ÷ with 2 * Next we see = 5 replace with 5 1 5 * 1 * 5 = 5 Final answer is 5
9
Labeled Binary Positional Tree: Digraph:
To draw this as a Binary Positional tree first, draw the same left most edge. 1 2 4 3 5 6 7 8 10 9 11 12 13 1 2 5 11
10
Next we look at every parent at level 1 and draw a line from vertex 2 in this diagram for 3 and is the second offspring of the root and 4 is the third offspring of the root. 3 has offspring 8 that is a leaf. 4 has offspring 9 and 10 that are leaves. 1 2 5 3 11 4 1 2 5 3 11 8 4 9 10
11
5 is the first offspring of 2
6 is the second offspring of 2 7 is the third offspring of 2 1 2 3 1 5 8 4 2 6 9 11 3 7 10 8 5 4 9 6 The first offspring of 5 is 11 The second offspring of 5 is 12 The third offspring of 5 is 13 11 10 12 7 13
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.