kenneth h. rosen chapter 8 trees
DESCRIPTION
Discrete Mathematics and Its Applications. Kenneth H. Rosen Chapter 8 Trees Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus. Tree. Definition 1. A tree is a connected undirected graph with no simple circuits. - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/1.jpg)
1
Kenneth H. Rosen
Chapter 8
Trees
Slides by Sylvia Sorkin, Community College of Baltimore County - Essex Campus
Discrete Mathematics and Its Applications
![Page 2: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/2.jpg)
2
Definition 1. A tree is a connected
undirected graph with no simple circuits.
Theorem 1. An undirected graph is a tree if
and only if there is a unique simple path
between any two of its vertices.
Tree
![Page 3: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/3.jpg)
3
Which graphs are trees?
a) b)
c)
![Page 4: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/4.jpg)
4
Specify a vertex as root
c)
Then, direct each edge away from the root.
ROOT
![Page 5: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/5.jpg)
5
Specify a root.
a)ROOT
Then, direct each edge away from the root.
![Page 6: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/6.jpg)
6
Specify a root.
a)ROOT
Then, direct each edge away from the root.
![Page 7: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/7.jpg)
7
Specify a root.
a)ROOT
Then, direct each edge away from the root.
A directed graph called a rooted tree results.
![Page 8: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/8.jpg)
8
a)ROOT
Then, direct each edge away from the root.
What if a different root is chosen?
![Page 9: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/9.jpg)
9
a)ROOT
Then, direct each edge away from the root.
What if a different root is chosen?
![Page 10: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/10.jpg)
10
a)ROOT
Then, direct each edge away from the root.
What if a different root is chosen?
![Page 11: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/11.jpg)
11
a)
ROOT
Then, direct each edge away from the root.
What if a different root is chosen?
A differentrooted tree results.
![Page 12: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/12.jpg)
12
Jake’s Pizza Shop Tree
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
![Page 13: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/13.jpg)
13
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has a Root
TREE ROOT
![Page 14: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/14.jpg)
14
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Leaf nodes have no children
LEAF NODES
![Page 15: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/15.jpg)
15
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
A Tree Has Levels
LEVEL 0
![Page 16: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/16.jpg)
16
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Level One
LEVEL 1
![Page 17: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/17.jpg)
17
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Level Two
LEVEL 2
![Page 18: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/18.jpg)
18
SIBLINGS
Sibling nodes have same parent
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
![Page 19: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/19.jpg)
19
SIBLINGS
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Sibling nodes have same parent
![Page 20: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/20.jpg)
20
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
A Subtree
LEFT SUBTREE OF ROOT
ROOT
![Page 21: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/21.jpg)
21
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
Another Subtree
RIGHT SUBTREE OF ROOT
ROOT
![Page 22: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/22.jpg)
22
A vertex that has children is called an internal vertex.
The subtree at vertex v is the subgraph of the tree consisting of vertex v and its descendants and all edges incident to those descendants.
Internal Vertex
![Page 23: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/23.jpg)
23
Owner Jake
Manager Brad Chef Carol
Waitress Waiter Cook Helper Joyce Chris Max Len
How many internal vertices?
![Page 24: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/24.jpg)
24
Definition 2’. A rooted tree is called a binary tree if every internal vertex has no more than 2 children.
The tree is called a full binary tree if every internal vertex has exactly 2 children.
Binary Tree
![Page 25: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/25.jpg)
25
Definition 2’’. An ordered rooted tree is a rooted tree where the children of each internal vertex are ordered.
In an ordered binary tree, the two possible children of a vertex are called the left child and the right child, if they exist.
Ordered Binary Tree
![Page 26: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/26.jpg)
26
Theorem 2. A tree with N vertices has N-1 edges.
Theorem 5. There are at most 2 H leaves in a
binary tree of height H.
Corallary. If a binary tree with L leaves is full and
balanced, then its height is
H = log2 L .
Tree Properties
![Page 27: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/27.jpg)
27
An Ordered Binary Tree
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
![Page 28: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/28.jpg)
28
The parent of a non-root vertex is the
unique vertex u with a directed edge
from u to v.
Parent
![Page 29: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/29.jpg)
29
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
What is the parent of Ed?
![Page 30: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/30.jpg)
30
A vertex is called a leaf if it has no
children.
Leaf
![Page 31: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/31.jpg)
31
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
How many leaves?
![Page 32: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/32.jpg)
32
The ancestors of a non-root vertex are
all the vertices in the path from root to
this vertex.
Ancestors
![Page 33: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/33.jpg)
33
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
How many ancestors of Ken?
![Page 34: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/34.jpg)
34
The descendants of vertex v are all the
vertices that have v as an ancestor.
Descendants
![Page 35: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/35.jpg)
35
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
How many descendants of Hal?
![Page 36: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/36.jpg)
36
The level of vertex v in a rooted tree is
the length of the unique path from the
root to v.
Level
![Page 37: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/37.jpg)
37
What is the level of Ted?
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
![Page 38: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/38.jpg)
38
The height of a rooted tree is the
maximum of the levels of its vertices.
Height
![Page 39: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/39.jpg)
39
What is the height?
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
![Page 40: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/40.jpg)
40
A rooted binary tree of height H is
called balanced if all its leaves are at
levels H or H-1.
Balanced
![Page 41: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/41.jpg)
41
Is this binary tree balanced?
Hal
Lou
Ken
Joe Ted
Sue Ed
Max
![Page 42: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/42.jpg)
42
So the goal in computer programs is to find any stored item efficiently when all stored items are ordered.
A Binary Search Tree can be used to store items in its vertices. It enables efficient searches.
Searching takes time . . .
![Page 43: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/43.jpg)
A special kind of binary tree in which:
1. Each vertex contains a distinct key value,
2. The key values in the tree can be compared using “greater than” and “less than”, and
3. The key value of each vertex in the tree is
less than every key value in its right subtree, and greater than every key value in its left subtree.
A Binary Search Tree (BST) is . . .
![Page 44: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/44.jpg)
44
Depends on its key values and their order of insertion.
Insert the elements ‘J’ ‘E’ ‘F’ ‘T’ ‘A’ in that order.
The first value to be inserted is put into the root.
Shape of a binary search tree . . .
‘J’
![Page 45: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/45.jpg)
45
Thereafter, each value to be inserted begins by comparing
itself to the value in the root, moving left it is less, or
moving right if it is greater. This continues at each
level until it can be inserted as a new leaf.
Inserting ‘E’ into the BST
‘J’
‘E’
![Page 46: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/46.jpg)
46
Begin by comparing ‘F’ to the value in the root,
moving left it is less, or moving right if it is greater.
This continues until it can be inserted as a leaf.
Inserting ‘F’ into the BST
‘J’
‘E’
‘F’
![Page 47: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/47.jpg)
47
Begin by comparing ‘T’ to the value in the root, moving
left it is less, or moving right if it is greater. This
continues until it can be inserted as a leaf.
Inserting ‘T’ into the BST
‘J’
‘E’
‘F’
‘T’
![Page 48: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/48.jpg)
48
Begin by comparing ‘A’ to the value in the root, moving
left it is less, or moving right if it is greater. This
continues until it can be inserted as a leaf.
Inserting ‘A’ into the BST
‘J’
‘E’
‘F’
‘T’
‘A’
![Page 49: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/49.jpg)
49
is obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order?
What binary search tree . . .
‘A’
![Page 50: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/50.jpg)
50
obtained by inserting
the elements ‘A’ ‘E’ ‘F’ ‘J’ ‘T’ in that order.
Binary search tree . . .
‘A’
‘E’
‘F’
‘J’
‘T’
![Page 51: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/51.jpg)
51
Another binary search tree
Add nodes containing these values in this order:
‘D’ ‘B’ ‘L’ ‘Q’ ‘S’ ‘V’ ‘Z’
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’
‘K’ ‘P’
![Page 52: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/52.jpg)
52
Is ‘F’ in the binary search tree?
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’
‘K’
‘V’
‘P’ ‘Z’‘D’
‘Q’‘L’‘B’
‘S’
![Page 53: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/53.jpg)
53
A traversal algorithm is a procedure for
systematically visiting every vertex of
an ordered binary tree.
Tree traversals are defined recursively.
Three traversals are named:
preorder,
inorder,
postorder.
Traversal Algorithms
![Page 54: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/54.jpg)
54
Let T be an ordered binary tree with root r.
If T has only r, then r is the preorder traversal.
Otherwise, suppose T1, T2 are the left and
right subtrees at r. The preorder traversal
begins by visiting r. Then traverses T1 in
preorder, then traverses T2 in preorder.
PREORDER Traversal Algorithm
![Page 55: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/55.jpg)
55
Preorder Traversal: J E A H T M Y
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
ROOT
Visit left subtree second Visit right subtree last
Visit first.
![Page 56: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/56.jpg)
56
Let T be an ordered binary tree with root r.
If T has only r, then r is the inorder traversal.
Otherwise, suppose T1, T2 are the left and
right subtrees at r. The inorder traversal
begins by traversing T1 in inorder. Then
visits r, then traverses T2 in inorder.
INORDER Traversal Algorithm
![Page 57: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/57.jpg)
57
Inorder Traversal: A E H J M T Y
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
ROOT
Visit left subtree first Visit right subtree last
Visit second
![Page 58: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/58.jpg)
58
Let T be an ordered binary tree with root r.
If T has only r, then r is the postorder traversal.
Otherwise, suppose T1, T2 are the left and right
subtrees at r. The postorder traversal begins
by traversing T1 in postorder. Then traverses
T2 in postorder, then ends by visiting r.
POSTORDER Traversal Algorithm
![Page 59: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/59.jpg)
59
‘J’
‘E’
‘A’ ‘H’
‘T’
‘M’ ‘Y’
ROOT
Visit left subtree first Visit right subtree second
Visit last
Postorder Traversal: A H E M Y T J
![Page 60: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/60.jpg)
60
A Binary Expression Tree
‘-’
‘8’ ‘5’
ROOT
INORDER TRAVERSAL: 8 - 5 has value 3
PREORDER TRAVERSAL: - 8 5
POSTORDER TRAVERSAL: 8 5 -
![Page 61: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/61.jpg)
61
A special kind of binary tree in which:
1. Each leaf node contains a single operand,
2. Each nonleaf node contains a single binary operator, and
3. The left and right subtrees of an operator node represent subexpressions that must be evaluated before applying the operator at the root of the subtree.
A Binary Expression Tree is . . .
![Page 62: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/62.jpg)
62
A Binary Expression Tree
‘*’
‘+’
‘4’
‘3’
‘2’
What value does it have?
( 4 + 2 ) * 3 = 18
![Page 63: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/63.jpg)
63
A Binary Expression Tree
‘*’
‘+’
‘4’
‘3’
‘2’
What infix, prefix, postfix expressions does it represent?
![Page 64: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/64.jpg)
64
A Binary Expression Tree
‘*’
‘+’
‘4’
‘3’
‘2’
Infix: ( ( 4 + 2 ) * 3 )
Prefix: * + 4 2 3 evaluate from right
Postfix: 4 2 + 3 * evaluate from left
![Page 65: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/65.jpg)
65
Levels Indicate Precedence
When a binary expression tree is used to represent an expression, the levels of the nodes in the tree indicate their relative precedence of evaluation.
Operations at higher levels of the tree are evaluated later than those below them. The operation at the root is always the last operation performed.
![Page 66: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/66.jpg)
66
Evaluate this binary expression tree
‘*’
‘-’
‘8’ ‘5’
What infix, prefix, postfix expressions does it represent?
‘/’
‘+’
‘4’
‘3’
‘2’
![Page 67: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/67.jpg)
67
A binary expression tree
Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )
Prefix: * - 8 5 / + 4 2 3
Postfix: 8 5 - 4 2 + 3 / * has operators in order used
‘*’
‘-’
‘8’ ‘5’
‘/’
‘+’
‘4’
‘3’
‘2’
![Page 68: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/68.jpg)
68
A binary expression tree
Infix: ( ( 8 - 5 ) * ( ( 4 + 2 ) / 3 ) )
Prefix: * - 8 5 / + 4 2 3 evaluate from right
Postfix: 8 5 - 4 2 + 3 / * evaluate from left
‘*’
‘-’
‘8’ ‘5’
‘/’
‘+’
‘4’
‘3’
‘2’
![Page 69: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/69.jpg)
69
Inorder Traversal: (A + H) / (M - Y)
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
ROOT
Print left subtree first Print right subtree last
Print second
![Page 70: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/70.jpg)
70
Preorder Traversal: / + A H - M Y
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
Print left subtree second Print right subtree last
Print first
ROOT
![Page 71: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/71.jpg)
71
‘/’
‘+’
‘A’ ‘H’
‘-’
‘M’ ‘Y’
Print left subtree first Print right subtree second
Print last
Postorder Traversal: A H + M Y - /
ROOT
![Page 72: Kenneth H. Rosen Chapter 8 Trees](https://reader033.vdocuments.us/reader033/viewer/2022061409/5681404c550346895dabbe19/html5/thumbnails/72.jpg)
72
ACKNOWLEDGMENT:
This project was supported in part by the National Science Foundation under grant DUE-ATE 9950056.