SlideShare a Scribd company logo
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
2
 Search trees are of great importance in an
algorithm design
 It is always desirable to keep the search time of
each node in the tree minimal
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
3
 Variations in binary search trees: static and dynamic
 Ways of building trees of each type to guarantee that
the trees remain balanced
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
4
 BSTs are widely used for retrieving data from
databases, look-up tables, and storage dictionaries
 It is the most efficient search technique having time
complexity that is logarithmic in the size of the set
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
5
 These two cases lead to the following two kinds of
search trees:
 Static BST
 Dynamic BST
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
6
 Static BST is the one that is not allowed to update
its structure once it is constructed
 In other words, the static BST is an offline
algorithm, which is presumably aware of the access
sequence beforehand
Static BST
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
7
 A dynamic BST is the one that changes during the
access sequence
 We assume that the dynamic BST is an online
algorithm, which does not have prior information
about the sequence
Dynamic BST
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
8
 While compilers and assemblers are scanning a
program, each identifier must be examined to
determine if it is a keyword
 This information concerning the keywords in a
programming language is stored in a symbol table
 Symbol table is a kind of ‘keyed table’
 The keyed table stores <key, information> pairs with
no additional logical structure
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
9
 The operations performed on symbol tables are the
following:
 Insert the pairs <key, information> into the
collection
 Remove the pairs <key, information> by specifying
the key
 Search for a particular key
 Retrieve the information associated with a key
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
10
 There are two different techniques for implementing
a keyed table: symbol table and tree table
 Static Tree Tables
 Dynamic Tree Tables
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
11
 Static Tree Tables
 When symbols are known in advance and no
insertion and deletion is allowed, it is called a static
tree table
 An example of this type of table is a reserved word
table in a compiler
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
12
 To optimize a table knowing what keys are in the table and
what the probable distribution is of those that are not in the
table, we build an optimal binary search tree (OBST)
 Optimal binary search tree is a binary search tree having an
average search time of all keys optimal
 An OBST is a BST with the minimum cost
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
13
 A dynamic tree table is used when symbols are not
known in advance but are inserted as they come and
deleted if not required
 Dynamic keyed tables are those that are built on-the-
fly
 The keys have no history associated with their use
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
14
 An AVL tree is a BST where the heights of the left and
right subtrees of the root differ by utmost 1 and the
left and right subtrees are again AVL trees
 The formal definition is as follows:
 An empty tree is height-balanced if T is a non-
empty binary tree with TL and TR as its left and right
subtrees, respectively
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
15
 The balance factor of a node T, BF(T), in a binary tree is
hL − hR, where hL and hR are the heights of the left
and right subtrees of T, respectively
 For any node T in an AVL tree,
 the BF(T) is equal to −1, 0, or 1
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
16
 For example, consider the BST as shown in Fig
BF(Fri) = 0
BF(Mon) = +1
BF(Sun) = +2
 Because BF(Sun) = +2, the tree is no longer height-
balanced, and it should be restructured
Unbalanced BST
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
17
 In an AVL tree, after insertion of each node, it is
checked whether the tree is balanced or not
 If unbalanced, it is rebalanced immediately
 A node is inserted or deleted from a balanced tree,
then it may become unbalanced
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
18
 So to rebalance it, the position of some nodes can
be changed in proper sequence
 This can be achieved by performing rotations of
nodes
 Rebalancing of AVL tree is performed using one of
the four rotations:
 LL,RR,LR,RL
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
19
Balancing a tree by rotating towards right (a) Unbalanced Balanced tree
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
20
Balancing a tree by rotating towards left (a) Unbalanced tree (b)
Balanced tree
Example
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
21
Repetition Construct
Case 1: LL (Left of Left)
Consider the BST in Fig :
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
22
Case 2: RR (Right of Right)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
23
Case 3: RL (Right of Left)
Case LR for unbalanced tree due to insertion in right of left of a node
(a)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
24
 Case 3: RL (Left to right)
Case LR for unbalanced tree due to insertion in right of left of a node (a)
Scenario 1 (b)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
25
Case 3: RL (Right of Left)
Case LR for unbalanced tree due to insertion in right of left of a node
(a) Scenario 1 (b) Scenario 2 (c) Scenario 3
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
26
 Case 4: LR (Left of Right)
Case RL for unbalancing due to insertion in left of right of a node (a)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
27
Case RL for unbalancing due to insertion in left of right of a node (a)
Scenario 1 (b)
 Case 4: LR (Left of Right)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
28
Case RL for unbalancing due to insertion in left of right of a node (a)
Scenario 1
(b) Scenario 2 (c) Scenario 3
Case 4: LR (Left of Right)
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
29
 Insertions and deletions in AVL tree are performed as
in BSTs and followed by rotations to correct the
imbalances in the outcome trees
 Unbalancing of an AVL tree due to insertion is removed
in a single rotation
 However, imbalancing due to the deletion may require
multiple steps for balancing
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
30
 Figure demonstrates the deletion of a node in a given AVL
tree
(a) Original tree
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
31
(b) Delete 4
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
32
(c)Note the imbalance at node 3 implies an LL rotation around node 2
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
33
(d) Imbalance at node 5 implies a RR rotation around node 8
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
34
 Search trees are of great importance in an algorithm design.
 It is always desirable to keep the search time of each node in the tree
minimal.
 OBST maintains the average search time of all the nodes optimal.
 In an AVL tree, after insertion of each node, it is checked whether the tree is
balanced or not.
 If unbalanced, it is rebalanced immediately.
 Rebalancing of AVL tree is performed using one of the four rotations: LL, RR,
LR, RL.
 AVL trees work by insisting that all nodes of the left and right subtrees differ
in height by utmost 1, which ensures that a tree cannot get too deep.
 Compilers use hash tables to keep track of the declared variables in a source
code called as a symbol table.
 Imbalancing of an AVL tree due to insertion is removed in a single rotation.
However, Imbalancing due to the deletion may require multiple steps for
balancing.
Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil
35

More Related Content

What's hot (20)

PPTX
Binary Tree Traversal
Dhrumil Panchal
 
PPTX
Graph traversals in Data Structures
Anandhasilambarasan D
 
PPT
Graphs In Data Structure
Anuj Modi
 
PDF
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
Ramco Institute of Technology, Rajapalayam, Tamilnadu, India
 
PDF
Stack
Zaid Shabbir
 
PPTX
linked list in data structure
shameen khan
 
PPTX
Tree in data structure
Äshïsh Jäïn
 
PDF
Introduction to Pandas and Time Series Analysis [PyCon DE]
Alexander Hendorf
 
PPT
17. Trees and Graphs
Intro C# Book
 
PPTX
heap Sort Algorithm
Lemia Algmri
 
PPTX
Tree Traversal Algorithm in Data Structure
Meghaj Mallick
 
PPTX
Queue ppt
SouravKumar328
 
PPT
Top down parsing
ASHOK KUMAR REDDY
 
PPTX
Tree Traversal
Md. Israil Fakir
 
PDF
Introduction to NumPy (PyData SV 2013)
PyData
 
PPTX
Binary Heap Tree, Data Structure
Anand Ingle
 
PPT
Spanning trees
Shareb Ismaeel
 
PDF
Collections in Java Notes
Shalabh Chaudhary
 
PPTX
Asymptotic notations
Nikhil Sharma
 
PPTX
AVL Tree Data Structure
Afaq Mansoor Khan
 
Binary Tree Traversal
Dhrumil Panchal
 
Graph traversals in Data Structures
Anandhasilambarasan D
 
Graphs In Data Structure
Anuj Modi
 
AD3251-Data Structures Design-Notes-Searching-Hashing.pdf
Ramco Institute of Technology, Rajapalayam, Tamilnadu, India
 
linked list in data structure
shameen khan
 
Tree in data structure
Äshïsh Jäïn
 
Introduction to Pandas and Time Series Analysis [PyCon DE]
Alexander Hendorf
 
17. Trees and Graphs
Intro C# Book
 
heap Sort Algorithm
Lemia Algmri
 
Tree Traversal Algorithm in Data Structure
Meghaj Mallick
 
Queue ppt
SouravKumar328
 
Top down parsing
ASHOK KUMAR REDDY
 
Tree Traversal
Md. Israil Fakir
 
Introduction to NumPy (PyData SV 2013)
PyData
 
Binary Heap Tree, Data Structure
Anand Ingle
 
Spanning trees
Shareb Ismaeel
 
Collections in Java Notes
Shalabh Chaudhary
 
Asymptotic notations
Nikhil Sharma
 
AVL Tree Data Structure
Afaq Mansoor Khan
 

Viewers also liked (20)

PPTX
7. Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
6. Linked list - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
15. STL - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
14. Files - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
5. Queue - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PDF
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
PPTX
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
11. Hashing - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
Pointers in c++
Vineeta Garg
 
PPTX
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
12. Heaps - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPTX
8. Graph - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
PPT
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
PPT
Ch17
Abbott
 
PPTX
Array,lists and hashes in perl
sana mateen
 
PPTX
Lists, queues and stacks
andreeamolnar
 
7. Tree - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
6. Linked list - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
15. STL - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
14. Files - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
5. Queue - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
widespreadpromotion
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
3. Stack - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
4. Recursion - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
11. Hashing - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Pointers in c++
Vineeta Garg
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
12. Heaps - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
8. Graph - Data Structures using C++ by Varsha Patil
widespreadpromotion
 
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Ch17
Abbott
 
Array,lists and hashes in perl
sana mateen
 
Lists, queues and stacks
andreeamolnar
 
Ad

Similar to 10. Search Tree - Data Structures using C++ by Varsha Patil (20)

PPT
avl trees avl trees avl trees avl trees avl trees avl trees avl trees
yatakonakiran2
 
DOC
for sbi so Ds c c++ unix rdbms sql cn os
alisha230390
 
PPTX
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
widespreadpromotion
 
PDF
104333 sri vidhya eng notes
Krishnakumar Btech
 
PPTX
17. Trees and Tree Like Structures
Intro C# Book
 
PPTX
DSA Lec 02 - Stacks and Queues Presentation
ibad29377
 
PDF
Introduction to Data Structures .
Ashutosh Satapathy
 
PPT
chap11.ppt
AswiniJ6
 
PDF
Cal Essay
Sherry Bailey
 
PPT
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
sumitbardhan
 
PPTX
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
kavi806657
 
PPTX
Computer Science-Data Structures :Abstract DataType (ADT)
St Mary's College,Thrissur,Kerala
 
PPTX
Kid171 chap02 english version
Frank S.C. Tseng
 
PDF
Binary search tree with pre-order inorder and postorder
VarshithaKolla
 
PDF
Data Structure Basics
Shakila Mahjabin
 
PPT
Introduction to Data Structures – Abstract Data Types- Classification of Data...
kavi806657
 
PPTX
Understanding Complete Linked Lists.pptx
rishjain0910
 
PDF
Introduction to database-Normalisation
Ajit Nayak
 
PPT
Positional Data Organization and Compression in Web Inverted Indexes
Leonidas Akritidis
 
PDF
Data-Structure-original-QuantumSupply.pdf
lehal93146
 
avl trees avl trees avl trees avl trees avl trees avl trees avl trees
yatakonakiran2
 
for sbi so Ds c c++ unix rdbms sql cn os
alisha230390
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
widespreadpromotion
 
104333 sri vidhya eng notes
Krishnakumar Btech
 
17. Trees and Tree Like Structures
Intro C# Book
 
DSA Lec 02 - Stacks and Queues Presentation
ibad29377
 
Introduction to Data Structures .
Ashutosh Satapathy
 
chap11.ppt
AswiniJ6
 
Cal Essay
Sherry Bailey
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
sumitbardhan
 
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
kavi806657
 
Computer Science-Data Structures :Abstract DataType (ADT)
St Mary's College,Thrissur,Kerala
 
Kid171 chap02 english version
Frank S.C. Tseng
 
Binary search tree with pre-order inorder and postorder
VarshithaKolla
 
Data Structure Basics
Shakila Mahjabin
 
Introduction to Data Structures – Abstract Data Types- Classification of Data...
kavi806657
 
Understanding Complete Linked Lists.pptx
rishjain0910
 
Introduction to database-Normalisation
Ajit Nayak
 
Positional Data Organization and Compression in Web Inverted Indexes
Leonidas Akritidis
 
Data-Structure-original-QuantumSupply.pdf
lehal93146
 
Ad

Recently uploaded (20)

PPT
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
PPTX
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
PPTX
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
PPTX
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
PPTX
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
PDF
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
PPTX
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
PDF
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
PDF
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
PDF
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
PDF
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
PDF
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
PPTX
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
PDF
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
PDF
Research Methodology Overview Introduction
ayeshagul29594
 
PPTX
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
PDF
Development and validation of the Japanese version of the Organizational Matt...
Yoga Tokuyoshi
 
PPTX
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
PDF
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
PDF
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 
Growth of Public Expendituuure_55423.ppt
NavyaDeora
 
apidays Helsinki & North 2025 - Agentic AI: A Friend or Foe?, Merja Kajava (A...
apidays
 
apidays Helsinki & North 2025 - APIs at Scale: Designing for Alignment, Trust...
apidays
 
apidays Helsinki & North 2025 - Vero APIs - Experiences of API development in...
apidays
 
apidays Munich 2025 - Building Telco-Aware Apps with Open Gateway APIs, Subhr...
apidays
 
Using AI/ML for Space Biology Research
VICTOR MAESTRE RAMIREZ
 
ER_Model_with_Diagrams_Presentation.pptx
dharaadhvaryu1992
 
NIS2 Compliance for MSPs: Roadmap, Benefits & Cybersecurity Trends (2025 Guide)
GRC Kompas
 
OOPs with Java_unit2.pdf. sarthak bookkk
Sarthak964187
 
Data Retrieval and Preparation Business Analytics.pdf
kayserrakib80
 
apidays Helsinki & North 2025 - Monetizing AI APIs: The New API Economy, Alla...
apidays
 
Context Engineering for AI Agents, approaches, memories.pdf
Tamanna
 
Listify-Intelligent-Voice-to-Catalog-Agent.pptx
nareshkottees
 
apidays Helsinki & North 2025 - How (not) to run a Graphql Stewardship Group,...
apidays
 
Research Methodology Overview Introduction
ayeshagul29594
 
apidays Helsinki & North 2025 - API access control strategies beyond JWT bear...
apidays
 
Development and validation of the Japanese version of the Organizational Matt...
Yoga Tokuyoshi
 
SlideEgg_501298-Agentic AI.pptx agentic ai
530BYManoj
 
apidays Helsinki & North 2025 - API-Powered Journeys: Mobility in an API-Driv...
apidays
 
apidays Singapore 2025 - From API Intelligence to API Governance by Harsha Ch...
apidays
 

10. Search Tree - Data Structures using C++ by Varsha Patil

  • 1. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 1
  • 2. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 2  Search trees are of great importance in an algorithm design  It is always desirable to keep the search time of each node in the tree minimal
  • 3. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 3  Variations in binary search trees: static and dynamic  Ways of building trees of each type to guarantee that the trees remain balanced
  • 4. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 4  BSTs are widely used for retrieving data from databases, look-up tables, and storage dictionaries  It is the most efficient search technique having time complexity that is logarithmic in the size of the set
  • 5. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 5  These two cases lead to the following two kinds of search trees:  Static BST  Dynamic BST
  • 6. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 6  Static BST is the one that is not allowed to update its structure once it is constructed  In other words, the static BST is an offline algorithm, which is presumably aware of the access sequence beforehand Static BST
  • 7. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 7  A dynamic BST is the one that changes during the access sequence  We assume that the dynamic BST is an online algorithm, which does not have prior information about the sequence Dynamic BST
  • 8. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 8  While compilers and assemblers are scanning a program, each identifier must be examined to determine if it is a keyword  This information concerning the keywords in a programming language is stored in a symbol table  Symbol table is a kind of ‘keyed table’  The keyed table stores <key, information> pairs with no additional logical structure
  • 9. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 9  The operations performed on symbol tables are the following:  Insert the pairs <key, information> into the collection  Remove the pairs <key, information> by specifying the key  Search for a particular key  Retrieve the information associated with a key
  • 10. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 10  There are two different techniques for implementing a keyed table: symbol table and tree table  Static Tree Tables  Dynamic Tree Tables
  • 11. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 11  Static Tree Tables  When symbols are known in advance and no insertion and deletion is allowed, it is called a static tree table  An example of this type of table is a reserved word table in a compiler
  • 12. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 12  To optimize a table knowing what keys are in the table and what the probable distribution is of those that are not in the table, we build an optimal binary search tree (OBST)  Optimal binary search tree is a binary search tree having an average search time of all keys optimal  An OBST is a BST with the minimum cost
  • 13. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 13  A dynamic tree table is used when symbols are not known in advance but are inserted as they come and deleted if not required  Dynamic keyed tables are those that are built on-the- fly  The keys have no history associated with their use
  • 14. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 14  An AVL tree is a BST where the heights of the left and right subtrees of the root differ by utmost 1 and the left and right subtrees are again AVL trees  The formal definition is as follows:  An empty tree is height-balanced if T is a non- empty binary tree with TL and TR as its left and right subtrees, respectively
  • 15. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 15  The balance factor of a node T, BF(T), in a binary tree is hL − hR, where hL and hR are the heights of the left and right subtrees of T, respectively  For any node T in an AVL tree,  the BF(T) is equal to −1, 0, or 1
  • 16. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 16  For example, consider the BST as shown in Fig BF(Fri) = 0 BF(Mon) = +1 BF(Sun) = +2  Because BF(Sun) = +2, the tree is no longer height- balanced, and it should be restructured Unbalanced BST
  • 17. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 17  In an AVL tree, after insertion of each node, it is checked whether the tree is balanced or not  If unbalanced, it is rebalanced immediately  A node is inserted or deleted from a balanced tree, then it may become unbalanced
  • 18. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 18  So to rebalance it, the position of some nodes can be changed in proper sequence  This can be achieved by performing rotations of nodes  Rebalancing of AVL tree is performed using one of the four rotations:  LL,RR,LR,RL
  • 19. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 19 Balancing a tree by rotating towards right (a) Unbalanced Balanced tree
  • 20. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 20 Balancing a tree by rotating towards left (a) Unbalanced tree (b) Balanced tree Example
  • 21. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 21 Repetition Construct Case 1: LL (Left of Left) Consider the BST in Fig :
  • 22. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 22 Case 2: RR (Right of Right)
  • 23. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 23 Case 3: RL (Right of Left) Case LR for unbalanced tree due to insertion in right of left of a node (a)
  • 24. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 24  Case 3: RL (Left to right) Case LR for unbalanced tree due to insertion in right of left of a node (a) Scenario 1 (b)
  • 25. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 25 Case 3: RL (Right of Left) Case LR for unbalanced tree due to insertion in right of left of a node (a) Scenario 1 (b) Scenario 2 (c) Scenario 3
  • 26. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 26  Case 4: LR (Left of Right) Case RL for unbalancing due to insertion in left of right of a node (a)
  • 27. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 27 Case RL for unbalancing due to insertion in left of right of a node (a) Scenario 1 (b)  Case 4: LR (Left of Right)
  • 28. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 28 Case RL for unbalancing due to insertion in left of right of a node (a) Scenario 1 (b) Scenario 2 (c) Scenario 3 Case 4: LR (Left of Right)
  • 29. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 29  Insertions and deletions in AVL tree are performed as in BSTs and followed by rotations to correct the imbalances in the outcome trees  Unbalancing of an AVL tree due to insertion is removed in a single rotation  However, imbalancing due to the deletion may require multiple steps for balancing
  • 30. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 30  Figure demonstrates the deletion of a node in a given AVL tree (a) Original tree
  • 31. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 31 (b) Delete 4
  • 32. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 32 (c)Note the imbalance at node 3 implies an LL rotation around node 2
  • 33. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 33 (d) Imbalance at node 5 implies a RR rotation around node 8
  • 34. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 34  Search trees are of great importance in an algorithm design.  It is always desirable to keep the search time of each node in the tree minimal.  OBST maintains the average search time of all the nodes optimal.  In an AVL tree, after insertion of each node, it is checked whether the tree is balanced or not.  If unbalanced, it is rebalanced immediately.  Rebalancing of AVL tree is performed using one of the four rotations: LL, RR, LR, RL.  AVL trees work by insisting that all nodes of the left and right subtrees differ in height by utmost 1, which ensures that a tree cannot get too deep.  Compilers use hash tables to keep track of the declared variables in a source code called as a symbol table.  Imbalancing of an AVL tree due to insertion is removed in a single rotation. However, Imbalancing due to the deletion may require multiple steps for balancing.
  • 35. Oxford University Press © 2012Data Structures Using C++ by Dr Varsha Patil 35