ADS-Unit 1
ADS-Unit 1
• Multiple Level
Dean
• Collection of entities or nodes
linked to gather to stimulate a
hierarchy relation
• Unidirectional
FACULTY
Tree - Terminology
In linear data structure data is organized in sequential order and in non-linear data structure data is
organized in random order.
A tree is a very popular non-linear data structure used in a wide range of applications. A tree data
structure can be defined as follows :
In tree data structure, every individual element is called as Node. Node in a tree data
structure stores the actual data of that particular element and link to next element in
Hierarchical structure.
In a tree data structure, if we have N number of nodes then we can have a maximum of
N-1 number of links.
TREES
Top Down Approach
Director
Dean
HOD HOD
FACULTY
Terminology of Tree Director
A
Root: No Parent, Top Most Element or First
Element in Hierarchy
b b
2 3
4 5 6 7
8 9 10 11 12 13 14 15
11 12 13
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Representation of Tree – Linked List
BINARY TREE A A
STRICTLY
BINARY
B C B TREE
It is a tree in each node having at most of
two children (0,1,2)
Representation:
SKEWED
BINARY
A
TREE
A
A(L) Node A(R) B
X Node A(R) D
A A
Ex. 1: X X
A(Node A A(Node
B) C) B C D B C D
Ex. 3:
A(Node Node A -
B)
SKEWED
BINARY TREE
A
A
A(Node Node B B
D)
D
BINARY TREES
Benefits of Binary Trees
Strictly binary tree is also called as Full Binary Tree or Proper Binary Tree or 2-Tree
A binary tree in which every node has either two or zero number of children is called Strictly Binary
Tree
PERFECT BINARY
TREES
Perfect binary trees are binary trees whose leaves are present at the same level and whose internal nodes carry two children.
COMPLETE BINARY TREES
A binary tree in which every internal node has exactly two children and all leaf nodes are at same level is
called Complete Binary Tree.
LEFT SKEWED TREE
SCSA1304 35
RIGHT SKEWED TREE
A
SCSA1304 36
PROPERTIES OF BINARY TREE
1. Maximum No. of nodes possible at any level is N = [2 power (i)]
i.e (2^i)
PROPERTIES OF BINARY TREE
2. Maximum No. of Nodes at height h is N = ((2^(h+1)) -1)
=0+1+2+3+4
= 1 + 2 + 4 + 8 + 16 (GP SERIES)
LEVEL 1
Ex. 1 : Level = 0 then h = 0
Ans : 1
LEVEL 0
Ex. 2 : Level = 1 then h =1
Ans: 2 LEVEL 1
Ans: 4
PROPERTIES OF BINARY TREE
4. Maximum Height of N nodes:
(Min. No. of Nodes in Height H) (N = h + 1) is H = N – 1
Level 0
Level 2
Level 3
Level 4
PROPERTIES OF BINARY TREE
1
2
3 4
PROPERTIES OF BINARY TREE
1 2
Nodes = 5
3 4
So 5/2=2.5
Round to 2
Internal Nodes = 2
PROPERTIES OF BINARY TREE
7. Number of Leaf Node (No children) in a perfect Binary
tree is L = (N + 1) / 2
1
2
N=5
3 L = (5 + 1) / 2
4 L=3
PROPERTIES OF BINARY TREE
8. Minimum Height of N nodes:
(Max. No. of Nodes in Height H) is N = ((2^(h+1)) -1)
THEN H= ?????
Level 0
Level 1
Level 2
Level 3
Level 4
PROPERTIES OF BINARY TREE
Level 0
Level 1
If no of nodes =5
Log (5+1) to base 2 =2.58
So take upper bound =3
Level 2
So height = 3-1
Therefore =2
60 70 80 90 100 120 75 50 85
BINARY TREE - ADT
Class BinaryTree
{
Objects
A finite set of nodes either empty or consisting of a root
node, left BinaryTree and right BinaryTree
Methods
BinaryTree() // Empty Binary Tree
isEmpty() // Is Binary Tree empty?
leftSubTree() // returns the left subtree
rightSubTree() // returns the right subtree
rootData() // returns data in the root node
}
BINARY TREE TRAVERSALS
Displaying (or) visiting order of nodes in a binary
tree is called as Binary Tree Traversal.
1. Visit each node in the tree exactly once.
2. When a node is visited, an operation is performed on it.
3. A full traversal produces a linear order for the nodes in a tree
Traversal Implementation:
1. Recursive Traversal Algorithms
2. Non-Recursive Traversal Algorithms
BINARY TREE TRAVERSAL
Recursive Iterative
It is a method call where the method Loop is repeated until the certain
being called is the same as one making condition met
call (Function call itself)
BINARY TREE TRAVERSALS
In-Order Traversal (Leftchild - Root -
Rightchild )
IN ORDER TRAVERSAL
B C
BAC
SCSA1304 52
IN ORDER TRAVERSAL
12.
e Vis
btre it Ri g
t Su ht S
Lef u
. Visit btre
e
1
v er se
1 . Tra
1 k. e 18
a c
B th .V
i nt a
e
r is
e
P
ue :
re
tr e
it
7. ubt
bt val R
S
Vi ree
ub
Su
ig
sit .
ht
tS
t
ef
Su
Ri
ef
tL
gh
bt
tL
r se
i si
re
i si
e
rs
V
v e.
Tra
.V
Pr ck ave
2.
17. k.
13
Ba Tr
e: e
va int .
lu th Bac t the
6.
b
n
Pri e: c
u
val
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
BINARY TREE TRAVERSALS
In-Order Traversal (Leftchild - Root -
Rightchild )
I-D-J-B-F-A-G-K-C-H
BINARY TREE TRAVERSALS
In-Order Traversal (Leftchild - Root -
Rightchild )
+
* E
* D
/ C
A / B * C* D +E
A B
BINARY TREE TRAVERSAL USING RECURSIVE APPROACH
Inorder (ROOT)
Temp = ROOT P
If Temp = = NULL
Return
Q R
End If
If Left(Temp) != NULL
Inorder (Left(Temp)) S T TU
End If
Print (Temp)
S -> Q -> T -> P -> U -> R
If Right(Temp) != NULL
Inorder(Right(Temp))
End If
End Inorder
Inorder(P) Inorder(Q) Inorder(S)
Temp = P 1 Temp = Q 2 Temp = S 3 P
P != Null Q != Null S != Null
If Left (P) != Null // Left(P) = Q If Left (Q) != Null // Left(Q) = S If Left (S) != Null // Left(S) = NULL
Inorder (Q) Inorder (S) Inorder () // Not Called Q R
End if End if End if
If Left (T) != Null // Left(T) = NULL If Left (R) != Null // Left(R) = U If Left (U) != Null // Left(U) = NULL
Inorder () // Not Called Inorder (U) Inorder () // Not Called
End if End if End if
If Right(T)!= Null //Right(T) = NULL If Right(R)!= Null //Right(R) = NULL If Right(U)!= Null //Right(U) = NULL
Inorder() // Not Called Inorder() // Not Called Inorder() // Not Called
End if End if End if
B C
ABC
SCSA1304 60
PRE ORDER TRAVERSAL
1. Print the Value: a
2. Visit Left Sub Tree 12. Visit Right Sub Tree
3. Print the Value: b 13. Print the Value: c 15. Visit Right Sub Tree
4. Visit Left Sub Tree 14. Visit Left Sub Tree
* E
* D
/ C
+ * * / A B C D E
A B
BINARY TREE TRAVERSALS
Pre-Order Traversal (Root – Leftchild- Rightchild )
A-B-D-I-J-F-C-G-K-H
BINARY TREE TRAVERSALS
Pre-Order Traversal (Root - Leftchild -
Rightchild ) RECURSIVE:
PREORDER USING
Preorder (ROOT) P
Temp = ROOT
If Temp = = NULL
Q R
Return
End If
Print (Temp) S T TU
If Left(Temp) != NULL
Preorder (Left(Temp)) P -> Q -> S -> T -> R -> U
End If
If Right(Temp) != NULL
Preorder(Right(Temp))
End If
End Preorder
Preorder(P) Preorder(Q) Preorder(S)
Temp = P 1 Temp = Q 2 Temp = S 3 P
P != Null Q != Null S != Null
If Left (T) != Null // Left(T) = NULL If Left (R) != Null // Left(R) = U If Left (U) != Null // Left(U) = NULL
Preorder () // Not Called Preorder (U) Preorder () // Not Called
End if End if End if
If Right(T)!= Null //Right(T) = NULL If Right(R)!= Null //Right(R) = NULL If Right(U)!= Null //Right(U) = NULL
Preorder() // Not Called Preorder() // Not Called Preorder() // Not Called
End if End if End if
B C
BCA
SCSA1304 68
POST ORDER TRAVERSAL
1. Visit Left Sub Tree
20. Visit
11. PrintRight
the Value: a
Sub Tree
* E
* D
/ C
A B /C *D *E+
A B
BINARY TREE TRAVERSALS
Post-Order Traversal ( Leftchild - Rightchild - Root )
I-J-D-F-B-K-G-H-C-A
BINARY TREE TRAVERSALS
Post-Order Traversal ( Leftchild - Rightchild - Root )
If left(temp) ≠ NULL
POSTORDER(left(temp)) Q R
End if
If right(temp) ≠ NULL S T TU
POSTORDER(right(temp))
End if
Print (temp)
S -> T -> Q -> U -> R -> P
End POSTORDER
Postorder(P) Postorder(Q) Postorder(S)
Temp = P 1 Temp = Q 2 Temp = S 3 P
P != Null Q != Null S != Null
If Left (P) != Null // Left(P) = Q If Left (Q) != Null // Left(Q) = S If Left (S) != Null // Left(S) = NULL
Postorder (Q) Postorder(S) Postorder() // Not Called Q R
End if End if End if
If Right(P)!= Null //Right(P) = R If Right(P)!= Null //Right(Q) = T If Right(S)!= Null //Right(S) = NULL
T
Postorder(R) Postorder(T) Postorder() // Not Called S T
End if End if End if U
If Left (T) != Null // Left(T) = NULL If Left (R) != Null // Left(R) = U If Left (U) != Null // Left(U) = NULL
Postorder () // Not Called Postorder (U) Postorder () // Not Called
End if End if End if
If Right(T)!= Null //Right(T) = NULL If Right(R)!= Null //Right(R) = NULL If Right(U)!= Null //Right(U) = NULL
Postorder() // Not Called Postorder() // Not Called Postorder() // Not Called
End if End if End if
Recursive Iterative
It is a method call where the method Loop is repeated until the certain
being called is the same as one making condition met
call (Function call itself)
BINARY TREE TRAVERSALS
In-Order Traversal ( Leftchild – Root - Rightchild )
BINARY TREE TRAVERSALS
In-Order Traversal (Leftchild – Root - Rightchild)
Psuedo code:
BINARY TREE TRAVERSALS
Pre-Order Traversal (Leftchild – Root - Rightchild)
BINARY TREE TRAVERSALS
Post-Order Traversal (Leftchild – Root - Rightchild)
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
BINARY SEARCH TREES (BST)
It may be empty.
20
10 25
BINARY SEARCH TREES (BST)
100 90 80 70 60 60 70 80 90 100
60
100
70
90
80
80
90
70
100
60
BINARY SEARCH TREES (BST)
92 93 90 91 88 89 87
92
90
93
88
91
87 89
BINARY SEARCH TREE INSERTION
BINARY SEARCH TREE