Balanced Binaries –AVL Trees Height of a tree node: 1. The height of a node with no elements is 0 2. The height of a node with 1 element is 1 3. The height of a node with > 1 element is 1 + the height of its tallest subtree
3.
Balanced Binaries –AVL Trees Height of a tree node: 1. The height of a node with no elements is 0 2. The height of a node with 1 element is 1 3. The height of a node with > 1 element is 1 + the height of its tallest subtree AVL tree: A binary tree in which the difference between the height of the right and left subtrees of the root is never more than one.
4.
Balanced Binaries –AVL Trees Height of a tree node: 1. The height of a node with no elements is 0 2. The height of a node with 1 element is 1 3. The height of a node with > 1 element is 1 + the height of its tallest subtree AVL tree: A binary tree in which the difference between the height of the right and left subtrees of the root is never more than one. Each node keeps a balance number which is the difference in heights of its two subtrees. For example, 2 0 -2 1 0 Whenever a balance number is not 0,-1,+1, perform some rotations according to some rules on following pages
5.
Balanced Binaries –AVL Trees Rules for rotation: If: 2 a 1 b c 0 c 0 b a 2 a 0 b c -1 c 1 b a Plus mirror image of these two cases
6.
Balanced Binaries –AVL Trees Rules for rotation: If: 2 a -1 1 d c b 0 -1 0 c d b a 2 a -1 0 d c b 0 0 0 c d b a
7.
Balanced Binaries –AVL Trees Rules for rotation: If: 2 a -1 -1 d c b 0 0 1 c d b a Plus mirror image of these three cases