SlideShare a Scribd company logo
1 of 35
Download to read offline
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

UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES Kathirvel Ayyaswamy
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of treeRacksaviR
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Dm from databases perspective u 1
Dm from databases perspective u 1Dm from databases perspective u 1
Dm from databases perspective u 1sakthyvel3
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Microprogram Control
Microprogram Control Microprogram Control
Microprogram Control Anuj Modi
 
17. Recovery System in DBMS
17. Recovery System in DBMS17. Recovery System in DBMS
17. Recovery System in DBMSkoolkampus
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure Meghaj Mallick
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptxAnusha sivakumar
 
Bsc cs ii-dbms- u-ii-database system concepts and architecture
Bsc cs ii-dbms- u-ii-database system concepts and architectureBsc cs ii-dbms- u-ii-database system concepts and architecture
Bsc cs ii-dbms- u-ii-database system concepts and architectureRai University
 
3.2 partitioning methods
3.2 partitioning methods3.2 partitioning methods
3.2 partitioning methodsKrish_ver2
 

What's hot (20)

joins in database
 joins in database joins in database
joins in database
 
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES	UNIT II 	LINEAR DATA STRUCTURES – STACKS, QUEUES
UNIT II LINEAR DATA STRUCTURES – STACKS, QUEUES
 
Paging and segmentation
Paging and segmentationPaging and segmentation
Paging and segmentation
 
Terminology of tree
Terminology of treeTerminology of tree
Terminology of tree
 
Binary search tree(bst)
Binary search tree(bst)Binary search tree(bst)
Binary search tree(bst)
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Dm from databases perspective u 1
Dm from databases perspective u 1Dm from databases perspective u 1
Dm from databases perspective u 1
 
Array data structure
Array data structureArray data structure
Array data structure
 
Microprogram Control
Microprogram Control Microprogram Control
Microprogram Control
 
17. Recovery System in DBMS
17. Recovery System in DBMS17. Recovery System in DBMS
17. Recovery System in DBMS
 
Dbms lab Manual
Dbms lab ManualDbms lab Manual
Dbms lab Manual
 
Linked list
Linked listLinked list
Linked list
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Hashing In Data Structure
Hashing In Data Structure Hashing In Data Structure
Hashing In Data Structure
 
database language ppt.pptx
database language ppt.pptxdatabase language ppt.pptx
database language ppt.pptx
 
Bsc cs ii-dbms- u-ii-database system concepts and architecture
Bsc cs ii-dbms- u-ii-database system concepts and architectureBsc cs ii-dbms- u-ii-database system concepts and architecture
Bsc cs ii-dbms- u-ii-database system concepts and architecture
 
Micro program example
Micro program exampleMicro program example
Micro program example
 
3.2 partitioning methods
3.2 partitioning methods3.2 partitioning methods
3.2 partitioning methods
 
Finite Automata
Finite AutomataFinite Automata
Finite Automata
 
Predictive parser
Predictive parserPredictive parser
Predictive parser
 

Viewers also liked

7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patilwidespreadpromotion
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patilwidespreadpromotion
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingEduardo Bergavera
 
Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perlsana mateen
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacksandreeamolnar
 

Viewers also liked (20)

7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil7. Tree - Data Structures using C++ by Varsha Patil
7. Tree - Data Structures using C++ by Varsha Patil
 
6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil6. Linked list - Data Structures using C++ by Varsha Patil
6. Linked list - Data Structures using C++ by Varsha Patil
 
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil9. Searching & Sorting - Data Structures using C++ by Varsha Patil
9. Searching & Sorting - Data Structures using C++ by Varsha Patil
 
15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil15. STL - Data Structures using C++ by Varsha Patil
15. STL - Data Structures using C++ by Varsha Patil
 
14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil14. Files - Data Structures using C++ by Varsha Patil
14. Files - Data Structures using C++ by Varsha Patil
 
5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil5. Queue - Data Structures using C++ by Varsha Patil
5. Queue - Data Structures using C++ by Varsha Patil
 
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. PatilDiscrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
Discrete Mathematics S. Lipschutz, M. Lipson And V. H. Patil
 
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil13. Indexing MTrees - Data Structures using C++ by Varsha Patil
13. Indexing MTrees - Data Structures using C++ by Varsha Patil
 
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
16. Algo analysis & Design - Data Structures using C++ by Varsha Patil
 
3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil3. Stack - Data Structures using C++ by Varsha Patil
3. Stack - Data Structures using C++ by Varsha Patil
 
4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil4. Recursion - Data Structures using C++ by Varsha Patil
4. Recursion - Data Structures using C++ by Varsha Patil
 
11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil11. Hashing - Data Structures using C++ by Varsha Patil
11. Hashing - Data Structures using C++ by Varsha Patil
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil1. Fundamental Concept - Data Structures using C++ by Varsha Patil
1. Fundamental Concept - Data Structures using C++ by Varsha Patil
 
12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil12. Heaps - Data Structures using C++ by Varsha Patil
12. Heaps - Data Structures using C++ by Varsha Patil
 
8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil8. Graph - Data Structures using C++ by Varsha Patil
8. Graph - Data Structures using C++ by Varsha Patil
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
 
Ch17
Ch17Ch17
Ch17
 
Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perl
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
 

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

for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn osalisha230390
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...widespreadpromotion
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like StructuresIntro C# Book
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .Ashutosh Satapathy
 
chap11.ppt
chap11.pptchap11.ppt
chap11.pptAswiniJ6
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4sumitbardhan
 
Kid171 chap02 english version
Kid171 chap02 english versionKid171 chap02 english version
Kid171 chap02 english versionFrank S.C. Tseng
 
Introduction to database-Normalisation
Introduction to database-NormalisationIntroduction to database-Normalisation
Introduction to database-NormalisationAjit Nayak
 
Positional Data Organization and Compression in Web Inverted Indexes
Positional Data Organization and Compression in Web Inverted IndexesPositional Data Organization and Compression in Web Inverted Indexes
Positional Data Organization and Compression in Web Inverted IndexesLeonidas Akritidis
 
Data-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdfData-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdflehal93146
 
datastructure-201021140600.pptx
datastructure-201021140600.pptxdatastructure-201021140600.pptx
datastructure-201021140600.pptxZISAN5
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structurefaran nawaz
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data StructureJazz Jinia Bhowmik
 

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

for sbi so Ds c c++ unix rdbms sql cn os
for sbi so   Ds c c++ unix rdbms sql cn osfor sbi so   Ds c c++ unix rdbms sql cn os
for sbi so Ds c c++ unix rdbms sql cn os
 
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
2. Linear Data Structure Using Arrays - Data Structures using C++ by Varsha P...
 
104333 sri vidhya eng notes
104333 sri vidhya eng notes104333 sri vidhya eng notes
104333 sri vidhya eng notes
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .
 
chap11.ppt
chap11.pptchap11.ppt
chap11.ppt
 
Cal Essay
Cal EssayCal Essay
Cal Essay
 
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
358 33 powerpoint-slides_4-introduction-data-structures_chapter-4
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
Kid171 chap02 english version
Kid171 chap02 english versionKid171 chap02 english version
Kid171 chap02 english version
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 
Introduction to database-Normalisation
Introduction to database-NormalisationIntroduction to database-Normalisation
Introduction to database-Normalisation
 
Positional Data Organization and Compression in Web Inverted Indexes
Positional Data Organization and Compression in Web Inverted IndexesPositional Data Organization and Compression in Web Inverted Indexes
Positional Data Organization and Compression in Web Inverted Indexes
 
Data-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdfData-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdf
 
datastructure-201021140600.pptx
datastructure-201021140600.pptxdatastructure-201021140600.pptx
datastructure-201021140600.pptx
 
Trees
TreesTrees
Trees
 
Stacks in algorithems & data structure
Stacks in algorithems & data structureStacks in algorithems & data structure
Stacks in algorithems & data structure
 
Data Structures
Data StructuresData Structures
Data Structures
 
unit06-3-Trees.pdf
unit06-3-Trees.pdfunit06-3-Trees.pdf
unit06-3-Trees.pdf
 
Introduction to Data Structure
Introduction to Data StructureIntroduction to Data Structure
Introduction to Data Structure
 

Recently uploaded

CI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual interventionCI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual interventionajayrajaganeshkayala
 
How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?sonikadigital1
 
AI for Sustainable Development Goals (SDGs)
AI for Sustainable Development Goals (SDGs)AI for Sustainable Development Goals (SDGs)
AI for Sustainable Development Goals (SDGs)Data & Analytics Magazin
 
SFBA Splunk Usergroup meeting March 13, 2024
SFBA Splunk Usergroup meeting March 13, 2024SFBA Splunk Usergroup meeting March 13, 2024
SFBA Splunk Usergroup meeting March 13, 2024Becky Burwell
 
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024Guido X Jansen
 
Strategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for ClarityStrategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for ClarityAggregage
 
5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best Practices5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best PracticesDataArchiva
 
The Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayerThe Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayerPavel Šabatka
 
Master's Thesis - Data Science - Presentation
Master's Thesis - Data Science - PresentationMaster's Thesis - Data Science - Presentation
Master's Thesis - Data Science - PresentationGiorgio Carbone
 
Mapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptxMapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptxVenkatasubramani13
 
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptxTINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptxDwiAyuSitiHartinah
 
ChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics InfrastructureChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics Infrastructuresonikadigital1
 
Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023Vladislav Solodkiy
 
Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...PrithaVashisht1
 
YourView Panel Book.pptx YourView Panel Book.
YourView Panel Book.pptx YourView Panel Book.YourView Panel Book.pptx YourView Panel Book.
YourView Panel Book.pptx YourView Panel Book.JasonViviers2
 
MEASURES OF DISPERSION I BSc Botany .ppt
MEASURES OF DISPERSION I BSc Botany .pptMEASURES OF DISPERSION I BSc Botany .ppt
MEASURES OF DISPERSION I BSc Botany .pptaigil2
 
Virtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product IntroductionVirtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product Introductionsanjaymuralee1
 

Recently uploaded (17)

CI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual interventionCI, CD -Tools to integrate without manual intervention
CI, CD -Tools to integrate without manual intervention
 
How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?How is Real-Time Analytics Different from Traditional OLAP?
How is Real-Time Analytics Different from Traditional OLAP?
 
AI for Sustainable Development Goals (SDGs)
AI for Sustainable Development Goals (SDGs)AI for Sustainable Development Goals (SDGs)
AI for Sustainable Development Goals (SDGs)
 
SFBA Splunk Usergroup meeting March 13, 2024
SFBA Splunk Usergroup meeting March 13, 2024SFBA Splunk Usergroup meeting March 13, 2024
SFBA Splunk Usergroup meeting March 13, 2024
 
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
Persuasive E-commerce, Our Biased Brain @ Bikkeldag 2024
 
Strategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for ClarityStrategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
Strategic CX: A Deep Dive into Voice of the Customer Insights for Clarity
 
5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best Practices5 Ds to Define Data Archiving Best Practices
5 Ds to Define Data Archiving Best Practices
 
The Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayerThe Universal GTM - how we design GTM and dataLayer
The Universal GTM - how we design GTM and dataLayer
 
Master's Thesis - Data Science - Presentation
Master's Thesis - Data Science - PresentationMaster's Thesis - Data Science - Presentation
Master's Thesis - Data Science - Presentation
 
Mapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptxMapping the pubmed data under different suptopics using NLP.pptx
Mapping the pubmed data under different suptopics using NLP.pptx
 
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptxTINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
TINJUAN PEMROSESAN TRANSAKSI DAN ERP.pptx
 
ChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics InfrastructureChistaDATA Real-Time DATA Analytics Infrastructure
ChistaDATA Real-Time DATA Analytics Infrastructure
 
Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023Cash Is Still King: ATM market research '2023
Cash Is Still King: ATM market research '2023
 
Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...Elements of language learning - an analysis of how different elements of lang...
Elements of language learning - an analysis of how different elements of lang...
 
YourView Panel Book.pptx YourView Panel Book.
YourView Panel Book.pptx YourView Panel Book.YourView Panel Book.pptx YourView Panel Book.
YourView Panel Book.pptx YourView Panel Book.
 
MEASURES OF DISPERSION I BSc Botany .ppt
MEASURES OF DISPERSION I BSc Botany .pptMEASURES OF DISPERSION I BSc Botany .ppt
MEASURES OF DISPERSION I BSc Botany .ppt
 
Virtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product IntroductionVirtuosoft SmartSync Product Introduction
Virtuosoft SmartSync Product Introduction
 

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