Docsity

AVL Trees: Balanced Binary Search Trees with Rotations, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
worksheet 31: AVL Trees Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 31: AVL Trees
In Preparation: Read Chapters 8 and 10 on Bags and Trees, respectively. In you have
not done so ready, do Worksheets 29 and 30 on Binary Search Trees.
The time required to perform operations on a binary search
tree is proportional to the length of the path from root to leaf.
This isn’t bad in a well-balanced tree. But nothing prevents a
tree from becoming unbalanced. In the extreme, such as the
tree shown on the right, the tree reduces to nothing more than
a linked list, and the path from root to leaf is O(n).
To preserve fast performance we need to ensure that the tree
remains well balanced. One way to do this is to notice that the
search tree property has some flexibility. Three nodes that are
unbalanced can be restored by a rotation, making the right child into the new root, with
the previous root as the new left child. Any existing left child of the old right child
becomes the new right child of the new left child. The resulting tree is still a binary
search tree, and has better balance.
This is known as a left rotation. There is also a corresponding right rotation. There is one
case where a simple rotation is not sufficient. Consider an unbalanced tree with a right
child that itself has a left child. If we perform a rotation, the result is still unbalanced.
The solution is to first perform a rotation on the child, and then rotate the parent. This is
termed a double rotation.
3
4
6
4
3
6
3
6
4
6
4
3
3
6
4
3
4
6
4
3
6
pf3
pf4

Partial preview of the text

Download AVL Trees: Balanced Binary Search Trees with Rotations and more Study notes Data Structures and Algorithms in PDF only on Docsity!

Worksheet 31: AVL Trees

In Preparation : Read Chapters 8 and 10 on Bags and Trees, respectively. In you have

not done so ready, do Worksheets 29 and 30 on Binary Search Trees.

The time required to perform operations on a binary search

tree is proportional to the length of the path from root to leaf.

This isn’t bad in a well-balanced tree. But nothing prevents a

tree from becoming unbalanced. In the extreme, such as the

tree shown on the right, the tree reduces to nothing more than

a linked list, and the path from root to leaf is O(n).

To preserve fast performance we need to ensure that the tree

remains well balanced. One way to do this is to notice that the

search tree property has some flexibility. Three nodes that are

unbalanced can be restored by a rotation , making the right child into the new root, with

the previous root as the new left child. Any existing left child of the old right child

becomes the new right child of the new left child. The resulting tree is still a binary

search tree, and has better balance.

This is known as a left rotation. There is also a corresponding right rotation. There is one

case where a simple rotation is not sufficient. Consider an unbalanced tree with a right

child that itself has a left child. If we perform a rotation, the result is still unbalanced.

The solution is to first perform a rotation on the child, and then rotate the parent. This is

termed a double rotation.

The data structure termed the AVL tree was designed using these ideas. The name honors

the inventors of the data structure, the Russian mathematicians Georgii M. Adel'son-

Vel'ski\u{\i} and Evgeni\u{\i} Mikha\u{\i}lovich Landis.

In order to know when to perform a rotation, it is

necessary to know the height of a node. We could

calculate this amount, but that would slow the

algorithm. Instead, we modify the Node so that each

node keeps a record of its own height.

A method h(Node) will be useful to determine the height of a child node. Since leaf

nodes have height zero, a value of - 1 is returned for a null value. Using this, a method

setHeight can be defined that will set the height value of a node, assuming that the height

of the child nodes is known:

int h(struct AVLnode * current) {if (current == 0) return - 1; return current->height;} void setHeight (struct AVLnode * current) { int lch = h(current->left); int rch = h(current->right); if (lch < rch) current->height = 1 + rch; else current->height = 1 + lch; }

Armed with the height information, the AVL tree algorithms are now easy to describe.

The addition and removal algorithms for the balanced binary search tree are modified so

that their very last step is to invoke a method balance :

struct AVLnode * AVLnodeAdd (struct AVLnode* current, EleType newValue) { struct AVLnode * newnode; if (current == 0) { newnode = (struct AVLnode ) malloc(sizeof(struct AVLnode)); assert(newnode != 0); newnode->value = newValue; newnode->left = newnode->right = 0; return balance(newnode); } else if (LT(newValue, current->value)) current->left = AVLnodeAdd(current->left, newValue); else current->right = AVLnodeAdd(current->right, newValue); return balance(current); / <- NEW the call on balance */ }

The function balance performs the rotations necessary to restore the balance in the tree.

Let the balance factor be the difference in height between the right and left child trees.

This is easily computed using a function. If the balance factor is more than 2, that is, if

one subtree is more than two levels different in height from the other, then a rebalancing

is performed. A check must be performed for double rotations, but again this is easy to

determine using the balance factor function. Once the tree has been rebalanced the height

is set by calling setHeight:

struct AVLnode { EleType value; struct AVLnode *left; struct AVLnode *right; int height; };

Complete the implementation of the AVLtree abstraction by writing the methods to

perform a left and right rotation. Both these methods should call setHeight on both the

new interior node that has been changed and the new root. Other methods that are similar

to those of the Binary Search Tree have been omitted:

struct AVLnode * rotateLeft (struct AVLnode * current) { struct AVLnode * newtop = n->right; n->right = newtop->left; newtop->left = n; setHeight(n); setHeight(newtop); return newtop; } struct AVLnode * rotateRight (struct AVLnode * current) { struct AVLnode * newtop = n->left; n->left = newtop->right; newtop->right = n; setHeight(n); setHeight(newtop); return newtop; }