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
Ad

More Related Content

What's hot (20)

Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
kalyanineve
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Tree
TreeTree
Tree
Raj Sarode
 
Allocation methods (1).pptx
Allocation methods (1).pptxAllocation methods (1).pptx
Allocation methods (1).pptx
ssuser55cbdb
 
Directory structure
Directory structureDirectory structure
Directory structure
sangrampatil81
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Heap and heapsort
Heap and heapsortHeap and heapsort
Heap and heapsort
Amit Kumar Rathi
 
Binary tree
Binary  treeBinary  tree
Binary tree
Vanitha Chandru
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
ASairamSairam1
 
Binary Tree Traversal
Binary Tree TraversalBinary Tree Traversal
Binary Tree Traversal
Dhrumil Panchal
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Stack and Queue
Stack and Queue Stack and Queue
Stack and Queue
Apurbo Datta
 
Queues
QueuesQueues
Queues
Lovely Professional University
 
Trees
TreesTrees
Trees
Burhan Ahmed
 
b+ tree
b+ treeb+ tree
b+ tree
bitistu
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
khabbab_h
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 
Data Structure (Tree)
Data Structure (Tree)Data Structure (Tree)
Data Structure (Tree)
Adam Mukharil Bachtiar
 
Unit 1 introduction to data structure
Unit 1   introduction to data structureUnit 1   introduction to data structure
Unit 1 introduction to data structure
kalyanineve
 
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
PPT On Sorting And Searching Concepts In Data Structure | In Programming Lang...
Umesh Kumar
 
Allocation methods (1).pptx
Allocation methods (1).pptxAllocation methods (1).pptx
Allocation methods (1).pptx
ssuser55cbdb
 
Binary Search Tree
Binary Search TreeBinary Search Tree
Binary Search Tree
sagar yadav
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Trees in data structures
Trees in data structuresTrees in data structures
Trees in data structures
ASairamSairam1
 
linked lists in data structures
linked lists in data structureslinked lists in data structures
linked lists in data structures
DurgaDeviCbit
 
Binary Search Tree in Data Structure
Binary Search Tree in Data StructureBinary Search Tree in Data Structure
Binary Search Tree in Data Structure
Dharita Chokshi
 
Threaded Binary Tree
Threaded Binary TreeThreaded Binary Tree
Threaded Binary Tree
khabbab_h
 
Binary Tree in Data Structure
Binary Tree in Data StructureBinary Tree in Data Structure
Binary Tree in Data Structure
Meghaj Mallick
 

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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Ch17
Ch17Ch17
Ch17
Abbott
 
Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perl
sana mateen
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
andreeamolnar
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
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
widespreadpromotion
 
Chapter 11 - Sorting and Searching
Chapter 11 - Sorting and SearchingChapter 11 - Sorting and Searching
Chapter 11 - Sorting and Searching
Eduardo Bergavera
 
Array,lists and hashes in perl
Array,lists and hashes in perlArray,lists and hashes in perl
Array,lists and hashes in perl
sana mateen
 
Lists, queues and stacks
Lists, queues and stacksLists, queues and stacks
Lists, queues and stacks
andreeamolnar
 
Ad

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

avl trees avl trees avl trees avl trees avl trees avl trees avl trees
avl trees avl trees avl trees avl trees avl trees avl trees avl treesavl trees avl trees avl trees avl trees avl trees avl trees avl trees
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
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
alisha230390
 
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
 
104333 sri vidhya eng notes
104333 sri vidhya eng notes104333 sri vidhya eng notes
104333 sri vidhya eng notes
Krishnakumar Btech
 
17. Trees and Tree Like Structures
17. Trees and Tree Like Structures17. Trees and Tree Like Structures
17. Trees and Tree Like Structures
Intro C# Book
 
DSA Lec 02 - Stacks and Queues Presentation
DSA Lec 02 - Stacks and Queues PresentationDSA Lec 02 - Stacks and Queues Presentation
DSA Lec 02 - Stacks and Queues Presentation
ibad29377
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .
Ashutosh Satapathy
 
chap11.ppt
chap11.pptchap11.ppt
chap11.ppt
AswiniJ6
 
Cal Essay
Cal EssayCal Essay
Cal Essay
Sherry Bailey
 
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
sumitbardhan
 
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
kavi806657
 
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)
St Mary's College,Thrissur,Kerala
 
Kid171 chap02 english version
Kid171 chap02 english versionKid171 chap02 english version
Kid171 chap02 english version
Frank S.C. Tseng
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
Shakila Mahjabin
 
Introduction to Data Structures – Abstract Data Types- Classification of Data...
Introduction to Data Structures – Abstract Data Types- Classification of Data...Introduction to Data Structures – Abstract Data Types- Classification of Data...
Introduction to Data Structures – Abstract Data Types- Classification of Data...
kavi806657
 
Understanding Complete Linked Lists.pptx
Understanding Complete Linked Lists.pptxUnderstanding Complete Linked Lists.pptx
Understanding Complete Linked Lists.pptx
rishjain0910
 
Introduction to database-Normalisation
Introduction to database-NormalisationIntroduction to database-Normalisation
Introduction to database-Normalisation
Ajit 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 Indexes
Leonidas Akritidis
 
Data-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdfData-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdf
lehal93146
 
datastructure-201021140600.pptx
datastructure-201021140600.pptxdatastructure-201021140600.pptx
datastructure-201021140600.pptx
ZISAN5
 
avl trees avl trees avl trees avl trees avl trees avl trees avl trees
avl trees avl trees avl trees avl trees avl trees avl trees avl treesavl trees avl trees avl trees avl trees avl trees avl trees avl trees
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
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
alisha230390
 
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 Structures
Intro C# Book
 
DSA Lec 02 - Stacks and Queues Presentation
DSA Lec 02 - Stacks and Queues PresentationDSA Lec 02 - Stacks and Queues Presentation
DSA Lec 02 - Stacks and Queues Presentation
ibad29377
 
Introduction to Data Structures .
Introduction to Data Structures        .Introduction to Data Structures        .
Introduction to Data Structures .
Ashutosh Satapathy
 
chap11.ppt
chap11.pptchap11.ppt
chap11.ppt
AswiniJ6
 
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
sumitbardhan
 
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
Trees — Tree Terminology – Binary Trees – Binary Search Trees – Tree Traversa...
kavi806657
 
Kid171 chap02 english version
Kid171 chap02 english versionKid171 chap02 english version
Kid171 chap02 english version
Frank S.C. Tseng
 
Introduction to Data Structures – Abstract Data Types- Classification of Data...
Introduction to Data Structures – Abstract Data Types- Classification of Data...Introduction to Data Structures – Abstract Data Types- Classification of Data...
Introduction to Data Structures – Abstract Data Types- Classification of Data...
kavi806657
 
Understanding Complete Linked Lists.pptx
Understanding Complete Linked Lists.pptxUnderstanding Complete Linked Lists.pptx
Understanding Complete Linked Lists.pptx
rishjain0910
 
Introduction to database-Normalisation
Introduction to database-NormalisationIntroduction to database-Normalisation
Introduction to database-Normalisation
Ajit 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 Indexes
Leonidas Akritidis
 
Data-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdfData-Structure-original-QuantumSupply.pdf
Data-Structure-original-QuantumSupply.pdf
lehal93146
 
datastructure-201021140600.pptx
datastructure-201021140600.pptxdatastructure-201021140600.pptx
datastructure-201021140600.pptx
ZISAN5
 
Ad

Recently uploaded (20)

Lesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdfLesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdf
hemelali11
 
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
lecture_13 tree in mmmmmmmm     mmmmmfftro.pptxlecture_13 tree in mmmmmmmm     mmmmmfftro.pptx
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
sarajafffri058
 
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdfZ14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Fariborz Seyedloo
 
AWS-Certified-ML-Engineer-Associate-Slides.pdf
AWS-Certified-ML-Engineer-Associate-Slides.pdfAWS-Certified-ML-Engineer-Associate-Slides.pdf
AWS-Certified-ML-Engineer-Associate-Slides.pdf
philsparkshome
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
Automation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success storyAutomation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success story
Process mining Evangelist
 
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial IntelligenceDr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug
 
AI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptxAI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptx
AyeshaJalil6
 
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docxAnalysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
hershtara1
 
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdfTOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
NhiV747372
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfjOral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
maitripatel5301
 
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
muhammed84essa
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
2024 Digital Equity Accelerator Report.pdf
2024 Digital Equity Accelerator Report.pdf2024 Digital Equity Accelerator Report.pdf
2024 Digital Equity Accelerator Report.pdf
dominikamizerska1
 
Process Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital TransformationsProcess Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital Transformations
Process mining Evangelist
 
problem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursingproblem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursing
vishnudathas123
 
AWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdfAWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdf
philsparkshome
 
Time series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdfTime series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdf
asmaamahmoudsaeed
 
Process Mining Machine Recoveries to Reduce Downtime
Process Mining Machine Recoveries to Reduce DowntimeProcess Mining Machine Recoveries to Reduce Downtime
Process Mining Machine Recoveries to Reduce Downtime
Process mining Evangelist
 
Lesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdfLesson 6-Interviewing in SHRM_updated.pdf
Lesson 6-Interviewing in SHRM_updated.pdf
hemelali11
 
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
lecture_13 tree in mmmmmmmm     mmmmmfftro.pptxlecture_13 tree in mmmmmmmm     mmmmmfftro.pptx
lecture_13 tree in mmmmmmmm mmmmmfftro.pptx
sarajafffri058
 
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdfZ14_IBM__APL_by_Christian_Demmer_IBM.pdf
Z14_IBM__APL_by_Christian_Demmer_IBM.pdf
Fariborz Seyedloo
 
AWS-Certified-ML-Engineer-Associate-Slides.pdf
AWS-Certified-ML-Engineer-Associate-Slides.pdfAWS-Certified-ML-Engineer-Associate-Slides.pdf
AWS-Certified-ML-Engineer-Associate-Slides.pdf
philsparkshome
 
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
2-Raction quotient_١٠٠١٤٦.ppt of physical chemisstry
bastakwyry
 
Automation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success storyAutomation Platforms and Process Mining - success story
Automation Platforms and Process Mining - success story
Process mining Evangelist
 
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial IntelligenceDr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug - Expert In Artificial Intelligence
Dr. Robert Krug
 
AI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptxAI ------------------------------ W1L2.pptx
AI ------------------------------ W1L2.pptx
AyeshaJalil6
 
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docxAnalysis of Billboards hot 100 toop five hit makers on the chart.docx
Analysis of Billboards hot 100 toop five hit makers on the chart.docx
hershtara1
 
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdfTOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
TOAE201-Slides-Chapter 4. Sample theoretical basis (1).pdf
NhiV747372
 
CS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docxCS-404 COA COURSE FILE JAN JUN 2025.docx
CS-404 COA COURSE FILE JAN JUN 2025.docx
nidarizvitit
 
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfjOral Malodor.pptx jsjshdhushehsidjjeiejdhfj
Oral Malodor.pptx jsjshdhushehsidjjeiejdhfj
maitripatel5301
 
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
CERTIFIED BUSINESS ANALYSIS PROFESSIONAL™
muhammed84essa
 
Controlling Financial Processes at a Municipality
Controlling Financial Processes at a MunicipalityControlling Financial Processes at a Municipality
Controlling Financial Processes at a Municipality
Process mining Evangelist
 
2024 Digital Equity Accelerator Report.pdf
2024 Digital Equity Accelerator Report.pdf2024 Digital Equity Accelerator Report.pdf
2024 Digital Equity Accelerator Report.pdf
dominikamizerska1
 
Process Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital TransformationsProcess Mining as Enabler for Digital Transformations
Process Mining as Enabler for Digital Transformations
Process mining Evangelist
 
problem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursingproblem solving.presentation slideshow bsc nursing
problem solving.presentation slideshow bsc nursing
vishnudathas123
 
AWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdfAWS Certified Machine Learning Slides.pdf
AWS Certified Machine Learning Slides.pdf
philsparkshome
 
Time series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdfTime series for yotube_1_data anlysis.pdf
Time series for yotube_1_data anlysis.pdf
asmaamahmoudsaeed
 
Process Mining Machine Recoveries to Reduce Downtime
Process Mining Machine Recoveries to Reduce DowntimeProcess Mining Machine Recoveries to Reduce Downtime
Process Mining Machine Recoveries to Reduce Downtime
Process mining Evangelist
 

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
  翻译: