Professional Documents
Culture Documents
Chapter 4b
Chapter 4b
Height of a node
I The height of a leaf is 1. The height of a null pointer is zero.
I The height of an internal node is the maximum height of its
children plus 1
Note that this definition of height is different from the one we defined
previously (we defined the height of a leaf as zero previously).
(AVL Trees) Data Structures Fall 2020 3 / 33
AVL Tree (Cont’d)
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.
An AVL tree maintains a ”balance factor” (BF(T) = Height(T.right)
- Height(T.left) ) in each node of 0, 1, or -1 i.e. no node’s two child
subtrees differ in height by more than 1.
Basic operation used in AVL trees: A left child could legally have its
parent as its right child.
Detach left child (11)’s right subtree (27) (don’t lose it!)
Consider left child (11) be the new parent.
Attach old parent (43) onto right of new parent (11).
Attach new parent (11)’s old right subtree (27) as left subtree of
old parent (43).
Detach right child (65)’s left subtree (51) (don’t lose it!)
Consider right child (65) be the new parent.
Attach old parent (43) onto left of new parent (65).
Attach new parent (65)’s old left subtree (51) as right subtree of
old parent (43).
After rotation, height of the subtree (in the example, h+1) is the
same as it was before the insert that imbalanced it