SlideShare a Scribd company logo
1 of 14
Active Learning Assignment
Data Structures
Prepared By:-Darji meet B (160120107025)
Topic :- Balanced trees
Gandhinagar Institute of Technology
What is Balanced
Tree ?
A binary tree is balanced if for each node it
holds that the number of inner nodes in the
left subtree and the number of inner nodes in
the right subtree differ by at most 1.
Example
7
8
5
126
11
4
9
13
10
T3
T1
T4
T2
X
Y
Z
(b)
Z
X
Y
T1 T2 T4
115
4 9
8
6 12
7
1310
T3
BALANCED TREES ALGORITHM
Procedure:-
CREATE_LEAF(NAME,INFO,NEW)
1[Create a new leaf node and initialize]
NEW NODE
LPTR(NEW) RPTR(NEW) NULL
BI(NEW) ‘B’
K(NEW) NAME
DATA(NEW) INFO
2[Finished]
Return
Function BALANCED_INSEAT(HEAD, NAME,INFO)
1.[Is this a first insertion?]
If LPTR(HEAD)=HEAD
then Call CREATE_LEAF(NAME,INFO,NEW)
LPTR(HEAD) NEW
Return(NEW)
2.[Initialize]
LEVEL 0
DIRECTION(LEVEL) ‘L’
PATH[LEVEL] HEAD
T LPTR(HAND)
3.[Compare until found or inserted]
Repeat step 4 forever
4.[Compare and insert, if required]
If NAME<K(T)
then If LPTR(T)≠ NULL
then LEVEL LEVEL+1
PATH[LEVEL] T
DIRECTION[LEVEL] ‘L’
T LPTR(T)
else Call CREATE_LEAF(NAME,INFO,NEW)
LPTR(T) NEW
LEVEL LEVEL +1
PATH[LEVEL] T
DIRECTION[LEVEL] ‘L’
Exitloop
else If NAME>K(T)
then If RPTR(T)≠NULL
then LEVEL LEVEL+1
PATH[LEVEL] T
DIRECTION[LEVEL] ‘R’
T RPTR(T)
Else call CREATE_LEAF(NAME,INFO,NEW)
RPTR(T) NEW
LEVEL LEVEL+1
PATH[LEVEL] T
DIRECTION[LEVEL] ‘R’
Exitloop
else (A match; NAME=K(T))
write(‘ITEM ALREADY THERE’)
Return(NULL)
5.[search for an unbalanced node]
MARK 0
Repeat for l= LEVEL,LEVEL-1,….,1
P PATH[1]
if Bl(P) ≠ B
then MARK 1
Exitloop
6.[Adjust balance indicators]
Report for l= MARK+1,MARK+2,….LEVEL
lf NAME<K(PATH[L])
then Bl(PATH[l]) ‘L’
else Bl(PATH[l]) ‘R’
7.[Is there a critical node?]
If MARK=0
then Return(NEW)
D DIRECTION[MARK]
X PATH[MARk]
Y PATH[MARK+1]
If Bl(X)≠D
then (the node was heavy and now becomes balanced)
Bl(X) ‘B’
Return(NEW)
8.[Rebalancing: case1]
If Bl(Y)=D
then (the node was heavy and now becomes balanced)
If D=‘L’
then LPTR(X) RPTP(Y)
RPTR(Y) X
else RPTR(X) LPTR(Y)
LPTR(Y) X
Bl(X) Bl(Y) ‘B’
F PATH[MARK-1]
If X=LPTR(F)
then LPTR(F) Y
else RPTR(F) Y
Return(NEW)
9.[Rebalancing tree: case 2]
(a) (Change structure links)
If D=‘L’
then Z RPTR(Y)
RPTR(Y) LPTR(Z)
LPTR(Z) Y
LPTR(X) RPTR(Z)
RPTR(Z) X
else Z LPTR(Y)
LPTR(Y) RPTR(Z)
RPTR(Z) Y
RPTR(X) LPTR(Z)
LPTR(Z) X
F PATH[MARK-1]
If X = LPTR(F)
then LPTR(F) Z
else RPTR(F) X
(b) (Chang balance indicators)
If Bl(Z)=D
then Bl(Y) Bl(Z) ‘ B’
If D =‘L’
then Bl(X) ‘R’
else Bl(X) ‘L’
else If Bl(Z)=B
then Bl(X) Bl(Y) Bl(Z) B
else Bl(X) Bl(Z) B
Bl(Y) D
Return(NEW)
Balanced trees

More Related Content

What's hot

Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
Kumar
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
Arvind Devaraj
 
Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002
SANTOSH RATH
 

What's hot (20)

Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure
 
Data structure and algorithm All in One
Data structure and algorithm All in OneData structure and algorithm All in One
Data structure and algorithm All in One
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
 
Datastructure
DatastructureDatastructure
Datastructure
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 
L6 structure
L6 structureL6 structure
L6 structure
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures Basics
 
DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1DATASTRUCTURES UNIT-1
DATASTRUCTURES UNIT-1
 
Data structure &amp; algorithms introduction
Data structure &amp; algorithms introductionData structure &amp; algorithms introduction
Data structure &amp; algorithms introduction
 
Data structures
Data structuresData structures
Data structures
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
i-Eclat: performance enhancement of Eclat via incremental approach in frequen...
 
Data Structures (BE)
Data Structures (BE)Data Structures (BE)
Data Structures (BE)
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Data Structure
Data StructureData Structure
Data Structure
 
Data structures
Data structuresData structures
Data structures
 
Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 

Similar to Balanced trees

Ds06 linked list- insert a node after a given node
Ds06   linked list-  insert a node after a given nodeDs06   linked list-  insert a node after a given node
Ds06 linked list- insert a node after a given node
jyoti_lakhani
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
Niraj Agarwal
 

Similar to Balanced trees (9)

NEED THE ANSWER IN 30 mins thank you Write a recursive .pdf
NEED THE ANSWER IN 30 mins thank you  Write a recursive .pdfNEED THE ANSWER IN 30 mins thank you  Write a recursive .pdf
NEED THE ANSWER IN 30 mins thank you Write a recursive .pdf
 
Segment Tree
Segment TreeSegment Tree
Segment Tree
 
Segment tree
Segment treeSegment tree
Segment tree
 
E2
E2E2
E2
 
Ds06 linked list- insert a node after a given node
Ds06   linked list-  insert a node after a given nodeDs06   linked list-  insert a node after a given node
Ds06 linked list- insert a node after a given node
 
Lo27
Lo27Lo27
Lo27
 
05
0505
05
 
Fundamentals of data structures
Fundamentals of data structuresFundamentals of data structures
Fundamentals of data structures
 
Lab-2.4 101.pdf
Lab-2.4 101.pdfLab-2.4 101.pdf
Lab-2.4 101.pdf
 

Recently uploaded

Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Christo Ananth
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
dollysharma2066
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
dharasingh5698
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
MsecMca
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
amitlee9823
 

Recently uploaded (20)

ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
Call for Papers - African Journal of Biological Sciences, E-ISSN: 2663-2187, ...
 
Unit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdfUnit 1 - Soil Classification and Compaction.pdf
Unit 1 - Soil Classification and Compaction.pdf
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
Roadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and RoutesRoadmap to Membership of RICS - Pathways and Routes
Roadmap to Membership of RICS - Pathways and Routes
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
University management System project report..pdf
University management System project report..pdfUniversity management System project report..pdf
University management System project report..pdf
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01Double rodded leveling 1 pdf activity 01
Double rodded leveling 1 pdf activity 01
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 BookingVIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
VIP Call Girls Ankleshwar 7001035870 Whatsapp Number, 24/07 Booking
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
(INDIRA) Call Girl Aurangabad Call Now 8617697112 Aurangabad Escorts 24x7
 
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night StandCall Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
Call Girls In Bangalore ☎ 7737669865 🥵 Book Your One night Stand
 
Online banking management system project.pdf
Online banking management system project.pdfOnline banking management system project.pdf
Online banking management system project.pdf
 
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
(INDIRA) Call Girl Meerut Call Now 8617697112 Meerut Escorts 24x7
 
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptxBSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
BSides Seattle 2024 - Stopping Ethan Hunt From Taking Your Data.pptx
 
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...Booking open Available Pune Call Girls Pargaon  6297143586 Call Hot Indian Gi...
Booking open Available Pune Call Girls Pargaon 6297143586 Call Hot Indian Gi...
 

Balanced trees

  • 1. Active Learning Assignment Data Structures Prepared By:-Darji meet B (160120107025) Topic :- Balanced trees Gandhinagar Institute of Technology
  • 3. A binary tree is balanced if for each node it holds that the number of inner nodes in the left subtree and the number of inner nodes in the right subtree differ by at most 1.
  • 5. (b) Z X Y T1 T2 T4 115 4 9 8 6 12 7 1310 T3
  • 6. BALANCED TREES ALGORITHM Procedure:- CREATE_LEAF(NAME,INFO,NEW) 1[Create a new leaf node and initialize] NEW NODE LPTR(NEW) RPTR(NEW) NULL BI(NEW) ‘B’ K(NEW) NAME DATA(NEW) INFO 2[Finished] Return
  • 7. Function BALANCED_INSEAT(HEAD, NAME,INFO) 1.[Is this a first insertion?] If LPTR(HEAD)=HEAD then Call CREATE_LEAF(NAME,INFO,NEW) LPTR(HEAD) NEW Return(NEW) 2.[Initialize] LEVEL 0 DIRECTION(LEVEL) ‘L’ PATH[LEVEL] HEAD T LPTR(HAND) 3.[Compare until found or inserted] Repeat step 4 forever 4.[Compare and insert, if required] If NAME<K(T)
  • 8. then If LPTR(T)≠ NULL then LEVEL LEVEL+1 PATH[LEVEL] T DIRECTION[LEVEL] ‘L’ T LPTR(T) else Call CREATE_LEAF(NAME,INFO,NEW) LPTR(T) NEW LEVEL LEVEL +1 PATH[LEVEL] T DIRECTION[LEVEL] ‘L’ Exitloop else If NAME>K(T) then If RPTR(T)≠NULL then LEVEL LEVEL+1 PATH[LEVEL] T DIRECTION[LEVEL] ‘R’ T RPTR(T) Else call CREATE_LEAF(NAME,INFO,NEW) RPTR(T) NEW LEVEL LEVEL+1
  • 9. PATH[LEVEL] T DIRECTION[LEVEL] ‘R’ Exitloop else (A match; NAME=K(T)) write(‘ITEM ALREADY THERE’) Return(NULL) 5.[search for an unbalanced node] MARK 0 Repeat for l= LEVEL,LEVEL-1,….,1 P PATH[1] if Bl(P) ≠ B then MARK 1 Exitloop 6.[Adjust balance indicators] Report for l= MARK+1,MARK+2,….LEVEL lf NAME<K(PATH[L]) then Bl(PATH[l]) ‘L’ else Bl(PATH[l]) ‘R’
  • 10. 7.[Is there a critical node?] If MARK=0 then Return(NEW) D DIRECTION[MARK] X PATH[MARk] Y PATH[MARK+1] If Bl(X)≠D then (the node was heavy and now becomes balanced) Bl(X) ‘B’ Return(NEW) 8.[Rebalancing: case1] If Bl(Y)=D then (the node was heavy and now becomes balanced) If D=‘L’ then LPTR(X) RPTP(Y) RPTR(Y) X
  • 11. else RPTR(X) LPTR(Y) LPTR(Y) X Bl(X) Bl(Y) ‘B’ F PATH[MARK-1] If X=LPTR(F) then LPTR(F) Y else RPTR(F) Y Return(NEW) 9.[Rebalancing tree: case 2] (a) (Change structure links) If D=‘L’ then Z RPTR(Y) RPTR(Y) LPTR(Z) LPTR(Z) Y LPTR(X) RPTR(Z) RPTR(Z) X
  • 12. else Z LPTR(Y) LPTR(Y) RPTR(Z) RPTR(Z) Y RPTR(X) LPTR(Z) LPTR(Z) X F PATH[MARK-1] If X = LPTR(F) then LPTR(F) Z else RPTR(F) X (b) (Chang balance indicators) If Bl(Z)=D then Bl(Y) Bl(Z) ‘ B’ If D =‘L’ then Bl(X) ‘R’ else Bl(X) ‘L’
  • 13. else If Bl(Z)=B then Bl(X) Bl(Y) Bl(Z) B else Bl(X) Bl(Z) B Bl(Y) D Return(NEW)