Data Structure And Algorithms for Computer Science

ManishShukla712917 29 views 170 slides Jul 14, 2024
Slide 1
Slide 1 of 170
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33
Slide 34
34
Slide 35
35
Slide 36
36
Slide 37
37
Slide 38
38
Slide 39
39
Slide 40
40
Slide 41
41
Slide 42
42
Slide 43
43
Slide 44
44
Slide 45
45
Slide 46
46
Slide 47
47
Slide 48
48
Slide 49
49
Slide 50
50
Slide 51
51
Slide 52
52
Slide 53
53
Slide 54
54
Slide 55
55
Slide 56
56
Slide 57
57
Slide 58
58
Slide 59
59
Slide 60
60
Slide 61
61
Slide 62
62
Slide 63
63
Slide 64
64
Slide 65
65
Slide 66
66
Slide 67
67
Slide 68
68
Slide 69
69
Slide 70
70
Slide 71
71
Slide 72
72
Slide 73
73
Slide 74
74
Slide 75
75
Slide 76
76
Slide 77
77
Slide 78
78
Slide 79
79
Slide 80
80
Slide 81
81
Slide 82
82
Slide 83
83
Slide 84
84
Slide 85
85
Slide 86
86
Slide 87
87
Slide 88
88
Slide 89
89
Slide 90
90
Slide 91
91
Slide 92
92
Slide 93
93
Slide 94
94
Slide 95
95
Slide 96
96
Slide 97
97
Slide 98
98
Slide 99
99
Slide 100
100
Slide 101
101
Slide 102
102
Slide 103
103
Slide 104
104
Slide 105
105
Slide 106
106
Slide 107
107
Slide 108
108
Slide 109
109
Slide 110
110
Slide 111
111
Slide 112
112
Slide 113
113
Slide 114
114
Slide 115
115
Slide 116
116
Slide 117
117
Slide 118
118
Slide 119
119
Slide 120
120
Slide 121
121
Slide 122
122
Slide 123
123
Slide 124
124
Slide 125
125
Slide 126
126
Slide 127
127
Slide 128
128
Slide 129
129
Slide 130
130
Slide 131
131
Slide 132
132
Slide 133
133
Slide 134
134
Slide 135
135
Slide 136
136
Slide 137
137
Slide 138
138
Slide 139
139
Slide 140
140
Slide 141
141
Slide 142
142
Slide 143
143
Slide 144
144
Slide 145
145
Slide 146
146
Slide 147
147
Slide 148
148
Slide 149
149
Slide 150
150
Slide 151
151
Slide 152
152
Slide 153
153
Slide 154
154
Slide 155
155
Slide 156
156
Slide 157
157
Slide 158
158
Slide 159
159
Slide 160
160
Slide 161
161
Slide 162
162
Slide 163
163
Slide 164
164
Slide 165
165
Slide 166
166
Slide 167
167
Slide 168
168
Slide 169
169
Slide 170
170

About This Presentation

This presentation deals with data and their structures and the algorithms required to process them.


Slide Content

UNIT –3 BINARYTREES
1

SYLLABUS
Terminologies,
RepresentationofbinaryTrees:UsingArraysandlinkedlists,
BinaryTreeTraversals:RecursiveandNon-Recursive
techniques,
ThreadedBinarytrees,
Binarysearchtreeoperations:Traversal,Searching,Inserting
andDeletingelements;
AVLsearchtreeoperations:InsertionandDeletion,
m-waysearchtree,SearchingInsertionandDeletioninanm-
waysearchtree,
B-treeSearching,InsertionandDeletioninaB-tree,
HeapSort.
2

Definition of Tree
•Atreeisafinitesetofoneormorenodes
suchthat:
•Thereisaspeciallydesignatednodecalled
theroot.
•Theremainingnodesarepartitionedinton>=0disjoint
setsT1,...,Tn,whereeachofthesesetsisatree.
•WecallT1,...,Tnthesubtreesoftheroot.
3

TREE–TERMINOLOGY
Root:Thebasicnodeofallnodesinthetree.
Child:asuccessornodeconnectedtoanodeiscalledchild.(B
andCarechildnodestothenodeA,LikethatDandEarechild
nodestothenodeB.)
Parent:anodeissaidtobeparentnodetoallitschildnodes.(A
isparentnodetoB,CandBisparentnodetothenodesDandF).
Leaf:anodethathasnochildnodes.(D,EandFareLeafnodes
)
Siblings:Twonodesaresiblingsiftheyarechildrentothesame
parentnode.
Ancestor:anodewhichisparentofparentnode(Aisancestor
nodetoD,EandF).
Descendent:anodewhichischildofchildnode(D,EandFare
descendentnodesofnodeA)
Degree:Thenumberofsubtreeofanode.Abinarytreeisdegree
2.
4

TREE–TERMINOLOGY
Level:Thedistanceofanodefromtherootnode,
Therootisatlevel–0,(BandCareatLevel1andD,
E,FhaveLevel2.
Depthorheight:ofatreeisthemaximumnumber
ofnodesinabranchofT.thisturnsouttobe1more
thanthelargestlevelnumberofT.
Edge:ThelinedrawnfromanodeNofTtoa
successoriscalledanedge.
Path:Asequenceofconsecutiveedgesiscalled
path.
Aforestisadisjointunionoftrees.
5

6

EXAMPLE
7

BINARYTREE
Abinarytreeisafinitesetofelementsthatiseitheremptyoris
partitionedintothreedisjointsubsets.
-Thefirstsubsetcontainsasingleelementcalledtherootofthe
tree.
-Theothertwosubsetsarethemselvesbinarytrees,calledtheleft
andrightsubtreesoftheoriginaltree.Aleftorrightsubtreecanbe
empty.Eachelementofatreeiscalledanodeofthetree.
B
G
E
D
IH
F
c
A Binary tree
8

STRUCTURESTHATARENOTBINARYTREES
9

TYPESOFBINARYTREE
1.Strictly binary trees
2.Complete binary tree
3.Extended binary tree
10

STRICTLYBINARYTREES
•Ifeverynon-leafnodeinabinarytreemusthavetwosubtree
leftandrightsub-trees,thetreeiscalledastrictlybinarytree.
•Astrictlybinarytreewithnleavesalwayscontains2n-1
nodes.
•Here, B,E,F,G are leaf nodes. So n=4, No. of nodes=2*4-1=7
B
D
G
F
E
C
A
11

COMPLETEBINARYTREE
Thetreeissaidtobecompletebinarytreeifallitslevels,except
possiblythelast,havethemaximumnumberofpossiblenodes
andifallthenodesatlastlevelappearasfarleftaspossible.
Maximumnumberofnodesatanylevelr=2
r
Eq:-atlevel0maximumnumberofnodes=2º=1
Atlevel2maximumnumberofnodes=2²=4andsoon.
.
12

COMPLETEBINARYTREE
Supposegivenacompletebinarytreehaving26nodes.Wearelabelingthe
nodesfrom1to26.
Leftchildofnodek=2*K
Rightchildofnodek=2*K+1
ParentofnodeK=[K/2]
DepthofcompletebinarytreeT
nwithnnode
D
n=[log
2n+1]
13

FULLBINARYTREEVSCOMPLETEBINARY
TREE
14

FULLVS. COMPLETEBINARYTREE
15

EXTENDEDBINARYTREE
•Anextendedbinarytreeisatransformationofanybinarytree
intoacompletebinarytree.Thistransformationconsistsof
replacingeverynullsubtreeoftheoriginaltreewith‘special
nodes.'
•Thenodesfromtheoriginaltreearetheninternalnodes,while
the``specialnodes''areexternalnodes.
•Forinstance,considerthefollowingbinarytreeinfig(a).Fig
(b)istheextendedbinarytree.
•Emptycirclesrepresentinternalnodes,andfilledcircles
representexternalnodes.Everyinternalnodeintheextended
treehasexactlytwochildren,andeveryexternalnodeisaleaf.
Theresultisacompletebinarytree.
16

EXAMPLE
a
b
17

REPRESENTATIONOFBINARYTREESINMEMORY
.
ArrayRepresentation
LinkedRepresentation
18

ARRAYREPRESENTATION
.
•TreeNodesarestoredinthearray.
•Rootnodeisstoredatindex‘0’
•Ifanodeisatalocation‘i’,thenits
leftchildislocatedat2*i+1
rightchildislocatedat2*i+2
•Thisstorageisefficientwhenitisacompletebinarytree,
becausealotofmemoryiswasted.
19

EXAMPLE1
20

EXAMPLE2
Disadvantages
(1) waste space
(2) insertion/deletion
problem
21

LINKEDREPRESENTATION
•Ifabinarytreeisnotcomplete,abetterchoiceforstoring
itistousealinkedrepresentation.
•ItusesthreearrayINFO,RIGHT,LEFT,andapointer
variableROOTasfollows.
•Firstofall,eachnodeNoftreeTwillcorrespondtoa
locationKsuchthat:
•INFO[K]containsthedataatnodeN.
•RIGHT[K]containsthelocationoftherightchildofnode
N.
•LEFT[K]containsthelocationoftheleftchildofnodeN.
•ROOTwillcontainthelocationoftherootRofT.
22

Linked Representation
struct node {
int info;
struct node *left;
struct node *right;
};
infoleft right
info
left right
23

LINKEDREPRESENTATION
ROOT
A
B x C x
x D x E x
x F x
24

LINKEDREPRESENTATION
1
2
3
4
5
6
7
8
9
10
B
C
D
E
A 3
6
8 9
X
X
X
7
X
X
1ROOT
F
4
5
X X
2AVAIL
25

TRAVERSINGABINARYTREE
Three methods:
1. inorder
2. preorder
3. postorder
Preorder
1. Visit the root
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
Root-left-right93
4
14
18
15
20
7
16
17
5
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
Left-right-root
Inorder
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
Left-root-right
26

27

TRAVERSINGABINARYTREE
Preorder
1. Visit the root
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
preorder: ABDGCEHIF
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
postorder: GDBHIEFCA
D
B
A
F
C
E
G
IH
Inorder
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
inorder: DGBAHEICF
28

TRAVERSINGABINARYTREE
Preorder
1. Visit the root
2. Traverse the left subtree in preorder
3. Traverse the right subtree in preorder
preorder: ABCEIFJDGHKL
Inorder
1. Traverse the left subtree in inorder
2. Visit the root
3. Traverse the right subtree in inorder
inorder: EICFJBGDKHLA
Postorder
1. Traverse the left subtree in postorder
2. Traverse the right subtree in postorder
3. Visit the root
postorder: IEJFCGKLHDBA
C
B
A
L
H
D
K
G
E
I
F
J
29

APPLY EACH TRAVERSAL IN BINARY
TREE
30
PREORDER: ABDCEFG
INORDER: BDAECGF
POSTORDER: DBEGFCA

TRAVERSING
There are two solution for traversing
•Recursive
•Non-recursive
31

RECURSIVETRAVERSALOFBINARYTREE
32

INORDER
inorder (node *root)
{
if (root != NULL)
{
inorder (left[root]);
print (info[root]);
inorder (right[root]);
}
}
33

PREORDER
preorder (node *root)
{
if (root != NULL)
{
print (info[root]);
preorder (left[root]);
preorder (right[root]);
}
}
34

POSTORDER
postorder (node *root)
{
if (root != NULL)
{
postorder (left[root]);
postorder (right[root]);
print (info[root]);
}
}
35

NON-RECURSIVETRAVERSALOFBINARY
TREE
36

INORDERTRAVERSAL
1) Create an empty stack S.
2) Initialize current node as root
3) Push the current node to S and
set current =current->left until current is NULL
4) If current is NULL and stack is not empty then
a) Pop the top item from stack.
b) Print the popped item, set current = current->right
c) Go to step 3.
5) If current is NULL and stack is empty then we are done.
37

Innonrecursivetraversing,stackisusedtostorethenodeswhichhasto
bevisitedlater.
1.SetTop:=0
2.P=Root.
3.Repeatstep4to10while(P≠NULL)
4.RepeatSteps5to6while(P≠NULL)
5.Push(Stack,P)
6.P=Left[P]
[EndofStep4whileloop.]
7. IF(Stack is non empty)
8. P=Pop(Stack)
9. Print P
10. P=Right[P]
[End of If structure.]
[End of Step 3 while loop.]
11.Stop
INORDERTRAVERSAL
struct node {
int info;
struct node *left;
struct node *right;
};
38

EXAMPLE
39

PREORDERTRAVERSAL
1.P=Root.
2.Push(Stack,P)
3.Repeatstep4to9while(stackisnonempty)
4.P=Pop(Stack)
5.If(Right[P]!=Null)
6.Push(Stack,Right[P])
7.If(Left[P]!=Null)
8. Push(Stack, Left[P])
9. Print Info[P]
[End of Step 3 while loop.]
10.Stop
struct node {
int info;
struct node *left;
struct node *right;
};
40

EXAMPLE
41

POSTORDERTRAVERSAL
1.P=Root.
2.Push(Stack,Root)
3.Repeatstep4to13while(stackisnonempty)
4.P=Stack[Top]
5.If(Left[P]!=Null&&Visited[Left[P]]==FALSE)
6.Push(Stack,Left[P])
7.Else
8.If(Right[P]!=Null&&Visited[Right[P]]==FALSE)
9.Push(Stack,Right[P])
10.Else
11.Pop(Stack)
12. Print Info[P]
13.visited[P]=TRUE
[End of if]
[End of while]
14.Stop
#define BOOL int
struct node {
int info;
struct node *left;
struct node *right;
BOOL visited;
};
42

EXAMPLE
43

CREATIONOFBINARYTREEFROMPREORDER
ANDINORDERTRAVERSALS
Step1:Scanthepreordertraversalfromlefttoright.
Step2:Foreachnodescannedlocateitspositionin
inordertraversal.Letthescannednodex.
Step3:Thenodexprecedingxininorderfromitsleft
subtreeandnodessucceedingitfromrightsubtree.
Step4:Repeatstep1foreachsymbolinthe
preorder.
44

CONSTRUCTTREEFROMGIVENINORDERAND
PREORDERTRAVERSALS
Let us consider the below traversals:
Inorder sequence: D B E A F C
Preorder sequence: A B D E C F
Solution:InaPreordersequence,leftmostelementis
therootofthetree.Soweknow‘A’isrootforgiven
sequences.Bysearching‘A’inInordersequence,
wecanfindoutallelementsonleftsideof‘A’arein
leftsubtreeandelementsonrightareinright
subtree.Soweknowbelowstructurenow.
45

Werecursivelyfollowabovestepsandgetthe
followingtree.
46

Inorder sequence: CAFDGHB
Preorder sequence: DACFBGH
CONSTRUCTTREEFROMGIVENINORDERAND
PREORDERTRAVERSALS
47

A
B
C
F
G
H
SOLUTION
D
48

CONSTRUCTTHETREEFROMITSGIVEN
PREORDERANDPOSTORDER
Constructthetreefromitsgivenpreorderand
postordertraversal.Explainthestepsinvolved.
Preorder:ABDGHKCEF
Postorder:GKHDBEFCA
49

SOLUTION
Thenodeappearinthefirstpositionpreorderis
alwaystheroot.Sohere,‘A’istheroot.
A
Now,takeone-onenodefrompreorderandobserve
itspositioninpostorder.LikehereBisnextin
preorder.BisbeforeAinpostorder,soitchildofA.
A
B
50

Next node in preorder is D. D is before B in postorder.
Next node in preorder is G. G is before D in postorder.
51

Next node in preorder is H. H is before D in postorder.
But D is already having a left child. H will become right
child of D.
H
•Next node in preorder is K. K is before H in postorder.
52

NextnodeinpreorderisC.CisbeforeAin
postorder.ButAisalreadyhavingaleftchild.Cwill
becomerightchildofA.
53

NextnodeinpreorderisE.EisbeforeFin
postorder.EFisbeforeCinpostorder.
FE
54

HEADERNODES
SupposeabinarytreeTismaintainedinmemorybymeansofa
linkedrepresentation.Sometimesanextra,specialnode,calleda
headernode,isaddedtothebeginningofT.
Whenthisextranodeisused,thetreepointervariable,whichwecall
HEAD,willpointtotheheadernode,andtheleftpointerofthe
headernodewillpointtotherootofT.
55

Threaded Binary Trees
•Too many null pointers in current representation of binary trees
n: number of nodes
number of non-null links: n-1
total links: 2n
null links: 2n-(n-1)=n+1
•Thisspacemaybemoreefficientlyusedbyreplacingthenull
entriesbysomeothertypeofinformation.
•Wewillreplacenullentriesbyspecialpointerswhichpointto
nodeshigherinthetree.
•Thesespecialpointersarecalled“threads”,andbinarytrees
withsuchpointersarecalledthreadedtree.
•Thethreadsinabinarytreeareusuallyindicatedbydottedlines.
56

THREADEDBINARYTREES
Tree can be threaded using
•One way threading:
i.Right-in–threaded:athreadwillappearinthe
RIGHTNullfieldofeachnodeandwillpointto
thesuccessorofthatnodeunderinordertraversal.
ii.Left-in-threaded:athreadwillappearintheLEFT
Nullfieldofeachnodeandwillpointtothe
predecessorofthatnodeunderinordertraversal.
•Twowaythreading(fullythreadedtree):bothleftand
rightNullpointerscanbeusedtopointtopredecessor
andsuccessorofthatnodeunderinordertraversal.
57

RIGHT-IN–THREADED
Inorder:FBHGADCE
58

LEFT-IN-THREADED
Inorder:FBHGADCE
59

FULLYTHREADEDTREE
60

BINARYSEARCHTREES(BST)
AbinarysearchtreeTisabinarytreewhichis
eitheremptyoreachnodeofthetreecontainsa
keythatsatisfiesthefollowingconditions:
i.Allkeysintheleftoftherootarelessthanthekeyin
theroot.
ii.Allkeysintherightoftherootaregreaterthanthe
keyintheroot.
iii.Theleftandrightsubtreeoftherootarealsobinary
searchtrees.
61

EXAMPLE
62

EXAMPLES
20 35 48 8578
50
534512
7530
80
2012
155
10
63

ADVANTAGES& DISADVANTAGES OFBINARYSEARCHTREE
Advantages
i.Storeskeysinthenodesinawaythatsearching,insertionand
deletioncanbedoneefficiently.
ii.SimpleImplementation
iii.Nodesintreearedynamic
Disadvantages
i.Theshapeofthetreedependsontheorderofinsertions,anditcan
bedegenerated.
ii.Wheninsertingorsearchingforanelement,thekeyofeachvisited
nodehastobecomparedwiththekeyoftheelementtobe
inserted/found.
iii.Keysinthetreemaybelongandtheruntimemayincrease.
64

OPERATIONSON BINARYSEARCHTREE
•Traverse
•Searching
•Inserting
•Deleting
65

TRAVERSE
Theinorder,preorderandpostorderalgorithmswill
remainsameasbinarytree.WhenaBSTis
traversedininorder,theelementsareprintedin
ascendingorder.
Example:inorder:5,10,12,15,20
66

SEARCHING
ThesearchwillstartfromtherootX.
Kisthekeyvaluewhichwearesearchinginthe
tree.
TREE_SEARCH(X,K)
1.If(X==NULL)orK=Info[X]
returnX
2.IfK<Info[X]
returnTREE_SEARCH(Left[X],K)
3.Else
returnTREE_SEARCH(Right[X],K)
67

68

INSERTION
Theoperationsofinsertionanddeletionchangethe
dynamicsetrepresentedbyabinarysearchtree.
BeforethenodecanbeinsertedintoaBST,the
positionofthenewnodemustbedetermined.
Theinsertoperationwillinsertanodetoatreeand
thenewnodewillbecomeleafnode.
Thisistoensurethataftertheinsertion,theBST
characteristicsisstillmaintained.
69

Insertion(T, Z) //TBinary Search Tree, Znew node
1. y=NULL
2. x=root
3. While x ≠ NULL
4. do y=x
5. If info[z] < info[x]
6. then x=left[x]
7. Else x=right[x]
[End while]
8. parent[z]=y
9. If y==NULL //tree was empty
10. then root=z
11. Else if info[z] < info[y]
12. then left[y]=z
13. Else right[y]=z
14. Stop 70

CONSTRUCT A BINARY SEARCH TREE
71

CONSTRUCTBINARYSEARCHTREE
Howcanyouconstructbinarysearchtreewiththe
followingelementsequence.
40,60,50,33,55,11
72
60
33
11 50
55
40

CONSTRUCTBINARYSEARCHTREE
Howcanyouconstructbinarysearchtreewiththe
followingelementsequence.
44,22,77,33,55,99,88
73
77
22
33 55
99
44
88

DELETION
Whenanodeisdeleted,thechildrenofthe
deletednodemustbetakencareoftoensurethat
thepropertyofthesearchtreeismaintained.
Thereare3possiblecasestodeleteanodeina
tree:
Case1:Deletealeaf
Case2:Deleteanodewithonechild
a)Deleteanodewithleftchild
b)Deleteanodewithrightchild
Case3:Deleteanodethathastwochildren 74

Delete(T,x)//deleteanodewithinfoxfromatreeT.
Firstsearchanodewithinfox.letthereisnodepwith
infoxandqbeitsparent.Ifnosuchpexiststhere,
xisnotpresentsonodeletionispossible.
Case1:Letpbealeafnode
If(q==null)androot=pthensetroot=null
Else
if(left[q]==p)thenleft[q]=null
elseright[q]=null
75

Case2(a):letphaveonlyleftchild
If(q==null)thenroot=left[p]
Else
If(left[q]==p)thenleft[q]=left[p]
elseright[q]=left[p]
76

EXAMPLE
77

Case2(b):letphaveonlyrightchild
If(q==null)thenroot=right[p]
else
If(left[q]==p)thenleft[q]=right[p]
elseright[q]=right[p]
78

Case 3:p has both left and right child,
replace the node's value with the min value in the
right subtree
delete the min node in the right subtree
79

BALANCEFACTOR(BF)
Foreachnodethebalancedfactor(bf)isdefinedas
bf(N)=heightofleftsubtreeofN–heightofrightsubtreeofN
80

AVL (HEIGHT-BALANCEDTREES)
Itisnamedaftertheirinventors,Adelson,Velskiiand
Landis.
AnAVLtreeisabinarysearchtreewhichhasthe
followingproperties:
Theheightoftheleftandrightsubtreesoftheroot
differsbyatmost1
TheleftandrightsubtreesoftherootareagainAVL
trees
Nodebalancefactorof+1ifnodeleftsubtreeishigh,
0ifnodebothsubtreeareatequalhightand-1isnode
rightsubtreeishigh.
81

PERFECTLYBALANCEDBINARYTREE
82

AVL TREES
83

NON-AVL TREES
84

INSERTIONALGORITHM
Insert(T,x)
Step1:InsertxintoTusingBSTinsertionalgorithm.
Step2:Recalculatethebalancefactorforeachnode.
Step3:Backtracktowardsrootfollowingthepathof
insertion.Stopatfirstnodewhichdoesnothavea
balancefactorbelongingto(0,1,-1).Ifnosuchnode
isfound,stopandinsertionisover.Ifonesuch
nodeisfound,thengoforrotation.
85

SINGLE& DOUBLEROTATIONS
Single
LL and RR
Double
LR:LR is RR followed by LL
RL:RL is LL followed by RR
86

AVLROTATION
Inordertobalanceatree,therearefourtypesof
rotations
LLRotation:insertednodeisaddedtotheleft
subtreeoftheleftchildoftheunbalancednode.
RRRotation:insertednodeisaddedtotheright
subtreeoftherightchildoftheunbalancednode.
LRRotation:insertednodeisaddedtotheright
subtreeoftheleftchildoftheunbalancednode.
RLRotation:insertednodeisaddedtotheleft
subtreeoftherightchildoftheunbalancednode.
87

LLROTATION
88

LL ROTATIONEXAMPLE
89
Insert node 10 in this tree

RRROTATION
90

RRROTATIONEXAMPLE
91

LRROTATION
92

LRROTATIONEXAMPLE
93

RLROTATION
94

RLROTATIONEXAMPLE
95

CONSTRUCTAVLTREE
96
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
97
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
98
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
99
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
100
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
101
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
102
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
103
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

AVL TREEROTATIONS
104
Insert the following nodes in an AVL tree:
40, 30,20, 60,50, 80,15,28,25

DELETIONINANAVL TREES
Thesequenceofstepstobefollowedindeletinganode
fromanAVLtreeisasfollows:
1.Initially,theAVLtreeissearchedtofindthenodetobe
deleted.
2.TheprocedureusedtodeleteanodeinanAVLtreeis
thesameasdeletingthenodeintheBST.
3.Afterdeletionofthenode,checkthebalancefactorof
eachnode.
4.RebalancetheAVLtreeifthetreeisunbalanced.For
this,AVLrotationsareused.
105

OndeletionofanodeXfromtheAVLtree,letAbethe
closestancestornodeonthepathfromXtotheroot
node,withabalancefactorof+2or-2.Torestore
balancetherotationisfirstclassifiedasLorRdepending
onwhetherthedeletionoccurredontheleftorright
subtreeofA.
BistherootoftheleftorrightsubtreeofA.Now
dependinguponthevalueofbalancefactorofB,theR
orLrotationisfurtherclassifiedasR0,R1andR-1or
L0,L1,L-1.
106

TYPESOFROTATION
R0 (LL) L0 (RR)
R1 (LL) L1 (RR)
R-1(LR) L-1(RL)
107

R0 ROTATION
108
WhenbalancefactorofBis0andBistherootofleft
subtreeofA.R0rotationisused.(R0rotationaresimilar
toLL).

109
R0 rotation

EXAMPLER0 ROTATION
110

R1 ROTATION
111
WhenbalancefactorofBis1andBistherootofleft
subtreeofA.R1rotationisused.(R1rotationare
similartoLL).

EXAMPLER1 ROTATION
112

R(-1) ROTATION
113
WhenbalancefactorofBis-1andBistherootofleftsubtree
ofA.R(-1)rotationisused.(R(-1)rotationaresimilarto
LR).

EXAMPLEL0 ROTATION
114
WhenbalancefactorofBis0andBistherootofrightsubtree
ofA.L0rotationisused.(L0rotationaresimilartoRR).

L1 ROTATION
115
WhenbalancefactorofBis1andBistherootofright
subtreeofA.L1rotationisused.(L1rotationare
similartoRR).

116
Insertthefollowingkeysinordershowntoconstructan
AVLtree10,20,30,40,50.deletethelasttwokeysinthe
orderofLIFO.

117

118
Advantages
1.Search is O(log N) since AVL trees are always balanced.
2.Insertion and deletions are also O(logn)
3.The height balancing adds no more than a constant factor to the speed of
insertion.
Disadvantages
1.Difficult to program & debug; more space for balance factor.
2.Asymptotically faster but rebalancing costs time.
3.Most large searches are done in database systems on disk and use other
structures (e.g. B-trees).
Pros and Cons of AVL Trees

M-way search tree
Searching , Insertion and Deletion
119

DEFINITION
Amulti-waytreeoforderm(oranm-waytree)isatreeTin
which
i.Eachnodesholdbetween1tom-1distinctkeysandeach
nodehas,atmostmchildnodes.
ii.Inanode,valuesarestoredinascendingorder:K
1<K
2<
...<K
m-1
iii.Thesubtreesareplacedbetweenadjacentvalues:each
valuehasaleftandrightsubtree.
k
irightsubtree=k
i+1leftsubtree.
Allthevaluesink
ileftsubtreeare<k
i
Allthevaluesink
irightsubtreeare>k
i
iv.EachofthesubtreeA
i,1≤i≤marealsom-waysearchtrees.
120

5-WAYSEARCHTREE
121

ALGORITHMFORSEARCHINGFORA
VALUEINANM-WAYSEARCHTREE
1.IfX<K
1,recursivelysearchforXinK
1'sleft
subtree.
2.IfX>K
m-1,recursivelysearchforXinK
m-1'sright
subtree.
3.IfX=K
i,forsomei,thenwearedone(Xhas
beenfound).
4.Theonlyremainingpossibilityisthat,forsomei,
K
i<X<K
i+1.InthiscaserecursivelysearchforX
inthesubtreethatisinbetweenK
iandK
i+1
122

INSERTIONINANM-WAYSEARCHTREE
Toinsertanewelementintoanm-waysearchtree
weproceedinthesamewayasonewouldinorder
tosearchfortheelement.
Toinsert6intothe5-waysearchtree,weproceed
tosearchfor6andfindthatwefalloffthetreeat
thenode[7,12]withthefirstchildnodeshowinga
nullpointer.
Sincethenodehasonlytwokeysanda5-key
searchtreecanaccommodateupto4keysina
node,6isinsertedintothenodeas[6,7,12].
Buttoinsert146,thenode[148,151,172,186]is
alreadyfull,henceweopenanewchildnodeand
insert146intoit.
123

INSERTIONINAN5-WAYSEARCHTREE
124

DELETIONINANM-WAYSEARCHTREE
LetKbeakeytobedeletedfromthem-waysearch
tree.Todeletethekeyweproceedasonewouldto
searchforthekey.
Letthenodeaccommodatingthekeyasshowninfigure
125

DELETIONINANM-WAYSEARCHTREE
Therearefourcase
Case1:if(A
i=A
j=NULL)thendeleteK.
Case2:if(A
i≠NULL,A
j=NULL)thenchoosethelargestofthekey
elementK’inthechildnodepointedtobyA
i,deletethekeyK’and
replaceKbyK’.ObviouslydeletionofK’maycallforsubsequent
replacementsandthereforedeletionsinasimilarmanner,toenable
thekeyK’moveupthetree.
Case3:if(A
i=NULL,A
j≠NULL)thenchoosethesmallestofthe
keyelementK’’inthechildnodepointedtobyA
j,deletethekeyK’’
andreplaceKbyK’’.AgaindeletionofK’’maytriggersubsequent
replacementsanddeletionstoenableK’’moveupthetree.
Case4:if(A
i≠NULL,A
j≠NULL)thenchooseeitherthelargestof
thekeyelementK’inthechildnodepointedtobyA
i,orthesmallest
ofthekeyelementsK’’fromthesubtreepointedtobyA
j,to
replaceK.TomoveK’orK’’upthetreeitmaycallforsubsequent
replacementsanddeletions. 126

CASE1
Todelete151,wesearchfor151andobservethatintheleafnode
[148,151,172,186]whereitispresent,bothitsleftsubtreepointer
andrightsubtreepointeraresuchthat((A
i=A
j=NULL).
Wethereforesimplydelete151andthenodebecomes[148,172,
186].Deletionof92alsofollowsthesameprocess.
127

CASE2
To delete 12 (in the figure of slide 120), we find the
node [7,12] accommodates 12 and key satisfies (A
i≠
NULL, A
j = NULL). Hence we choose the largest of the
keys from the node pointed to by A
i10 and replace 12 with
10.
128

CASE3
Todelete262(inthefigureofslide120,wefinditsleftandright
subtreepointersA
iandA
jrespectively,aresuchthat(A
i=NULL,
A
j≠NULL).
Hencewechoosethesmallestelement272fromthechildnode[272,
286,300],delete272andreplace262with272.todelete272the
deletionprocedureneedstobeobservedagain.
129

CASE4
Todelete198(inthefigureofslide120,wefinditsleftandright
subtreepointersA
iandA
jrespectively,aresuchthat(A
i≠NULL,
A
j≠NULL).
Hencewechoosethelargestelement141fromthechildnode[80,
92,198],delete141andreplace198with141.inplaceof141,148
willcome.
130

B TREE
131

INTRODUCTION
M-waysearchtreeshavetheadvantagesof
minimizingfileaccessesduetotheirrestricted
height.
Howeveritisessentialthattheheightofthetreebe
keptaslowaspossibleandthereforetherearises
theneedtomaintainbalancedm-waysearchtrees.
Suchabalancedm-waysearchtreeiswhatis
definedasaB-tree.
132

DEFINITIONOFAB-TREE
AB-treeofordermisanm-waysearchtreeinwhich:
1.theroothasatleasttwochildnodesandatmostm
childnodes.
2.allinternal(non-leaf)nodesexcepttheroothaveat
leastm/2(ceil)childnodesandatmostmchild
nodes.
3.thenumberofkeysineachinternal(non-leaf)nodeis
onelessthanthenumberofitschildandthesekeys
partitionthekeysinthesubtreeofthenodeina
mannersimilartothatofm-waysearchtrees.
4.allleafnodesareonthesamelevel.
133

B-TREEOFORDER5
134

SEARCHINGINAB-TREE
Searching for a key in a B-tree is similar to one an
m-way search tree. The number of accesses
depends on the height h of the B-tree.
135

INSERTINGINTOAB-TREE
TheinsertionofakeyinaB-treeproceedsasifonewere
searchingforthekeyinthetree.Thenthekeyisinserted
accordingtothefollowingprocedure:
Case1:iftheleafnodeinwhichthekeyistobeinsertedis
notfull,thentheinsertionisdoneinthenode.Anodeis
saidtobefullifitcontainsamaximumof(m-1)keys,
giventheorderoftheB-treetobem.
Case2:ifthenodeweretobefull,theninsertthekeyin
orderintotheexistingsetofkeysinthenode,splitthe
nodeatitsmedianintotwonodesatthesamelevel,
pushingthemedianelementintheparentnodeifitisnot
full.
136

INSERTIONINTOAB-TREE
137
Consider the B-tree of order 5 shown below. Insert the elements 4,5, 58, 6 in
The order given.

AFTERINSERTION
138

139
Suppose we start with an empty B-tree and keys arrive in the
following order:1 12 8 2 25 6 14 28 17 7 52 16 48 68
3 26 29 53 55 45
We want to construct a B-tree of order 5
The first four items go into the root:
To put the fifth item in the root would violate condition
Therefore, when 25 arrives, pick the middle key to make a new
root
CONSTRUCTINGAB-TREE
12812

140
CONSTRUCTINGAB-TREE(CONTD.)
12
8
1225
6, 14, 28 get added to the leaf nodes:
12
8
12146 2528

141
CONSTRUCTINGAB-TREE(CONTD.)
Adding 17 to the right leaf node would over-fill it, so we take the
middle key, promote it (to the root) and split the leaf
817
1214 2528126
7, 52, 16, 48 get added to the leaf nodes
817
1214 2528126 16 48527

142
CONSTRUCTINGAB-TREE(CONTD.)
Adding 68 causes us to split the right most leaf, promoting 48 to the
root, and adding 3 causes us to split the left most leaf, promoting 3
to the root; 26, 29, 53, 55 then go into the leaves
381748
525355682526282912 67 121416
Adding 45 causes a split of25262829
and promoting 28 to the root then causes the root to split

143
CONSTRUCTINGAB-TREE(CONTD.)
17
38 2848
12 67 121416 525355682526 2945

144
Suppose we start with an empty B-tree and keys arrive in the
following order: 10 20 50 60 40 80 100 70 130 90 30 120 140
25 35 160 180
We want to construct a B-tree of order 5
CONSTRUCTINGAB-TREE
70
25 40 100140
2010 3035 5060 8090120130 160180

145
DELETIONFROMAB-TREE
1.Ifthekeyisalreadyinaleafnode,andremovingitdoesn’tcause
thatleafnodetohavetoofewkeys,thensimplyremovethekeyto
bedeleted.
2.Ifthekeyisnotinaleafthenitisguaranteedthatitspredecessoror
successorwillbeinaleaf--inthiscasewecandeletethekeyand
promotethepredecessororsuccessorkeytothenon-leafdeleted
key’sposition.
If(1)or(2)leadtoaleafnodecontaininglessthantheminimum
numberofkeysthenwehavetolookatthesiblingsimmediately
adjacenttotheleaf:
3.ifoneofthemhasmorethanthemin.numberofkeysthenwe
canpromoteoneofitskeystotheparentandtaketheparentkey
intoourlackingleaf
4.ifneitherofthemhasmorethanthemin.numberofkeysthenthe
lackingleafandoneofitsneighbourscanbecombinedwiththeir
sharedparent(theoppositeofpromotingakey)andthenewleaf
willhavethecorrectnumberofkeys;ifthisstepleavetheparent
withtoofewkeysthenwerepeattheprocessuptotherootitself,
ifrequired

146
CASE#1: SIMPLELEAFDELETION
122952
2791522 5669723143
Delete 2: Since there are enough
keys in the node, just delete it
Assuming a 5-way
B-Tree, as before...

147
CASE#2: SIMPLENON-LEAFDELETION
122952
791522 5669723143
Delete 52
Borrow the predecessor
or (in this case) successor
56

148
CASE#4: TOOFEWKEYSINNODEANDITS
SIBLINGS
122956
791522 69723143
Delete 72
Too few keys!
Join back together

149
CASE#4: TOOFEWKEYSINNODEANDITS
SIBLINGS
1229
791522 69563143

150
CASE#3: ENOUGHSIBLINGS
1229
791522 69563143
Delete 22
Demote root key and
promote leaf key

151
CASE#3: ENOUGHSIBLINGS
12
297915
31
695643

152
Consider the B-tree of order 5 shown below. Delete the keys 95, 226 , 221
and 70.

DELETIONINAB-TREE
Thedeletionofkey95issimpleandstraightsincetheleafnodehas
morethantheminimumnumberofelements.
Todelete226,theinternalnodehasonlytheminimumnumberof
elementsandhenceborrowstheimmediatesuccessorviz.,300from
theleafnodewhichhasmorethanthenumberofelements.
Deletionof221callsforthehaulingofkey440totheparentnode
andpullingdownof300totaketheplaceofthedeletedentryinthe
leaf.
Lastlythedeletionof70isalittlemoreinvolvedinprocess.Since
noneoftheadjacentleafnodescanaffordlendingakey,twoofthe
leafnodesarecombinedwiththeinterveningelementfromthe
parenttoformanewleafnode,viz,[32,44,85,81]leaving86alone
intheparentnode.
Thisisnotpossiblesincetheparentnodeisnowrunninglowonits
minimumnumberofelements.Henceweonceagainproceedto
combinetheadjacentsiblingnodesofthisyieldsthe
node[86,110,120,440]whichisthenewroot.

153

EXAMPLE
154
OntheB-treeoforder3givenbelow,performthe
followingoperationsintheorderoftheir
appearance:
Insert75,57delete35,65

SOLUTION
155

ALGORITHMTOFINDTHENO. OFLEAFNODES
INTHEBINARYTREE
get_leaf_node(node)
1. If node==Null then
Return 0
2. If left[node]=Null && right[node]==Null then
Return 1
3. Else
Return get_leaf_node(left[node])+
get_leaf_node(right[node])
156

HEAPSORT
157

A DATASTRUCTUREHEAP
A heapis an array object that can be viewed as a
nearly complete binary tree.
35
2 4
1
6
6 5 3 24 1
1
2
3
4 5 6
158

159
ARRAYREPRESENTATION OFHEAPS
A heap can be stored as an array
A.
Root of tree is A[1]
Left child of A[i] = A[2i]
Right child of A[i] = A[2i + 1]
Parent of A[i] = A[ i/2]

160
HEAPTYPES
Max-heaps(largestelementatroot),havethemax-heap
property:
for all nodes i, excluding the root:
A[PARENT(i)] ≥ A[i]
Min-heaps(smallestelementatroot),havethemin-heap
property:
for all nodes i, excluding the root:
A[PARENT(i)] ≤ A[i]

161
ADDING/DELETINGNODES
New nodes are always inserted at the bottom level (left to right)
Nodes are removed from the bottom level (right to left)

162
OPERATIONSONHEAPS
Maintain/Restore the max-heap property
MAX-HEAPIFY
Create a max-heap from an unordered array
BUILD-MAX-HEAP
Sort an array in place
HEAPSORT

163
MAINTAININGTHEHEAPPROPERTY
Supposeanodeissmallerthanachild
LeftandRightsubtreesofiaremax-heaps
Toeliminatetheviolation:
Exchangewithlargerchild
Movedownthetree
Continueuntilnodeisnotsmallerthanchildren

164
EXAMPLE
MAX-HEAPIFY(A, 2, 10)
A[2] violates the heap property
A[2] A[4]
A[4] violates the heap property
A[4] A[9]
Heap property restored

165
MAINTAININGTHEHEAPPROPERTY
Assumptions:
Left and Right
subtrees of i are
max-heaps
A[i] may be
smaller than its
children
Alg:MAX-HEAPIFY(A, i, n)
1.l ← LEFT(i)
2.r ← RIGHT(i)
3.ifl ≤ n and A[l] > A[i]
4.thenlargest ←l
5.elselargest ←i
6.ifr ≤ n and A[r] > A[largest]
7.thenlargest ←r
8.iflargest i
9.thenexchange A[i] ↔ A[largest]
10. MAX-HEAPIFY(A, largest, n)

166
BUILDINGAHEAP
Alg:BUILD-MAX-HEAP(A)
1.n= length[A]
2.fori ←n/2downto1
3. doMAX-HEAPIFY(A, i, n)
Convert an array A[1 … n] into a max-heap (n = length[A])
The elements in the subarray A[(n/2+1) .. n] are leaves
Apply MAX-HEAPIFY on elements between 1 and n/2
2
14 8
1
16
7
4
3
9 10
1
2 3
4 5 6 7
8 9 10
4132169101487A:

167
EXAMPLE: A
4132169101487
2
14 8
1
16
7
4
3
9 10
1
2 3
4 5 6 7
8 9 10
14
2 8
1
16
7
4
10
9 3
1
2 3
4 5 6 7
8 9 10
2
14 8
1
16
7
4
3
9 10
1
2 3
4 5 6 7
8 9 10
14
2 8
1
16
7
4
3
9 10
1
2 3
4 5 6 7
8 9 10
14
2 8
16
7
1
4
10
9 3
1
2 3
4 5 6 7
8 9 10
8
2 4
14
7
1
16
10
9 3
1
2 3
4 5 6 7
8 9 10
i= 5 i= 4 i= 3
i= 2 i= 1

168
HEAPSORT
Goal:
Sort an array using heap representations
Idea:
Build a max-heapfrom the array
Swap the root (the maximum element) with the last element
in the array
“Discard” this last node by decreasing the heap size
Call MAX-HEAPIFY on the new root
Repeat this process until only one node remains

169
EXAMPLE: A=[7, 4, 3, 1, 2]
MAX-HEAPIFY(A, 1, 4) MAX-HEAPIFY(A, 1, 3) MAX-HEAPIFY(A, 1, 2)
MAX-HEAPIFY(A, 1, 1)

170
ALG: HEAPSORT(A)
1.BUILD-MAX-HEAP(A)
2.for i ←length[A]downto 2
3. do exchange A[1] ↔A[i]
4. MAX-HEAPIFY(A, 1, i -1)