51
MA 513: Data Structures Lecture Note http://www.iitg.ernet.in/psm/indexing_ma513/y09/index.html Partha Sarathi Mandal [email protected] Dept. of Mathematics, IIT Guwahati lTue 9:00-9:55 Wed 10:00-10:55 Thu 11:00-11:55 Class Room : 1G2 lMA514 Data Structure Lab : Tue 14:00-16:55

MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Embed Size (px)

Citation preview

Page 1: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

MA 513: Data Structures Lecture Note

http://www.iitg.ernet.in/psm/indexing_ma513/y09/index.html

Partha Sarathi [email protected]

Dept. of Mathematics, IIT Guwahati

lTue 9:00-9:55 Wed 10:00-10:55 Thu 11:00-11:55 Class Room : 1G2

lMA514 Data Structure Lab : Tue 14:00-16:55

Page 2: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Constructing an expression Tree

• Convert postfix expression to expression tree

• a b + c d e + * *

a b

Page 3: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Constructing an expression Tree

• Convert postfix expression to expression tree

• a b + c d e + * *

a b

+

Page 4: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Constructing an expression Tree

• Convert postfix expression to expression tree

• a b + c d e + * *

a b

+ c d e

Page 5: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Constructing an expression Tree

• Convert postfix expression to expression tree

• a b + c d e + * *

a b

+ c

d e

+

Page 6: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Constructing an expression Tree

• Convert postfix expression to expression tree

• a b + c d e + * *

a b

+

c

d e

+

*

Page 7: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Constructing an expression Tree

• Convert postfix expression to expression tree

• a b + c d e + * *

a b

+

c

d e

+

*

*

Page 8: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL-Trees

Page 9: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Objectives

• Understand the concept of an AVL tree.

• Understand how AVL trees and BSTs differ.

• Understand the issues involved in balancing an AVL tree.

Page 10: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Introduction

• Named for its inventors (Adelson-Velsky and Landis), an AVL tree is a binary search tree in which the heights of the subtrees of a node differ by no more than 1.

• It is thus a balanced BST.

• To understand the significance of the tree being balanced, let’s look at two different trees containing the same data.

Page 11: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Introduction

Page 12: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

This “tree” is just a linked

list in binary tree clothing.

It takes 2 tests to locate

12, 3 to locate 14, and 8

to locate 52.

Hence, the search effort

for this binary tree is

O(n).

Introduction

Page 13: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

This BST is an AVL tree.

It takes 2 tests to locate

18, 3 to locate 12, and 4

to locate 8.

Hence, the search effort

for this binary tree is

O(log2n).

Introduction

Page 14: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Introduction

• For a tree with 1000 nodes, the worst case for a completely unbalanced tree is 1000 tests.

• However, the worst case for a balanced tree is 10 tests.

• Hence, balancing a tree can lead to significant improvements.

Page 15: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Balanced Binary Search Trees

• height is O(log n), where n is the number of elements in the tree

• AVL (Adelson-Velsky and Landis) trees

• red-black trees

• get, put, and remove take O(log n) time

Page 16: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Balanced Binary Search Trees

• Indexed AVL trees

• Indexed red-black trees

• Indexed operations also take

– O(log n) time

Page 17: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Balanced Search Trees

• weight balanced binary search trees

• 2-3 & 2-3-4 trees

• B-trees

• etc.

Page 18: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL Tree

• binary tree

• for every node x, define its balance factor

balance factor of x = height of left subtree of x - height of right subtree of x

• balance factor of every node x is -1, 0, or 1

Page 19: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Balance Factors

• This is an AVL tree.

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

Page 20: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Height

The height of an AVL tree that has n nodes is

at most 1.44 log2 (n+2).

The height of every n node binary tree is

at least log2 (n+1).

Page 21: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL Search Tree

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

10

7

83

1 5

30

40

20

25

35

45

60

Page 22: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

put(9)

0 0

0 0

1

0

-1 0

1

0

-1

1

-1

90

-1

0

10

7

83

1 5

30

40

20

25

35

45

60

Page 23: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

put(29)

0 0

0 0

1

0

0

1

0

-1

1

-110

7

83

1 5

30

40

25

35

45

60

29

0

-1

-1-2

RR imbalance => new node is in right

subtree of right subtree of blue node

(node with bf = -2)

20

Page 24: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

put(29)

0 0

0 0

1

0

0

1

0

-1

1

-1

10

7

83

1 5

30

40

2535

45

600

RR rotation

20

029

Page 25: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Balanced binary tree

• The disadvantage of a binary search tree is that its height can be as large as N-1

• This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case

• We want a tree with small height

• A binary tree with N node has height at least (log N)

• Thus, our goal is to keep the height of a binary search tree O(log N)

• Such trees are called balanced binary search trees. Examples are AVL tree, red-black tree.

Page 26: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL tree

Height of a node

• The height of a leaf is zero.

• The height of an internal node is the maximum height of its children plus 1.

Page 27: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL tree

• An AVL tree is a binary search tree in which

– for every node in the tree, the height of the left and right subtrees differ by at most 1.

AVL property

violated here

Page 28: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL tree

2

2

21

2

12

1

h

h

hhh

N

N

NNN

• Let x be the root of an AVL tree of height h• Let Nh denote the minimum number of nodes in an AVL

tree of height h• Clearly, Ni ≥ Ni-1 by definition• We have

• By repeated substitution, we obtain the general form

• The boundary conditions are: N0=1 and N1 =2. This implies that h = O(log Nh).

• Thus, many operations (searching, insertion, deletion) on an AVL tree will take O(log N) time.

ih

i

h NN 22

Page 29: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Rotations

• When the tree structure changes (e.g., insertion or deletion), we need to transform the tree to restore the AVL tree property.

• This is done using single rotations or double rotations.

x

y

AB

C

y

x

AB C

Before Rotation After Rotation

e.g. Single Rotation

Page 30: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Rotations

• Since an insertion/deletion involves adding/ deleting a single node, this can only increase/ decrease the height of some subtree by 1

• Thus, if the AVL tree property is violated at a node x, it means that the heights of left(x) ad right(x) differ by exactly 2.

• Rotations will be applied to x to restore the AVL tree property.

Page 31: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Insertion

• First, insert the new key as a new leaf just as in ordinary binary search tree.

• Then trace the path from the new leaf towards the root. For each node x encountered, check if heights of left(x) and right(x) differ by at most 1.

• If yes, proceed to parent(x). If not, restructure by doing either a single rotation or a double rotation [next slide].

• For insertion, once we perform a rotation at a node x, we won’t need to perform any rotation at any ancestor of x.

Page 32: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Insertion

• Let x be the node at which left(x) and right(x) differ by more than 1.

• Assume that the height of x is h+3• There are 4 cases

– Height of left(x) is h+2 (i.e. height of right(x) is h)• LL: Height of left(left(x)) is h+1 single rotate with left child• LR: Height of right(left(x)) is h+1 double rotate with left

child

– Height of right(x) is h+2 (i.e. height of left(x) is h)• RR: Height of right(right(x)) is h+1 single rotate with right

child.• RL: Height of left(right(x)) is h+1 double rotate with right

child.

Page 33: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL Rotations

• RR: right-right

• LL: left-left

• RL: right-left

• LR: left-right

Page 34: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Single rotation (LL)

The new key is inserted in the subtree A.

The AVL-property is violated at x

o height of left(x) is h+2

o height of right(x) is h.

Page 35: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Single rotation (RR)

Single rotation takes O(1) time.

Insertion takes O(log N) time.

The new key is inserted in the subtree C.

The AVL-property is violated at x.

Page 36: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

5

3

1 4

Insert 0.8

AVL Tree

8

0.8

5

3

1 4

8

x

y

A

B

C

3

51

0.84 8

After rotation

Page 37: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Double rotation (LR)The new key is inserted in the subtree B1 or B2.

The AVL-property is violated at x.

x-y-z forms a zig-zag shape

also called left-right rotate

Page 38: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Double rotation (RL)

The new key is inserted in the subtree B1 or B2.

The AVL-property is violated at x.

also called right-left rotate

Page 39: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

5

3

1 4

Insert 3.5

AVL Tree

8

3.5

5

3

1 4

8

4

5

1

3

3.5 After Rotation

x

y

A z

B

C

8

Page 40: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

An Extended Example

Insert 3,2,1,4,5,6,7, 16,15,14

3

Fig 1

3

2

Fig 2

3

2

1

Fig 3

2

1 3Fig 4

2

1 3

4

Fig 5

2

1 3

4

5

Fig 6

Single rotation

Single rotation

Page 41: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

2

1 4

53

Fig 7 6

2

1 4

53

Fig 8

4

2 5

61 3

Fig 9

4

2 5

61 3

7Fig 10

4

2 6

71 3

5 Fig 11

Single rotation

Single rotation

Page 42: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

4

2 6

71 3

5 16

Fig 12

4

2 6

71 3

5 16

15Fig 13

4

2 6

151 3 5

167Fig 14

Double rotation

Page 43: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

5

4

2 7

151 3 6

1614

Fig 16

4

2 6

151 3 5

167

14

Fig 15

Double rotation

Page 44: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

AVL Tree template classtemplate <class keyType> class AVL;template <class keyType>class AvlNode{friend class AVL<keyType>;private:

keyType data;AvlNode *LiftChild, *RightChild;int bf;

};template <class keyType> class AVL{

AvlNode<keyType>* root;public:

AVL(AvlNode<keyType> *init = 0): root(init){};Boolean Insert(const ketType&);Boolean Delete(const ketType&);AvlNode<keyType>* Search(const ketType&);

};

Page 45: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Insert in AVL Treetemplate<class keyType>Boolean AVL<keyType>::Insert(const ketType& x);AvlNode<keyType> *a, *b, *c, *f, *p, *q, *y, *clchild, *crchild;Boolean Found, Unbalanced;int d;{ if(!root){ y = new AvlNode<keyType>; y->data = x;

root=y; root->bf=0; root->leftChild=root->RightChild=0; return TRUE;}

}//a = most resent node with bf +1/-1, f =p[a], q follows p

f=0; a=p=root; q=0; Found = FALSE;While(p && !Found){

if(p->bf){a=p; f=q;}if(x.key < p->data.key){q=p; p=p->LeftChild;}else if(x.key > p->data.key) {q=p; p=p->RightChild;}

else{y=p; Found = TRUE; }

}

Page 46: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Insert in AVL Treeif(!Found){

y = new AvlNode<keyType>; y->data = x; y->bf=0; y->leftChild= y->RightChild=0;

if(x.key < q->data.key)q->LeftChild=y;

else q->RightChild=y;if(x.key > a->data.key){

p=a->RightChild; b=p; d= -1}else { p=a->LeftChild; b=p; d=1}while(p!=y)

if(x.key > p->data.key){p->bf = -1; p=p->RightChild;}else {p->bf = 1; p=p->LeftChild;}

Unbalanced = TRUE;If( !(a->bf) || !(a->bf+d)){

a->bf+=d; Unbalanced = FALSE;}

If(Unbalanced){ //tree unbalancedif(d==1){ // left imbalanceif(b->bf==1){//rotation LL

a->LeftChild = b->RightChild;b->RightChild=a, a->bf=0; b->bf=0;

}else{ //rotation LR

c= b->RightChild;b->RightChild = c->LeftChild;a->LeftChild = c->RightChild ;c->LeftChild =b; c->RightChild =a; switch(c->bf){

case 1: a->bf = -1; b->bf =0; break;case -1: b->bf =1; a->bf =0; break;case 0 : b->bf =0; a->bf =0; break;

} c->bf =0; b=c;} // end of LR

} // end of left imbalanceelse {// right imbalance} if(!f) root = b;else if(a==f->LeftChild) f->leftChild =b;

else if(a==f->RightChild) f->RightChild =b;} // end of tree unbalancedreturn TRUE;} // end of if(!Found)

Page 47: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Deletion

• Delete a node x as in ordinary binary search tree. Note that the last node deleted is a leaf.

• Then trace the path from the new leaf towards the root.

• For each node x encountered, check if heights of left(x) and right(x) differ by at most 1. If yes, proceed to parent(x). If not, perform an appropriate rotation at x. There are 4 cases as in the case of insertion.

• For deletion, after we perform a rotation at x, we may have to perform a rotation at some ancestor of x. Thus, we must continue to trace the path until we reach the root.

Page 48: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Deletion

• On closer examination: the single rotations for deletion can be divided into 4 cases (instead of 2 cases)

– Two cases for rotate with left child

– Two cases for rotate with right child

Page 49: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Single rotations in deletion

rotate with left child

In both figures, a node is deleted in subtree C, causing the height

to drop to h. The height of y is h+2. When the height of subtree A

is h+1, the height of B can be h or h+1. Fortunately, the same

single rotation can correct both cases.

Page 50: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Single rotations in deletion

rotate with right child

In both figures, a node is deleted in subtree A, causing the height

to drop to h. The height of y is h+2. When the height of subtree C

is h+1, the height of B can be h or h+1. A single rotation can

correct both cases.

Page 51: MA 513: Data Structures - Indian Institute of Technology ... 513: Data Structures ... •Convert postfix expression to expression tree •a b + c d e + * * a b. ... •Convert postfix

Rotations in deletion

• There are 4 cases for single rotations, but we do not need to distinguish among them.

• There are exactly two cases for double rotations (as in the case of insertion)

• Therefore, we can reuse exactly the same procedure for insertion to determine which rotation to perform