Professional Documents
Culture Documents
Objectives
In this session, you will learn to:
Store data in a tree
Implement a binary tree
Implement a binary search tree
Ver. 1.0
Session 13
Ver. 1.0
Session 13
A
C
J
L
Ver. 1.0
Session 13
J
L
Ver. 1.0
node
Session 13
J
L
Ver. 1.0
node
Session 13
A
C
Nodes E, F, G, H, I, J,
L, and M are leaf nodes.
J
L
Ver. 1.0
Session 13
E, F, with
G, and
are
Tree
rootHB,
children of nodes
node B.
containing
E,BF,
is the
these of
G,
andparent
H is aof
subtree
nodes.
node
A.
J
L
Ver. 1.0
Session 13
A
C
Degree of node C is 1
Degree of node D is 2
Degree of node A is 3
Degree of node B is 4
Edge
E
J
L
Ver. 1.0
Session 13
A
C
J
L
Ver. 1.0
Session 13
A
C
Nodes B, C, D, and K
are internal nodes. Level 1
J
L
Ver. 1.0
Level 2
Level 3
Session 13
A
C
Level 1
J
L
Ver. 1.0
Level 2
Level 3
Session 13
D
H
Ver. 1.0
root
C
E
Session 13
Ver. 1.0
4
D and E
C
2
H does not have any siblings
The only sibling of D is E
F, G, H, and I
Session 13
Ver. 1.0
Session 13
Ver. 1.0
Depth = 3
Total number of
3
nodes = 2 1 = 7
Session 13
1
3
B
4
Ver. 1.0
C
3
B
4
C
3
B
4
C
5
Session 13
0
A
3
D
C
4
E
Binary Tree
Ver. 1.0
[0]
[1]
[2]
[3]
[4]
[5]
[6]
Array Representation
Session 13
If a node does not have a left child or a right child or both, the
respective left or right child fields of that node point to NULL.
Data
Node
Ver. 1.0
Session 13
root
52
36
24
68
59
70
Binary Tree
Ver. 1.0
24
72
80
52
.
.
36
68
.
.
59
72
70
80
Linked Representation
Session 13
Ver. 1.0
Session 13
Ver. 1.0
Session 13
Ver. 1.0
Session 13
D
Ver. 1.0
Session 13
D
Ver. 1.0
Session 13
D
Ver. 1.0
H
Session 13
D
Ver. 1.0
H
Session 13
D
Ver. 1.0
B
Session 13
D
Ver. 1.0
B
Session 13
D
Ver. 1.0
E
Session 13
D
Ver. 1.0
E
Session 13
D
Ver. 1.0
A
Session 13
D
Ver. 1.0
A
Session 13
D
Ver. 1.0
A
Session 13
D
Ver. 1.0
F
Session 13
D
Ver. 1.0
F
Session 13
D
Ver. 1.0
C
Session 13
D
Ver. 1.0
C
Session 13
D
Ver. 1.0
C
Session 13
D
Ver. 1.0
I
Session 13
D
Ver. 1.0
I
Session 13
D
Ver. 1.0
G
Session 13
D
Ver. 1.0
Traversal complete
G
Session 13
Ver. 1.0
Session 13
Preorder Traversal:
Ver. 1.0
A B D H E
C F
Session 13
Ver. 1.0
Session 13
Postorder Traversal: H D E
Ver. 1.0
B F
G C A
Session 13
Answer:
Preorder
Ver. 1.0
Session 13
Ver. 1.0
Session 13
Ver. 1.0
Session 13
24
44
40
Ver. 1.0
59
72
55
Session 13
Ver. 1.0
Session 13
b.
c.
Ver. 1.0
Display Found
Exit
Session 13
Answer:
smaller
Ver. 1.0
Session 13
Ver. 1.0
Session 13
52
1.
2.
3.
4.
5.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
root
52
1.
2.
3.
4.
5.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
root
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
root
parent = NULL
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
root
parent = NULL
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
root
parent
parent= NULL
currentNode
52
1.
2.
3.
4.
5.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
1.
2.
3.
4.
5.
55 > 52
root
parent
currentNode
52
a.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
1.
2.
3.
4.
5.
55 > 52
root
parent
currentNode
52
a.
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
1.
2.
3.
4.
5.
55 > 52
root
parent
currentNode
52
a.
currentNode
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
root
parent
52
1.
2.
3.
4.
5.
currentNode
36
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
root
parent
52
.
.
36
2.
3.
4.
5.
currentNode
parent
1.
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
55 < 68
root
52
.
.
36
2.
3.
4.
5.
currentNode
parent
1.
68
6.
24
59
.
Ver. 1.0
70
72
.
80
Session 13
55 < 68
root
52
.
.
36
2.
3.
4.
5.
currentNode
parent
1.
68
6.
24
59
72
currentNode
.
Ver. 1.0
70
80
Session 13
root
52
1.
2.
3.
4.
5.
parent
36
68
6.
24
59
72
currentNode
.
Ver. 1.0
70
80
Session 13
root
52
1.
2.
3.
4.
5.
parent
36
68
6.
parent
24
59
72
currentNode
.
Ver. 1.0
70
80
Session 13
1.
2.
3.
4.
5.
55 < 59
root
52
a.
36
68
6.
parent
24
59
72
currentNode
.
Ver. 1.0
70
80
Session 13
1.
2.
3.
4.
5.
55 < 59
root
52
a.
36
68
6.
parent
24
currentNode
currentNode = NULL
Ver. 1.0
59
70
72
.
80
Session 13
root
52
1.
2.
3.
4.
5.
36
68
6.
parent
24
currentNode = NULL
Ver. 1.0
59
70
72
.
80
Session 13
52
1.
2.
3.
4.
5.
36
68
6.
parent
24
currentNode = NULL
Ver. 1.0
59
70
72
.
80
Session 13
Ver. 1.0
Session 13
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
55
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
24
59
72
b.
7.
55
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
59
70
80
Ver. 1.0
root
.
.
52
2.
3.
4.
5.
a.
b.
36
68
6.
parent
24
72
b.
7.
55
70
59
80
Ver. 1.0
Session 13
.
.
24
52
.
.
36
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
.
24
52
.
.
36
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
.
24
52
.
.
36
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
.
24
52
.
.
36
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
.
24
52
parent
36
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
.
24
52
parent
36
68
2.
3.
b.
70 > 52
59
1.
72
i.
c.
70
80
69
Ver. 1.0
Session 13
.
24
52
parent
36
68
2.
3.
b.
70 > 52
59
1.
72
i.
c.
70
80
69
Ver. 1.0
Session 13
52
parent
currentNode
.
24
36
68
2.
3.
b.
70 > 52
59
1.
72
i.
c.
70
80
69
Ver. 1.0
Session 13
52
parent
currentNode
.
24
36
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
52
parent
currentNode
.
24
36
parent
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
52
70 > 68
currentNode
.
24
36
parent
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
52
70 > 68
currentNode
.
24
36
parent
68
2.
3.
b.
i.
59
1.
72
c.
70
80
69
Ver. 1.0
Session 13
52
70 > 68
currentNode
36
parent
68
1.
2.
3.
b.
i.
currentNode
24
59
72
c.
70
80
69
Ver. 1.0
Session 13
.
.
36
52
parent
68
1.
2.
3.
b.
i.
currentNode
24
59
72
c.
70
80
69
Ver. 1.0
Session 13
.
.
36
52
parent
68
1.
2.
3.
b.
i.
currentNode
24
59
72
c.
70
parent
80
69
Ver. 1.0
Session 13
.
.
52
70 < 72
36
68
1.
2.
3.
b.
i.
currentNode
24
59
72
c.
70
parent
80
69
Ver. 1.0
Session 13
.
.
52
70 < 72
36
68
1.
2.
3.
b.
i.
currentNode
24
59
72
c.
70
parent
80
currentNode
69
Ver. 1.0
Session 13
.
.
52
.
.
36
24
68
2.
3.
b.
.
.
59
Nodes located
1.
72
c.
70
parent
80
currentNode
69
Ver. 1.0
Session 13
Ver. 1.0
Session 13
Ver. 1.0
Session 13
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
Ver. 1.0
Session 13
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
parent
currentNode
Ver. 1.0
Session 13
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
parent
currentNode
Ver. 1.0
Session 13
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
parent
currentNode
Ver. 1.0
Session 13
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
parent
currentNode
Ver. 1.0
Session 13
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
parent
currentNode
Ver. 1.0
Session 13
52
1.
2.
.
3.
36
68
24
59
72
4.
70
b.
80
5.
69
parent
Session 13
Ver. 1.0
Session 13
1.
2.
3.
4.
5.
6.
7.
root
.
.
24
52
.
.
36
68
.
.
59
72
.
.
70
80
75
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
child
Ver. 1.0
Session 13
2.
3.
4.
Delete node 80
root
.
.
52
.
.
36
68
.
parent
24
59
72
.
currentNode 5.
70
80
6.
7.
75
Session 13
Ver. 1.0
Session 13
1.
2.
3.
4.
5.
root
.
.
24
52
.
.
36
68
.
.
59
72
70
75
Ver. 1.0
.
80
Session 13
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
70
75
Ver. 1.0
.
80
Session 13
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
70
75
Ver. 1.0
.
80
Session 13
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
80
Inorder_suc
75
Ver. 1.0
Session 13
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
80
Inorder_suc
75
Ver. 1.0
Session 13
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
.
.
70
80
Inorder_suc
75
Inorder_suc
Ver. 1.0
Session 13
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
72
75
.
.
70
80
75
Inorder_suc
Ver. 1.0
Session 13
2.
3.
4.
5.
Delete node 72
root
52
.
parent
36
68
.
currentNode
24
59
75
.
.
70
80
75
Inorder_suc
Ver. 1.0
Session 13
2.
3.
4.
5.
52
.
parent
36
68
.
currentNode
24
59
75
.
.
70
80
75
Session 13
Ver. 1.0
Session 13
Ver. 1.0
Session 13
Ver. 1.0
Session 13