10. search tree - data structures using c++ by varsha patil
TRANSCRIPT
1Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
10.SEARCH TREES
2Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Introduction
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
3Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
OBJECTIVES Variations in binary search trees: static and
dynamic Ways of building trees of each type to
guarantee that the trees remain balanced
4Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Binary Search Tree 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
5Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Binary Search Tree These two cases lead to the following two
kinds of search trees: Static BST Dynamic BST
6Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
7Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
8Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
SYMBOL TABLE 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
9Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
SYMBOL TABLE 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
10Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Representation of Symbol Table
There are two different techniques for implementing a keyed table: symbol table and tree table
Static Tree Tables Dynamic Tree Tables
11Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Static Tree Tables 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
12Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Optimal Binary Search Tree
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
13Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Dynamic Tree Tables
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
14Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
AVL TREE(HEIGHT-BALANCED TREE)
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
15Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Balance Factor 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
16Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
17Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Balance Factor 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
18Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Balance Factor 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
19Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
For example
Balancing a tree by rotating towards right (a) Unbalanced Balanced tree
20Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Balancing a tree by rotating towards left (a) Unbalanced tree (b) Balanced tree
Example
21Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Repetition Construct
Case 1: LL (Left of Left)Consider the BST in Fig :
22Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Case 2: RR (Right of Right)
23Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Case 3: RL (Right of Left)
Case LR for unbalanced tree due to insertion in right of left of a node (a)
24Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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)
25Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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
26Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Case 4: LR (Left of Right)
Case RL for unbalancing due to insertion in left of right of a node (a)
27Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Case RL for unbalancing due to insertion in left of right of a node (a) Scenario 1 (b)
Case 4: LR (Left of Right)
28Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
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)
29Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Insertions and Deletions in AVL Tree
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
30Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Figure demonstrates the deletion of a node in a given AVL tree
(a) Original tree
Deletion of a node in AVL tree
31Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
(b) Delete 4
32Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
(c)Note the imbalance at node 3 implies an LL rotation around node 2
33Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
(d) Imbalance at node 5 implies a RR rotation around node 8
34Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
Summary 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.
35Oxford University Press © 2012
Data Structures Using C++ by Dr Varsha Patil
End of Chapter 10…!