10. search tree - data structures using c++ by varsha patil

35
Oxford University Press © 2012 Data Structures Using C++ by Dr Varsha Patil 1 10.SEARCH TREES

Upload: widespreadpromotion

Post on 12-Jan-2017

622 views

Category:

Data & Analytics


5 download

TRANSCRIPT

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

1Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

10.SEARCH TREES

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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 :

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

22Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

Case 2: RR (Right of Right)

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

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)

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

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)

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

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

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

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)

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

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)

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

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)

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

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

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

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

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

31Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

(b) Delete 4

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

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

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

33Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

(d) Imbalance at node 5 implies a RR rotation around node 8

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

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.

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

35Oxford University Press © 2012

Data Structures Using C++ by Dr Varsha Patil

End of Chapter 10…!