AVL Trees
AVL Trees
AVL Trees
To orient yourself, drop the key of the node and use / \ - // \\ to represent the balance factor of that node: \ : right high
/ : left high - : equally high \\ : right imbalance // : left imbalance
h(right subtree) = 1 + h(left subtree)
h(left subtree) = 1 + h(right subtree)
AVL Trees
Non-AVL Trees
AVL Trees
An AVL tree is not a full tree nor it is a minimum level tree. Insertions and deletions are done exactly in the same way as in a BST. However after each operation we need to check the AVL property as the tree may become imbalanced:
/ new node
// / / 8 h different by 2!
AVL Trees
When inserting, if AVL property is not violated we are done. Actually there is a good chance that it will not:
\ \ \ -
/ / \ /
/ -
/
-
/ -
\
10
When will the insertion of a new node cause the tree to become imbalanced?
/ -
\ -
12
UU UU
BBBB U U UU U UUU
13
14
15
16
17
18
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34