140 likes | 225 Views
Balanced Trees. There are several ways to define balance Examples: Force the subtrees of each node to have almost equal heights Place upper and lower bounds on the heights of the subtrees of each node. Force the subtrees of each node to have similar sizes (=number of nodes). AVL Trees.
E N D
Balanced Trees • There are several ways to define balance • Examples: • Force the subtrees of each node to have almost equal heights • Place upper and lower bounds on the heights of the subtrees of each node. • Force the subtrees of each node to have similar sizes (=number of nodes)
AVL Trees • AVL tree = • A binary search tree • with the property: • for every node, the heights of the left and right subtrees differ at most by one. • Implementation issues: • Each node contains a value (-1, 1, 0) indicating which subtree is "heavier" • Insert and Delete are modified. They restructure the tree to make it balanced (if necessary).
AVL trees 1 nodes are marked with balance value -1 1 0 1 0 0
-2 2 -2 2 1 -1 -1 1 0 0 0 0 AVL trees: Fixing imbalances • An imbalance is detected when the height difference between two subtrees of a node becomes greater than 1 or smaller than -1. • There are two types of imbalances: or or TYPE 1 TYPE 2
AVL trees: Fixing imbalances • Fixing an imbalance is done by rotating the tree. • There are two types of rotation: • single rotation • for TYPE 1 imbalances • double rotation • for TYPE 2 imbalances • consists of two single rotations. • The rotations must always preserve the BST property.
AVL Trees: single rotation node with imbalance 6 D 4 4 right rotate at node 6 C 2 6 2 A B C D B A This is a single right rotation. A single left rotation is symmetric.
AVL Trees: Double rotation node with imbalance node with imbalance 6 6 4 D D 4 2 2 6 STEP2: right rotate at node 6 C A A B C D 2 4 B A C B STEP 1: left rotate at node 2
AVL Trees: Double rotation If you want to do it in one step, imagine taking 4 and moving it up in between 2 and 6, so that 2 and 6 become its new children: node with imbalance 6 D 4 2 2 6 A 4 A B C D C B B and C will then be adopted by 2 and 6 respectively, in order to maintain the BST property.
AVL Trees: Insert • Insert the node as in a BST • Starting at the newly inserted node, travel up the tree (towards the root), updating the balances along the way. • If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances.
AVL Trees: Delete • Delete the node as in a BST • Starting at the newly inserted node, travel up the tree (towards the root), updating the balances along the way. • If the tree becomes unbalanced, decide which rotation needs to be performed, rotate the tree and update the balances. • Keep traveling towards the root, checking the balances. You may need to rotate again.
AVL Trees: Insert/Delete • Insert • maximum possible number of rotations = 1 • Delete • maximum possible number of rotations = lgn • Worst case times • search: O(lgn) • insert: O(lgn) • delete: O(lgn) • one rotation: O(1)
AVL Trees: Efficiency • It can be shown that the worst case height of an AVL tree is at most 44% larger than the minimum possible for a BST (i.e. approximately 1.44lgn)
Other balanced trees • Red-black trees • Each node has an extra bit to hold a color • The tree stays balanced by placing restrictions on the way the nodes are colored • Every path from the root to a leaf has the same number of black nodes and there cannot be consecutive red nodes. • A red-black tree is balanced when the longest path from the root to a leaf is at most twice the length of the shortest path from the root to a leaf. • A red-black tree with n nodes has height at most 2lg(n+1)
Other balanced trees • α-balanced trees • α is a constant between 1/2 and 1 • A tree is α-balanced if, for every node x:size[left(x)] α·size[x]size[right(x)] α·size[x]