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

Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms ManishPrajapati78
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureBalwant Gorad
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Treesagar yadav
 
Array data structure
Array data structureArray data structure
Array data structuremaamir farooq
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data StructureDharita Chokshi
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSAjunnubabu
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2Self-Employed
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary treeKrish_ver2
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
UNIT III NON LINEAR DATA STRUCTURES – TREES
UNIT III 	NON LINEAR DATA STRUCTURES – TREESUNIT III 	NON LINEAR DATA STRUCTURES – TREES
UNIT III NON LINEAR DATA STRUCTURES – TREESKathirvel Ayyaswamy
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure pptNalinNishant3
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptxSyed Zaid Irshad
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)Elavarasi K
 
SQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJSQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJDharita Chokshi
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure Janki Shah
 

What's hot (20)

Data Structure and Algorithms
Data Structure and Algorithms Data Structure and Algorithms
Data Structure and Algorithms
 
Searching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data StructureSearching and Sorting Techniques in Data Structure
Searching and Sorting Techniques in Data Structure
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
 
Array data structure
Array data structureArray data structure
Array data structure
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
 
Priority queue in DSA
Priority queue in DSAPriority queue in DSA
Priority queue in DSA
 
Abstract data types (adt) intro to data structure part 2
Abstract data types (adt)   intro to data structure part 2Abstract data types (adt)   intro to data structure part 2
Abstract data types (adt) intro to data structure part 2
 
1.1 binary tree
1.1 binary tree1.1 binary tree
1.1 binary tree
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
UNIT III NON LINEAR DATA STRUCTURES – TREES
UNIT III 	NON LINEAR DATA STRUCTURES – TREESUNIT III 	NON LINEAR DATA STRUCTURES – TREES
UNIT III NON LINEAR DATA STRUCTURES – TREES
 
Structure in C language
Structure in C languageStructure in C language
Structure in C language
 
Tree - Data Structure
Tree - Data StructureTree - Data Structure
Tree - Data Structure
 
First and follow set
First and follow setFirst and follow set
First and follow set
 
Introduction to data structure ppt
Introduction to data structure pptIntroduction to data structure ppt
Introduction to data structure ppt
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
 
Data Structures (CS8391)
Data Structures (CS8391)Data Structures (CS8391)
Data Structures (CS8391)
 
Object Based Databases
Object Based DatabasesObject Based Databases
Object Based Databases
 
SQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJSQL, Embedded SQL, Dynamic SQL and SQLJ
SQL, Embedded SQL, Dynamic SQL and SQLJ
 
Queue in Data Structure
Queue in Data Structure Queue in Data Structure
Queue in Data Structure
 

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

Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Colleen Farrelly
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfSocial Samosa
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryJeremy Anderson
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Cantervoginip
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...limedy534
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsVICTOR MAESTRE RAMIREZ
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.natarajan8993
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfgstagge
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfchwongval
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改yuu sss
 
MK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxMK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxUnduhUnggah1
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degreeyuu sss
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSINGmarianagonzalez07
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhijennyeacort
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...soniya singh
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanMYRABACSAFRA2
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]📊 Markus Baersch
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...Florian Roscheck
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queensdataanalyticsqueen03
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsappssapnasaifi408
 

Recently uploaded (20)

Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024Generative AI for Social Good at Open Data Science East 2024
Generative AI for Social Good at Open Data Science East 2024
 
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdfKantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
Kantar AI Summit- Under Embargo till Wednesday, 24th April 2024, 4 PM, IST.pdf
 
Defining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data StoryDefining Constituents, Data Vizzes and Telling a Data Story
Defining Constituents, Data Vizzes and Telling a Data Story
 
ASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel CanterASML's Taxonomy Adventure by Daniel Canter
ASML's Taxonomy Adventure by Daniel Canter
 
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
Effects of Smartphone Addiction on the Academic Performances of Grades 9 to 1...
 
Advanced Machine Learning for Business Professionals
Advanced Machine Learning for Business ProfessionalsAdvanced Machine Learning for Business Professionals
Advanced Machine Learning for Business Professionals
 
RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.RABBIT: A CLI tool for identifying bots based on their GitHub events.
RABBIT: A CLI tool for identifying bots based on their GitHub events.
 
RadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdfRadioAdProWritingCinderellabyButleri.pdf
RadioAdProWritingCinderellabyButleri.pdf
 
Multiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdfMultiple time frame trading analysis -brianshannon.pdf
Multiple time frame trading analysis -brianshannon.pdf
 
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
专业一比一美国俄亥俄大学毕业证成绩单pdf电子版制作修改
 
MK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docxMK KOMUNIKASI DATA (TI)komdat komdat.docx
MK KOMUNIKASI DATA (TI)komdat komdat.docx
 
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree澳洲中央昆士兰大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
2006_GasProcessing_HB (1).pdf HYDROCARBON PROCESSING
 
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝DelhiRS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
RS 9000 Call In girls Dwarka Mor (DELHI)⇛9711147426🔝Delhi
 
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
High Class Call Girls Noida Sector 39 Aarushi 🔝8264348440🔝 Independent Escort...
 
Identifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population MeanIdentifying Appropriate Test Statistics Involving Population Mean
Identifying Appropriate Test Statistics Involving Population Mean
 
GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]GA4 Without Cookies [Measure Camp AMS]
GA4 Without Cookies [Measure Camp AMS]
 
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...From idea to production in a day – Leveraging Azure ML and Streamlit to build...
From idea to production in a day – Leveraging Azure ML and Streamlit to build...
 
Top 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In QueensTop 5 Best Data Analytics Courses In Queens
Top 5 Best Data Analytics Courses In Queens
 
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /WhatsappsBeautiful Sapna Vip  Call Girls Hauz Khas 9711199012 Call /Whatsapps
Beautiful Sapna Vip Call Girls Hauz Khas 9711199012 Call /Whatsapps
 

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