Professional Documents
Culture Documents
Maolin Tang
Science and Engineering Faculty
Queensland University of Technology
real world 2
R
a university for the
real world 3
R
a university for the
real world 4
R
a university for the
real world 5
R
a university for the
real world 6
R
a university for the
root
A
B C ^
D ^^ E ^^ F ^
G ^
H ^^
^ null
real world 7
R
a university for the
50 70 50 70
80 30 55 75
30 55 65 65
75 80
real world 8
R
a university for the
• Often one wishes to visit each of the nodes in a binary tree and
examine the value there.
• There are three common orders in which the nodes can be
visited, and each has useful properties that are exploited in
algorithms based on binary trees.
– Pre-order
– In-order
– Post-order
real world 9
R
a university for the
ABDECFGH
real world 10
R
a university for the
DBEACGHF
real world 11
R
a university for the
• Post-order traversal is
recursively defined as follows:
– traverse the left subtree
in post-order, and then
– traverse the right subtree
in post-order, and then
– visit the root.
DEBHGFCA
real world 12
R
a university for the
real world 13
R
a university for the
Search for 80
60
50 70
30 55 80
65
75
real world 14
R
a university for the
Search for 79
60
50 70
30 55 80
65
75
real world 15
R
a university for the
Insert 45
60
50 70
30 55 80
65
45 75
real world 16
R
a university for the
real world 17
R
a university for the
parent parent
ptr
real world 18
R
a university for the
parent
parent
ptr
c
c
real world 19
R
a university for the
The right-most
node in the left
subtree of ptr
real world 20
R
a university for the
real world 21
R
a university for the
root
edge
node
real world 22
R
a university for the
real world 23
R
a university for the
Depth = 0
subtree
Height = 3 Depth = 1
Depth = 2
real world 24
R
a university for the
real world 25
R
a university for the
root
A ^ ^ null
B C ^ E ^
F ^ G ^ H ^^ I ^ J ^^
real world 26
R
a university for the
real world 27
R
a university for the
1
depth = 0
2 3 4
depth = 1
5 6 7 8 9
depth = 2
Output: A B C E F G H I J
real world 28
R
a university for the
• Basic operations
Enqueue(5) Enqueue(9)
1 3 2 1 3 2 5 1 3 2 5 9
Dequeue() 1
Dequeue() 3
2 5 9 3 2 5 9
ALGORITHM BreadthFirstTraveral(root)
q ← Φ //q is a queue; initially it is empty
q.enqueue(root)
while q ≠ Φ do
r ← q.dequeue()
visit r;
// add r’s all children to q one by one from left to right
r ← r.firstchild; //r.firstchild is the first child of r
while r ≠ null do
q.enqueue(r)
r ← r.firstsibling
real world 31
R
a university for the
after visiting A B C E
after visiting B C E F G H
after visiting C E F G H
after visiting E F G H I J
after visiting F G H I J
after visiting G H I J
after visiting H I J
after visiting I J
after visiting J
real world 32
R
a university for the
real world 33
R
a university for the
2 6 7
3 4 5 8 9
Output: A B F G H C E I J
real world 34
R
a university for the
Push(1) Push(3)
3
1 1
empty stack
Push(2)
Pop() 3 Pop() 2
2
3 3
1 1 1
ALGORITHM DepthFirstTraversal(root)
s ← Φ // s is a stack; initially it is empty
s.push(root)
while s ≠ Φ do
r = s.pop()
repeat
visit r
if r.firstsibling ≠ null s.push(r.firstsibling)
r ← r.firstchild;
until r = null
real world 37
R
a university for the
after visiting A
after visiting B C
after visiting F C G
after visiting G C H
A after visiting H C
after visiting C E
B C E
after visiting E
after visiting I J
F G H I J
after visiting J
real world 38
R
a university for the