Objectives of this lecture
What is an AVL tree?
typedef
enum BalanceFactor{ LH, EQ, RH }BalanceFactor;
typedef
struct node {TreeEntry entry;
BalanceFactor bf;
struct node *left;
struct node *right;
} TreeNode.
Insertion
Implementation.
/* InsertAVL: insert
newnode in AVL tree starting at the root.
Pre: The root of the AVL tree is pointed by root,
and newnode is a new
node to be inserted into the tree.
Post: newnode has been
inserted into the AVL tree with taller equal to
TRUE if the height of the tree has
increased, FALSE otherwise.
Uses: InsertAVL
recursively, RightBalance, LeftBalance.
*/
TreeNode
*InsertAVL(TreeNode *root, TreeNode *newnode,
Boolean *taller)
{ if (!root) {
root = newnode;
root->left = root->right = NULL;
root->bf = EH;
*taller = TRUE;
} else if (EQ(newnode->entry.key,
root->entry.key)) {
Error("Duplicate key is not
allowed in AVL tree.");
} else if (LT(newnode->entry.key,
root->entry.key)) {
root->left =
InsertAVL(root->left, newnode, taller);
if (*taller) /* Left subtree is taller. */
switch(root->bf) {
case LH: /* Node was left high. */
root = LeftBalance(root,
taller); break;
case EH:
root->bf = LH; break;
/* Node is now left high. */
case RH:
root->bf = EH; /* Node now has balanced height.*/
*taller = FALSE; break;
}
} else {
root->right =
InsertAVL(root->right, newnode, taller);
if (*taller) /* Right subtree is taller. */
switch(root->bf) {
case LH:
root->bf = EH; /* Node now has balanced height.*/
*taller = FALSE; break;
case EH:
root->bf = RH;
break; /* Node is right high. */
case RH: /* Node was right
high. */
root = RightBalance(root,
taller); break;
}
}
return root; }
Balance
restoration
1. Right Higher: The insertion makes the
right of x to be higher
/*
RotateLeft: rotate a binary tree to the left.
Pre: p is the root of the nonempty AVL subtree
being rotated, and its
right child is nonempty.
Post:
The right child of p becomes the new p. The old p becomes the
left child of the new p.
*/
TreeNode
*RotateLeft(TreeNode *p)
{ TreeNode *rightchild = p;
if (!p)
Error("Not possible to rotate
an empty tree in RotateLeft.");
else if (!p->right)
Error("Not possible to make an
empty subtree the root.");
else {
rightchild = p->right;
p->right = rightchild->left;
rightchild->left = p;
}
return rightchild;
}
2. Left Higher: The insertion makes the left of
x to be higher
old w |
new r |
new x |
- |
- |
- |
/ |
- |
\ |
\ |
/ |
- |
Example:
/*
RightBalance: right balance a binary tree.
Pre: A node of an AVL tree has become doubly
unbalanced to the right.
Post:
The AVL properties have been restored.
Uses:
RotateRight, RotateLeft.
*/
TreeNode
*RightBalance(TreeNode *root, Boolean *taller)
{ TreeNode *rs = root->right; /* right subtree of root*/
TreeNode *ls; /* left subtree
of right subtree */
switch(rs->bf) {
case RH:
root->bf = rs->bf = EH;
root = RotateLeft(root); /* single rotation left */
*taller = FALSE;
break;
case EH:
Error("Tree is already
balanced");
break;
case LH: /* double rotation left */
ls = rs->left;
switch(ls->bf) {
case RH:
root->bf = LH;
rs->bf = EH;
break;
case EH:
root->bf = rs->bf =
EH;
break;
case LH:
root->bf = EH;
rs->bf = RH;
break;
}
ls->bf = EH;
root->right = RotateRight(rs);
root = RotateLeft(root);
*taller = FALSE;
}
return root;
}
Analysis:
Deletion of a Node:
Exercises:
Implement
the LeftBalance function.
Try
Exercises E1-E5, of pages 436-437
of your book.