0% found this document useful (0 votes)
38 views88 pages

ADS-Unit 1

The document discusses advanced data structures. It aims to teach non-linear data organization using trees and graphs. Specifically, it covers tree terminology like root, leaf nodes, and paths. It explains tree traversal approaches and the use of trees in applications like file systems and quick searching. Binary trees are also introduced, which restrict each node to having no more than two children. Array and linked list representations of trees are presented. The overall goal is to learn efficient non-linear data structures and their applications.

Uploaded by

Sai Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views88 pages

ADS-Unit 1

The document discusses advanced data structures. It aims to teach non-linear data organization using trees and graphs. Specifically, it covers tree terminology like root, leaf nodes, and paths. It explains tree traversal approaches and the use of trees in applications like file systems and quick searching. Binary trees are also introduced, which restrict each node to having no more than two children. Array and linked list representations of trees are presented. The overall goal is to learn efficient non-linear data structures and their applications.

Uploaded by

Sai Krishna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 88

SCSA1304

ADVANCED DATA STRUCTURES

B.E CSE 2019-2023


III SEMESTER
COURSE OBJECTIVES
➢ To acquire knowledge of organizing the data in non
linear fashion.
➢ To get the idea of balancing the height of trees to
optimize the structure and search time.
➢ To learn the process of establishing the network with
various nodes with minimum cost and finding the
shortest path.
➢ To understand the method of designing the table
data structure and its applications.
Trees
Non Linear Graphs
Data structures
Set
Tables
TREE Director

• Non Linear Data Structure

• Multiple Level
Dean
• Collection of entities or nodes
linked to gather to stimulate a
hierarchy relation

• Top – Down Approach HOD HOD

• Nodes store information

• 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 :

Tree data structure is a collection of data (Node) which is organized in hierarchical


structure recursively.

A tree ‘t’ is a finite nonempty set of elements.


One of these elements is called the root.
The remaining elements, if any, are partitioned into trees, which are called the subtrees of ‘t’.

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

Node: Store Information B Dean C

Parent Node: Immediate predecessor of any


node
D HOD E F HOD G
Child Node: Immediate Successor of node

Branch: Relationship between parent and


child. Also called as Edges H I J K L M N O

Leaf Node/ External Node: No children FACULTY


Terminology of Tree
Internal Node: Children (Non Leaf Node) Director
A
Path: Sequent of consecutive edges from source to
destination

Ancestor: Any predecessor node on the path from root


B Dean C
to that node

Descendant: Any Successor node on the path from that


node to leaf node D F
HOD E HOD G

Subtree: Subset of tree that is itself a tree (Descendant)

Sibling: All children of same parent H I J K L M N O

Cousins: Children's of different parent FACULTY

Degree: No. of Children of that node (Tree-Max)


Terminology of Tree
Director
Depth of Node: Length of path from root to that Level 0 A
node

Height of Node: No. of Edges in the longest path


from that node to leaf node Level 1
B Dean C

Level of Node: Distance from root to the given


node (No. of Edges)
Level 2 D F
HOD E HOD G
Level = Depth ->node and edges

N Nodes means N-1 Edges


Level 3 H I L M N O
J K
File system, Quick Search, Organize (Binary
FACULTY
tree, Heap tree)
Terminology of Tree
Terminology of Tree
Terminology of Tree
Terminology of Tree
Terminology of Tree
Terminology of Tree
Terminology of Tree
Terminology of Tree
BINARY TREE

• Finite collection of elements.


• A nonempty binary tree has a root element.
• The remaining elements are partitioned into
two binary trees.
• These are called the left and right subtrees of
the binary tree.
BINARY TREE Vs TREE
BINARY TREE TREE

No node in a binary tree may have a


No limit on the degree of a node in a tree
degree more than 2

A binary tree may be empty A tree cannot be empty

The subtreesaare ordered The subtrees


a
are not ordered

b b

Left & Right Binary Trees No difference. Treated in a similar manner


Representations of Tree :
1.Array Representation
2.Linked List Representation
Array Representation
• If a complete binary tree with n nodes is represented sequentially, then
for any node with index i, 1  i  n,
– PARENT(i) is at | i / 2 | if i > 1.
• If i = 1, i is at the root and has no parent.
– LEFTCHILD(i) is at 2i if 2i  n,
• if 2i > n, i has no left child
– RIGHTCHILD(i) is at 2i + 1 if 2i +1  n,
• if 2i +1 > n, i has no right child

SCSA 1304 - Unit I School of Computing 22


1
Example 1

2 3

4 5 6 7

8 9 10 11 12 13 14 15

SCSA 1304 - Unit I School of Computing 23


index tree
[0] --
[1] A
Example 1 A
[2] B
[3] C
[4] D
B C [5] E
[6] F
[7] G
[8] H
D E F G [9] I
[10] J
[11] K
[12] L
H I J K L M N O
[13] M
[14] N
SCSA 1304 - Unit I School of Computing 24
[15] O
Representation of Tree – Arrays

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

Where X indicates not a binary tree


BINARY TREES
Ex. 2:
A A
A(Node Node A -
B) STRICTLY
BINARY TREE
B C
B

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

1. An ideal way to store data in a hierarchical way.


2. Reflect structural relationships that exist in the given data set
3. Make insertion and deletion faster than list and Array pattern.
4. A flexible way of holding and moving data
5. Used to store as many nodes as possible
6. Faster than linked lists and slower than arrays when we try to
accessing elements
BINARY TREES -
TYPES
Special Binary Tree
• Skewed Tree
• Left Skewed
• Right Skewed
• Complete Binary Tree – all levels complete, except last with left nodes
• Full Binary Tree – complete at all levels (with 2 nodes)
• Perfect Binary tree – all leaves at the same level
• Rooted Binary Tree – only root is allowed to have degree 2 others <2

SCSA 1304 - Unit I School of Computing 31


STRICTLY 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

= 2^0 + 2^1 + 2^2 + 2^3 + 2^4

= 1 + 2 + 4 + 8 + 16 (GP SERIES)

EX. HEIGHT = LEVEL


WHERE H = 4

(2^(4 + 1)) – 1 = 31 NODES


PROPERTIES OF BINARY TREE
3. MINIMUM NO. OF NODES AT HEIGHT H IS N = H + 1
Height = Level LEVEL 0

LEVEL 1
Ex. 1 : Level = 0 then h = 0
Ans : 1
LEVEL 0
Ex. 2 : Level = 1 then h =1
Ans: 2 LEVEL 1

Ex. 3 : Level = 2 then h = 2


Ans: 3 LEVEL 2

Ex. 4 : Level = 3 then h = 3 LEVEL 3

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

Ex., N = 5 then H = 4 Level 1

Level 2

Level 3

Level 4
PROPERTIES OF BINARY TREE

5. Number of edges possible at n elements : E = N - 1


Ex., N = 5 Then E = 4

1
2

3 4
PROPERTIES OF BINARY TREE

6. Number of Internal Nodes (Children) in


Complete Binary Tree is N / 2

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

Height of a perfect binary tree having ’n’ nodes is -

Height of a complete binary tree having ’n’ nodes is -


BINARY TREE TRAVERSALS

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

There are three types of binary tree traversals.


1. In - Order Traversal
2. Pre - Order Traversal
3. Post - Order Traversal

Traversal Implementation:
1. Recursive Traversal Algorithms
2. Non-Recursive Traversal Algorithms
BINARY TREE TRAVERSAL

Binary tree traversal can be implemented :


Recursive Approach
Iterative Approach

Recursive Iterative

 Execute the set of instructions  Execute the set of instructions


repeatedly repeatedly

 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 )

In-Order Traversal for above example of binary tree


is :

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 USING RECURSIVE:

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

Print Temp // P Print Temp // Q Print Temp // S


T
S T
If Right(P)!= Null //Right(P) = R If Right(P)!= Null //Right(Q) = T If Right(S)!= Null //Right(S) = NULL U
Inorder(R) Inorder(T) Inorder() // Not Called
End if End if End if S -> Q -> T -> P -> U -> R
End Inorder(P) End Inorder(Q) End Inorder(S)

Inorder(T) Inorder(R) Inorder(U)


Temp = T 4 Temp = R 5 Temp = U 6
T != Null R != Null U != Null

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

Print Temp // T Print Temp // R Print Temp // U

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

End Inorder(T) End Inorder(R) End Inorder(U)


ROOT = P ROOT = Q
Step 1: Inorder (ROOT) // Inorder (P) ROOT = Q Step 10: Right(Temp) = Null
Step 2: Temp = ROOT // Temp = P Step 1: Inorder(ROOT) // Assume Q // Right(Temp) = T
Step 3,4,5: Fails Step 2: Temp = ROOT // Temp = Q Step 11: Inorder(Right (Temp))
Step 6: Left(Temp)=NULL Step 3,6: Fails, Already Visited // Inorder(T)
// Left(Temp) = Q 1 Step 9: Print(Temp) Step 1: Inorder(T)
Step 7: Inorder(Q) P OUTPUT: S Q 2 Step 2: Temp = ROOT // Temp = T
Step 1: Inorder(ROOT) // Assume Q Step 3: Fails
Step 2: Temp = Q Step 9: Print(Temp)
INORDER USING RECURSIVE: Step 6: Left (Q) = NULL P OUTPUT: S Q T 3
Step 7: Left(Temp) = NULL Q R 2
Inorder (ROOT)
// Left(Temp) = S
Step 2: Temp = ROOT
Step 1: Inorder (ROOT) // Assume S Q R
Step 3: If Temp == NULL
Step 2: Temp = S
3 P
Step 4: Return
Step 3: Fails S T U S T U
Step 5: End If
Step 9: Print(Temp) Q R
Step 6: If Left(Temp) != NULL
OUTPUT : S 1
Step 7: Inorder (Left(Temp)) S T U
Step 8: End If ROOT = P
Step 10: Right (Temp) = NULL
ROOT = R
Step 9: Print (Temp) // Right (Temp) = R
Step 1: Inorder(ROOT) // Inorder(R)
Step 11: Inorder( R)
ROOT = P Step 2: Temp = ROOT // Temp = R
Step 10 :If Right(Temp) != NULL Step 1: Inorder (ROOT) // Assume R
Step 1: Inorder(P) Step 3: Fails
Step 11: Inorder(Right(Temp)) Step 2: Temp = ROOT
Step 2: Temp = ROOT // Temp = P Step 9: Print( Temp)
Step 12: End If Step 3: Fails
Step 3: Fails OUTPUT: S Q T P U R
Step 13: End Inorder Step 6: Already Visited Step 6: Left(Temp) = NULL // U
Step 13: End 6
Step 9: Print (Temp) Step 7: Inorder(U)
OUTPUT: S Q T P
4 Step 1: Inorder(ROOT) // Assume U
Step 2: Temp = U
Step 3: fails 5 6 P
Step 9: Print (Temp) P
P OUTPUT: S Q T P U 5 Q R
Q R
4 Q R
S T U
S T U
S T U
BINARY TREE TRAVERSALS
Pre-Order Traversal (Root - Leftchild -
Rightchild)
PRE ORDER TRAVERSAL

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

8. Visit Right Sub Tree

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
BINARY TREE TRAVERSALS
Pre-Order Traversal (Root - Leftchild -
Rightchild)
+

* E

* D

/ C

+ * * / A B C D E
A B
BINARY TREE TRAVERSALS
Pre-Order Traversal (Root – Leftchild- Rightchild )

Pre-Order Traversal for above example of binary


tree is :

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

Print Temp // P Print Temp // Q Print Temp // S


Q R
If Left (P) != Null // Left(P) = Q If Left (Q) != Null // Left(Q) = S If Left (S) != Null // Left(S) = NULL
Preorder (Q) Preorder (S) Preorder () // Not Called
End if End if End if
T
S T
If Right(P)!= Null //Right(P) = R If Right(P)!= Null //Right(Q) = T If Right(S)!= Null //Right(S) = NULL U
Preorder(R) Preorder(T) Preorder() // Not Called
End if End if End if P -> Q -> S -> T -> R -> U

End Preorder(P) End Preorder(Q) End Preorder(S)

Preorder(T) Preorder(R) Preorder(U)


Temp = T 4 Temp = R 5 Temp = U 6
T != Null R != Null U != Null

Print Temp // T Print Temp // R Print Temp // U

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

End Preorder(T) End Preorder(R) End Preorder(U)


Temp = P Temp = Q
Step 7: Left(Temp) = NULL Step 7: Left(Temp) = NULL 3
// Left(Temp) = Q // Left(Temp) = S
Step 8: Preorder(Left(Temp)) Step 8: Preorder (Left(Temp))
P
// Preorder (Q) // Preorder (S)
Step 1: Preorder(ROOT) // Assume Q Step 1: Preorder(ROOT) //Assume S Q R
PREORDER USING Step 2: Temp = ROOT // Temp = Q Step 2: Temp = ROOT // Temp = S
RECURSIVE: Step 3: Fails
Step 6: Print (Temp)
Step 3: Fails S T U
Step 6: Print (Temp)
Step1: Preorder (ROOT)
OUTPUT : P Q
2 3
Step2: Temp = ROOT OUTPUT : P Q S
Step3: If Temp = = NULL
step4: Return ROOT = P
Step5: End If Step 1: Function call Temp = R
Preorder(ROOT) // Preorder P 2 P Step 7: Left(Temp) = NULL 6
Step 6: Print (Temp) (P)
1 // Left(Temp) = U
Step 2: Temp = Root // Temp Q R Q R Step 8: Preorder (Left(Temp))
Step7: If Left(Temp) != NULL =P // Preorder(U) P
Step8: Preorder (Left(Temp)) Step 3: Fails
S T U S T U Step 1: Preorder(ROOT) // Assume
Step9: End If Step 6: Print (Temp) U
1 Step 2: Temp = ROOT // Temp = U Q R
Step10: If Right(Temp) != NULL OUTPUT : P Step 3: Fails
Step11: Preorder(Right(Temp)) Step 6: Print (Temp)
Step12: End If OUTPUT: P Q S T R U
S T U
Step13: End Preorder ROOT = P , Temp = P
Step 13: End 6
Temp = Q P Step 10: Right (Temp) = NULL
Step 10: Right(Temp) = NULL // Right(Temp) = R
// Right(Temp) = T Step 11: Preorder (Right (Temp))
Step 11: Preorder(Right(Temp)) Q R // Preorder (R)
Step 1: Preorder(ROOT) // Preorder (R )
// Preorder(T)
Step 1: Preorder(ROOT) // Assume T Step 2: Temp = ROOT // Temp = R
P
Step 2: Temp = ROOT // Temp = T Step 3: Fails
Step 3: Fails S T U 4 Step 6: Print (Temp) Q R
Step 6: Print( Temp)
5 S T U
4 OUTPUT: P Q S T R
5
OUTPUT: P Q S T
BINARY TREE TRAVERSALS
Post-Order Traversal (Leftchild – Rightchild -
Root)
POST ORDER TRAVERSAL

B C
BCA

SCSA1304 68
POST ORDER TRAVERSAL
1. Visit Left Sub Tree
20. Visit
11. PrintRight
the Value: a
Sub Tree

15. Visit Right Sub Tree


2. Visit Left Sub Tree 10. Print the Value: b
12. Visit Left Sub Tree 19. Print the Value: c
6. Visit Right Sub Tree

3. Left Sub Tree is NULL


7. Left Sub Tree is NULL 13. Left Sub Tree is NULL
4 Right Sub Tree is NULL 16. Left Sub Tree is NULL
8. Right Sub Tree is NULL 14. Right Sub Tree is NULL
5. Print the Value: d 17. Right Sub Tree is NULL
9. Print the Value: e 15. Print the Value: f
18. Print the Value: g
BINARY TREE TRAVERSALS
Post-Order Traversal (Leftchild – Rightchild-
Root)
+

* E

* D

/ C

A B /C *D *E+
A B
BINARY TREE TRAVERSALS
Post-Order Traversal ( Leftchild - Rightchild - Root )

Post-Order Traversal for above example of binary


tree is :

I-J-D-F-B-K-G-H-C-A
BINARY TREE TRAVERSALS
Post-Order Traversal ( Leftchild - Rightchild - Root )

POSTORDER USING RECURSIVE:


POSTORDER(ROOT)
Temp = ROOT
If temp = NULL
Return
End if P

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

Print Temp // P Print Temp // Q Print Temp // S


S -> T -> Q -> U -> R -> P
End Postorder(P) End Postorder(Q) End Postorder(S)

Postorder(T) Postorder(R) Postorder(U)


Temp = T 4 Temp = R 5 Temp = U 6
T != Null R != Null U != Null

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

Print Temp // T Print Temp // R Print Temp // U

End Postorder(T) End Postorder(R) End Postorder(U)


ROOT = P
Step 1: Temp = ROOT // Temp = ROOT = Q 3
P
ROOT = Q P
Step 2: Fails
1 Step 8: Right(Temp) = T Step 1: Temp = Q
Step 9: POSTORDER (T) Step 2: Fails
Step 5: Left(Temp) = Q
Step 6: POSTORDER( Q)
Step 1: Temp = T Step 5: Already visited Q R
P Step 2: Fails Step 8: Already visited
Step 1: Temp = ROOT // Temp = Step 11: Print(Temp) 2 Step 11:Print(Temp)
Q OUTPUT: S T 3 S
Step 2: Fails
OUTPUT: S T Q T U
Step 5: left(Q) = S Q R
Step 6: POSTORDER(S) P
Step 1: Temp = S 2
Step 2: Fails S T U Q R
Step 5: Fails ROOT = P
POSTORDER USING Step 8: Fails Step 1: Temp = P
RECURSIVE: Step 11: Print(Temp) 1 S T U ROOT = P Step 2: Fails
POSTOREDER(ROOT) OUTPUT: S Step 1: Temp = Root Step 5: Already visited
Step 1: Temp = ROOT Step 2: Fails Step 8: Already visited
Step 2: If temp = NULL Step 8: Right(Temp) = R Step 11:Print(temp)
ROOT = P Step 9: POSTORDER (R)
Step 3: Return
Step 1: Temp = P
Step 4: End if
Step 2: Fails
4 Step 1: Temp = R OUTPUT: S T Q U R P
Step 2: Fails
Step 5: If left(temp) ≠ NULL
Step 5: Fails Step11:Print(Temp) 5 Step 2: temp = Null
Step 6:
Step 6: Right(Temp) = R P OUTPUT: S T Q U R Step 4: End
POSTORDER(left(temp))
Step 7: POSTORDER (R) Step 12:End 6
Step 1: Temp = R P
Step 7: End if
Step 2: Fails Q R
Step 5:left(temp) = U
Step 8: If right(temp) ≠ P
NULL
Step 6: POSTORDER(U) Q R
Step 9:
Step 1: Temp = U S T U 6
POSTORDER(right(temp))
Step 2: Fails
4 Fails 5 Q R
Step 10:End if
Step 5: S T U
Step 6: Fails
Step 11:Print (temp)
Step 11: Print(Temp)
Step 12:End POSTORDER
OUTPUT: S T Q U S T U
BINARY TREE TRAVERSAL

Binary tree traversal can be implemented :


Recursive Approach
Iterative Approach

Recursive Iterative

 Execute the set of instructions  Execute the set of instructions


repeatedly repeatedly

 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)

If you just observe here, postorder


traversal is just reverse of preorder
traversal (1 3 6 2 5 4 if we traverse the
right node first and then left node.)

So idea is follow the same technique as


preorder traversal and instead of printing it
push it to the another Stack so that they
will come out in reverse order (LIFO).

At the end just pop all the items from the


second Stack and print it.
BINARY TREE TRAVERSALS
Traversing a tree
Before we dive into the binary search trees, the two general ways of traversing any tree:
depth-first search and breadth-first search.
Depth-first search
Depth-first traversal is a technique in which branches of a tree will be fully explored before taking
steps back and traversing other branches/leaves. An example of a post-order depth-first traversal
is shown in the following image:
The three most common traversal orders for depth-first search are:
•Pre-order traversal: visit current node first, then children.
•In-order traversal: visit left child first, then current node, then right child.
•Post-order traversal: visit children first, then current node.
Breadth-first search
The other way of traversing a tree is breadth-first search, where the different layers of a tree will
be traversed one-by-one. This means that the root layer (containing one node) will always be
visited first, then all nodes in the next layer, etc.
In practice, this is usually done by putting nodes to visit in a queue. Using a queue guarantees
certain nodes (those less deep in the tree) will be visited/processed first.
BINARY TREE TRAVERSALS
BREADTH FIRST SEARCH

Level Order Traversal


• Visit the root first
• Visit the roots left child
• Visit the roots right child
• Continue the traverse at each level

• Employs Queue Data Structure


BINARY TREE TRAVERSALS
BREADTH FIRST SEARCH
queue

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)

A Binary Search Tree is a binary Tree.

 It may be empty.

 If it is not empty then it satisfies the following properties:


 Every element has a key and no two elements have the same key
 The keys (if any) in the left subtree are smaller than the key in the root.
 The keys (if any) in the right subtree are greater than the key in the root.
 The left and right subtrees are also binary search trees.
BINARY SEARCH TREES (BST)
20 10 25 50 75 25 35 15 65 85

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

Average Worst case


Operation
complexity complexity
Searching O(log n) O(n)
Insertion O(log n) O(n)
Deletion O(log n) O(n)
Space
O(n) O(n)
complexity

You might also like