Unit I
Unit I
TREES
Binary tree
ADT representations
Recursive and Non Recursive Traversals
Binary Search tree
Insertion
Deletion
branches
root
branches
nodes
– Employees of a corporation.
• President, vice presidents, managers, and so on.
Hierarchy Data
• The element at the top of the hierarchy is the
root.
• Elements next in the hierarchy are the
children of the root.
• Elements next in the hierarchy are the
grandchildren of the root, and so on.
• Elements that have no children are leaves.
SCSA 1304 - Unit I School of Computing 12
Example
(root) (children)
great grand children
(grand children)
Level 1 Level 2
Level 5 Level 6
Level 4
Height = Depth = 6
3 0
1
1 0
1 2
0
1 3 0
1 0
1 2
Tree Degree = 3
B C D
E F G H I J
K L
List M
List B C D
A(B(E(K,L),F),C(G),(D(H(M),I,J))) E F G H I J
K L M
A 0
D I 0
B C G 0
H M 0
E K L 0
b b
D E F G
H I J K
B C
D E F G
H I J K
B C
D E F G
H I J K L M N O
B C
n2 = 5
no = 6
D E F G
H I J K
n2 = 0 C
C
no = 1
D
D
E
n 0 = n2 + 1
Hence Proved.
• Array Representation
• Linked Representation
2 3
4 5 6 7
8 9 10 11 12 13 14 15
Example 3 [0]
[1]
--
A
[2] --
A
[3] B
[4] --
B [5] --
[6] --
[7] C
C
[8] --
[9] --
D … --
[15] D
… --
E
[32] E
data
leftChild rightChild
B 0
B
C 0
C
D 0
D
0 E 0
E
A
Example 2
0 B
B
C 0 C
D
0 D
E
0 E 0
B C
D E F G
0 H 0 0 I 0 0 J 0 0 K 0 0 L 0 0 M 0 0 N 0 0 O 0
b c
bac
e: k.
i S L ht
l u ac
V s e al u ub
sit Su
c
2.
va e B
e r t i
v v r bt
ra the ee .V
he r s
T . 3 re
t t ve
1 e.
6. int
in ra
r
Pr 7. T
P
1
3. Left Subtree is NULL 8. Left Subtree is NULL 14. Left Subtree is NULL 19. Left Subtree is NULL
4. Print the Value: d 9. Print the Value: e 15. Print the Value: f 20. Print the Value: g
5. Right Subtree is NULL 10. Right Subtree is NULL 16. Right Subtree is NULL 21. Right Subtree is NULL
* e
*
d
/ c
a
b a / b * c * d +e
SCSA 1304 - Unit I School of Computing 71
Preorder Traversal
a
b c
abc
5. Print the Value: d 9. Print the Value: e 15. Print the Value: f 18. Print the Value: g
6. Left Sub Tree is NULL 10. Left Sub Tree is NULL 16. Left Sub Tree is NULL 19. Left Sub Tree is NULL
7. Right Sub Tree is NULL 11. Right Sub Tree is NULL 17. Right Sub Tree is NULL 20. Right Sub Tree is NULL
*
d
/ c
a
b + * * / a b c d e
SCSA 1304 - Unit I School of Computing 74
Postorder Traversal
b c
bca
3. Left Sub Tree is NULL 7. Left Sub Tree is NULL 13. Left Sub Tree is NULL 16. Left Sub Tree is NULL
4 Right Sub Tree is NULL 8. Right Sub Tree is NULL 14. Right Sub Tree is NULL 17. Right Sub Tree is NULL
5. Print the Value: d 9. Print the Value: e 15. Print the Value: f 18. Print the Value: g
Exercise * e
*
d
/ c
a
b a b / c * d * e +
SCSA 1304 - Unit I School of Computing 77
Level Order Traversing
• Visit the root first
• Visit the roots left child
• Visit the roots right child
• Continue the traverse at each level
• Use Queue Data Structure
b c
abc
root a
Print a b c
Print b c d e
Print c d e f g
Print d e f g
Print e f g
Print f g
Print g
20
10 25
90 70
80
80
90
70
Examples 100
60
88
91
87 89
10 15
Not a BST
Not a BST
X=
X=
X=
X=