AVL Tree Example
AVL Tree Example
AVL Tree Example
• -1 if the depth of the node’s left subtree is 1 more than the depth of the right
node’s left sub tree
• +1 if the depth of the node’s left sub tree is 1 more than the depth of the
right node’s left subtree
• 0 if both of the node’s subtrees has the same depths
• -2 if the depth of the node’s left subtree is 2 more than the depth of the right
node’s left sub tree
• +2 if the depth of the node’s left sub tree is 2 more than the depth of the
right node’s left subtree
• If the balance factor becomes -2 or +2, the tree must be rebalanced.
0 57
0 26 +1 72
-1 25 0 38 0 63 -1 94
0 3 0 37 0 47 0 78
-1 26 +1 72
-2 25 0 38 0 63 -1 94
-1 3 0 37 0 47 0 78
0 1
-1 57
-1 26 +1 72
-1 3 0 37 0 47 0 78
0 1
0 57
0 26 +1 72
0 3 0 38 0 63 -1 94
0 1 0 25 0 37 0 47 0 78
-1 57
+1 26 +1 72
0 3 -1 38 0 63 -1 94
0 1 0 25 -1 37 0 47 0 78
0 30
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
+1 30
0 32
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
+1 30
Left Right Case
0 32
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 78
0 47
-1 32
0 30
-1 57
+1 26 +1 72
0 3 -1 38 0 63 -1 94
0 1 0 25 0 32 0 47 0 78
0 30 0 37
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 +1 32 0 47 0 78
0 30 -1 37
0 35
-2 57
0 3 -2 38 0 63 -1 94
0 1 0 25 +1 32 0 47 0 78
0 30 -1 37
Left Right Case
0 35
-2 57
+2 26 +1 72
0 3 -2 38 0 63 -1 94
0 1 0 25 -2 37 0 47 0 78
0 32
0 30 0 35
-1 57
+1 26 +1 72
0 3 0 37 0 63 -1 94
0 1 0 25 0 32 +1 38 0 78
0 30 0 35 0 47
-1 57
+1 26 +1 72
0 3 0 37 0 63 -1 94
0 1 0 25 0 32 +1 38 0 78
0 30 0 35 0 47
Exercise: insert 36