27
CS 261 – Data Structures AVL Trees

CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Embed Size (px)

Citation preview

Page 1: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

CS 261 – Data Structures

AVL Trees

Page 2: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

AVL Implementation

struct AVLNode {

TYPE val;

struct AVLNode *left;

struct AVLNode *rght;

int hght; /* Height of node*/

};

Page 3: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add

Alex

Abner Angela

Adela

Adam

Abigail

Page 4: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Insert at the Leaf Level

Adam

Alex

Abner Angela

Adela

Adam

Abigail

Alex

Abner Angela

AdelaAbigail

height = 0height = 2

Page 5: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Insert at the Leaf Level

Adam

Alex

Abner Angela

Adela

Adam

Abigail

Alex

Abner Angela

AdelaAbigail

height = 0height = 2

Rotation right

Is it double rotation?

Page 6: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Insert at the Leaf Level

Adam

Alex

Abner Angela

Adela

Adam

Abigail

Alex

Abner Angela

AdelaAbigail

height = 0height = 2

rotation right

height = 1height = 0

left child is heavy on the right

Page 7: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Insert at the Leaf Level

Adam

Alex

Abner Angela

Adela

Adam

Abigail

Alex

Abner Angela

AdelaAbigail

Double rotation right

Page 8: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Insert at the Leaf Level

Adam

Alex

Angela

Adam

Abner

Adela

Abigail

Alex

Abner Angela

AdelaAbigail

Double rotation right

Page 9: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Insert at the Leaf Level

Adam

Alex

AngelaAdam

Abner

Adela

Abigail

Alex

Abner Angela

AdelaAbigail

Double rotation right

height = 1height = 1

Page 10: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Recursive Functionstruct AVLNode *_addAVLNode(struct AVLNode* current, TYPE e){

...

if (current == 0) {/* stop recursion */

...

/*allocate memory for newnode; add newnode to tree*/

return newnode;

}

else {

/* recursively move down the tree */

}

return /* ???? */;

}

Page 11: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Recursive Functionstruct AVLNode *_addAVLNode(struct AVLNode* current, TYPE e){

...

if (current == 0) {/* stop recursion */

...

/*allocate memory for newnode; add newnode to tree*/

return newnode; /* why not call balance() here? */

}

else {

/* recursively move down the tree */

}

return balance(current);

}

Page 12: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Add – Recursive Functionstruct AVLNode *_addAVLNode(struct AVLNode* current, TYPE e){

...

if (current == 0) {/* stop recursion */

... /*allocate memory for newnode; add newnode to tree*/

return newnode;

}

else {/* recursively move down the tree */

if( LT(e, current->value) )

current->left = _addAVLNode(current->left, e);

else

current->right = _addAVLNode(current->right, e);

}

return balance(current);

}

Page 13: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Rotation

13

1(0)

2(1)

6(0)3(0)

4(2)

5(1)

1(0)

2(3)

6(0)

3(0)

4(2)

5(1)

Rotateleft

2(0)

3(1)

7(0)5(0)

4(2)

6(1)7(0)

6(3)

2(0)

3(1)

4(2)

5(0)

Rotateright

Page 14: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Double Rotation

14

7(0)

6(3)

4(0)

5(1)

4(2)

Rotateleft the

left child 7(0)

6(3)

5(2)

4(1)

4(0)

Rotateright

7(0)

6(3)

5(2)

4(1)

4(0)

heavyon right

Page 15: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Balancestruct AVLNode * balance (struct AVLNode * current){

int rotation = _height(current->right) - _height(current->left);

if (rotation < -1) {

/* (double) rotation right */

} else if (rotation > 1) {

/* (double) rotation left */

}

_setHeight(current);

return current;

}

Page 16: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Balance – (Double) Rotation Right...

if (rotation < -1) { /* (double) rotation right */

int drotation = _height(current->left->right) - _height(current->left->left);

if (drotation > 0){ /* double rotation */

/* left child is heavy on the right */

current->left = _rotateLeft(current->left);

}

return _rotateRight(current);

}

else { ...

Page 17: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Balance – (Double) Rotation Left...

}else if (rotation > 1) { /* (double) rotation left */

int drotation = _height(current->right->right)

- _height(current->right->left);

if (drotation < 0) {/* double rotation */

/* right child is heavy on the left */

current->right = _rotateRight(current->right);

}

return _rotateLeft(current);

} ...

Page 18: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Rotation Leftstruct AVLNode * _rotateLeft (struct AVLNode * current){

struct AVLNode * newtop = current->right;

...

}

1(0)

2(3)

6(0)

3(0)

4(2)

5(1)

current

newtop

Page 19: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Rotation Leftstruct AVLNode * _rotateLeft (struct AVLNode * current){

struct AVLNode * newtop = current->right;

current->right = newtop->left;

newtop->left = current;

...

}

1(0)

2(3)

6(0)

3(0)

4(2)

5(1) 1(0)

2(3)

6(0)3(0)

4(2)

5(1)

current

newtop

Page 20: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Rotation Leftstruct AVLNode * _rotateLeft (struct AVLNode * current){

struct AVLNode * newtop = current->right;

current->right = newtop->left;

newtop->left = current;

_setHeight(current);

_setHeight(newtop);

return newtop;

}

1(0)

2(3)

6(0)

3(0)

4(2)

5(1) 1(0)

2(1)

6(0)3(0)

4(2)

5(1)

Page 21: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Rotation Rightstruct AVLNode * _rotateRight (struct AVLNode * current){

struct AVLNode * newtop = current->left;

current->left = newtop->right;

newtop->right = current;

_setHeight(current);

_setHeight(newtop);

return newtop;

}

7(0)

6(3)

2(0)

3(1)

4(2)

5(0) 2(0)

3(1)

7(0)5(0)

4(2)

6(1)

current

newtop

Page 22: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Remove: Who fills the hole in the tree?

Answer: the leftmost child of the right child (smallest element in right subtree)

Page 23: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Remove

void removeAVLTree(struct AVLTree *tree, TYPE val) {

if (containsAVLTree(tree, val)) {

tree->root = _removeNode(tree->root, val);

tree->cnt--;

}

}

Page 24: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

Removestruct AVLNode *_removeNode(struct AVLNode *current, TYPE e) {

struct AVLNode *temp;

assert(current);

if(EQ(e, current->val)){

/* replace current with the leftmost descendant

of the right child */

}

else if(LT(e, current->val))

current->left = _removeNode(current->left, e);

else

current->rght = _removeNode(current->rght, e);

return balance(current);

}

Page 25: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

AVL Trees: Sorting

• An AVL tree can sort a collection of values:

1.Copy data into the AVL tree: O(??)

2.Copy them out using the ?? traversal: O(??)

25

Page 26: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

AVL Trees: Sorting

• An AVL tree can sort a collection of values:Copy data into the AVL tree:

O(n log2n)Copy them out using the in-order traversal:

O(n)

26

Page 27: CS 261 –Data Structuresweb.engr.oregonstate.edu/~sinisa/courses/OSU/CS261/lectures/AVL... · Add –Insert at the Leaf Level Adam Alex Abner Angela Adela Adam Abigail Alex Abner

AVL Trees: Sorting

• Execution time à O(n log n):– Matches that of quick sort in benchmarks– Unlike quick sort, AVL trees don’t have problems if data is

already sorted or almost sorted (which degrades quick sort to O(n2))

• However, requires extra storage to maintain both the original data buffer (e.g., a DynArr) and the tree structure

27