410 likes | 634 Views
Height-Balanced Binary Search Trees. AVL Trees. Background. Binary Search Trees allow dynamic allocation (like linked lists), but O(log 2 (n)) average case search time (like arrays). Problem: they still have O(n) worst case search time.
E N D
Height-Balanced Binary Search Trees AVL Trees
Background • Binary Search Trees allow dynamic allocation (like linked lists), but O(log2(n)) average case search time (like arrays). • Problem: they still have O(n) worst case search time. • Solution: if we can balance the trees on insert, we can have worst case O(log2(n)) search time.
AVL Trees • Named for inventors Adelsson, Velski, and Landis. • One way to maintain a balanced tree. • Idea: keep the height of every node in the tree. • When the difference between the height of a node’s left and right subtrees exceeds 1, rotate the nodes to eliminate the imbalance.
AVL Details • Each node will have an additional piece of information – its height. It is calculated as: • H(node) = MAX(H(left), H(right)) + 1 • Each node’s balance factor will be calculated when needed; the formula is • BF(node) = H(left) – H(right) • When BF(node) = +2 or –2, a fix is needed.
AVL Algorithm • Insert a new key value in the normal way • Call the newly inserted node “newnode” • Set H(newnode) = 1; • For each node from newnode back to root • Recalculate H(node) and BF(node) • If BF(node) = +2 or BF(node) = –2 • Perform appropriate rotation • Once a rotation is performed, stop.
AVL Rotations • The appropriate rotation depends on where “newnode” is with respect to the node that is out of balance. • The four types are • Right-Right (newnode was inserted into the right subtree of the right subtree of the node) • Left-Left • Left-Right • Right-Left
AVL Rotations II • The rotations are usually specified in general, since a rotation can occur anywhere in the tree. • Subtrees move as whole units • Note in the following examples that the height of the tree after rotation is the same as before the insertion. • This means that no heights or balance factors above this node will change.
AVL Tree: LL Rotation H : h+2 BF : +1 A • This is the general tree set up. • Now suppose a new node is inserted into BL that increases its height: H : h+1 BF : 0 B AR h BL BR h
AVL Tree: LL Rotation H : h+2 BF : +2 A • BF of node A is now +2, so a rotation is necessary: H : h+2 BF : +1 B AR h BL BR h New
AVL Tree: LL Rotation H : h+2 BF : +0 B • This is the result of the rotation. • Note the height of the root is the same as before the insertion. H : h+1 BF : 0 A BL h BR AR h New
LL Rotation Algorithm • Set up pointers for A, B, BL, BR, AR • A->left = BR • B->right = A • Root = B • Set H and BF for nodes A and B. • NOTE: the book does this more “cleverly”, with fewer pointers. I think this is more clear.
RR Rotation H : h+2 BF : -1 A • This is the general tree set up. • Now suppose a new node is inserted into BR that increases its height: H : h+1 BF : 0 B AL h BL BR h
RR Rotation H : h+3 BF : -2 A • BF of node A is –2, so a rotation is needed H : h+2 BF : -1 B AL h BL BR h New
RR Rotation H : h+2 BF : 0 B • This is the result of the rotation. • Note the height of the root returns to h+2 H : h+1 BF : 0 A BR h AL BL h New
RR Rotation Algorithm • Set up pointers for A, B, BL, BR, AL • A->right = BL • B->left = A • Root = B • Set H and BF for nodes A and B.
AR h LR Rotation H : h+2 BF : +1 A • This is the general tree set up. • Now suppose a new node is inserted into CL that increases its height: H : h+1 BF : 0 B H : h BF : 0 C BL h CL CR h–1
AR h LR Rotation H : h+3 BF : +2 A • The balance factor of A goes to +2, so a left-right rotation is needed. H : h+2 BF : -1 B H : h+1 BF : +1 C BL h CL CR h–1 New
AR h LR Rotation H : h+2 BF : 0 C • Again, note the height of the root returns to h+2. H : h+1 BF : 0 B H : h+1 BF : -1 A BL CL CR h–1 h New
LR Rotation Algorithm • Set up pointers for A, B, C, BL, AR, CL, CR • A->left = CR • B->right = CL • C->left = B • C->right = A • Root = C • Set H and BF for nodes A, B and C.
BR AL h h RL Rotation H : h+2 BF : -1 A • This is the general tree set up. • Now suppose a new node is inserted into CL that increases its height: H : h+1 BF : 0 B H : h BF : 0 C CL CR h–1
BR AL h h RL Rotation H : h+3 BF : -2 A • BF of Node A is now –2, so a rotation is necessary. H : h+2 BF : +1 B H : h+1 BF : +1 C CL CR h–1 New
BR AL h h RL Rotation H : h+2 BF : 0 • This shows the result of the rotation. C H : h+1 BF : -1 H : h+1 BF : 0 A B CL CR h–1 New
RL Rotation Algorithm • Set up pointers for A, B, C, BR, AL, CL, CR • A->right = CL • B->left = CR • C->left = A • C->right = B • Root = C • Set H and BF for nodes A, B and C.
BR AL h h RL Rotation Method 2 H : h+3 BF : -2 A • First, Perform a LL rotation around B: H : h+2 BF : +1 B H : h+1 BF : +1 C CL CR h–1 New
BR AL h h RL Rotation Method 2 H : h+3 BF : -2 A • Next, perform a RR Rotation around A: H : h+2 BF : -1 C H : h+1 BF : -1 CL B h–1 CR New
BR AL h h RL Rotation Method 2 H : h+2 BF : 0 • This result is the same as the first method. C H : h+1 BF : -1 H : h+1 BF : 0 A B CL CR h–1 New
AVL Example • Now let’s do an extended example, inserting a series of key values into an AVL tree and rotating as necessary. • We will be inserting 25, 50, 90, 10, 15, 20, 75. • Let’s start with 25:
AVL Example H : 1 BF : 0 25 • 25 is a new root; no problem. • Now, insert 50: Left to insert: 50, 90, 10, 15, 20, 75
AVL Example H : 2 BF : -1 25 • No problem here. • Now, insert 90: 50 H : 1 BF : 0 Left to insert: 90, 10, 15, 20, 75
AVL Example H : 3 BF : -2 25 • BF(25) = –2; what do we do? • This is a RR rotation: 50 H : 2 BF : -1 90 H : 1 BF : 0 Left to insert: 10, 15, 20, 75
AVL Example H : 2 BF : 0 50 • This is the result; note height of the root. • Next, insert 10: H : 1 BF : 0 25 90 H : 1 BF : 0 Left to insert: 10, 15, 20, 75
AVL Example H : 3 BF : +1 50 • No problems here. • Next, insert 15: H : 2 BF : +1 25 90 H : 1 BF : 0 H : 1 BF : 0 10 Left to insert: 15, 20, 75
AVL Example H : 3 BF : +1 50 • BF(25)=+2, so time to rotate • This is a Left-Right (LR) rotation: • (Note that I didn’t update 50) H : 3 BF : +2 25 90 H : 1 BF : 0 H : 2 BF : -1 10 15 H : 1 BF : 0 Left to insert: 20, 75
AVL Example H : 3 BF : +1 50 • This shows the result. • Note that I didn’t need to update 50… • Now, insert 20: H : 2 BF : 0 15 90 H : 1 BF : 0 H : 1 BF : 0 10 25 H : 1 BF : 0 Left to insert: 20, 75
AVL Example H : 4 BF : +2 50 • BF(50) = +2, so time to rotate… • This is a left right (LR) rotation again: H : 3 BF : -1 15 90 H : 1 BF : 0 H : 1 BF : 0 10 25 H : 2 BF : +1 20 H : 1 BF : 0 Left to insert: 75
AVL Example H : 3 BF : 0 25 • This is the result. Note the movements of the subtrees. • Finally, insert 75: H : 2 BF : 0 15 50 H : 2 BF : -1 H : 1 BF : 0 10 20 H : 1 BF : 0 90 H : 1 BF : 0 Left to insert: 75
AVL Example H : 3 BF : 0 25 • H(50) = –2, so time to rotate again. • This is a right-left (RL) rotation: H : 2 BF : 0 15 50 H : 3 BF : -2 H : 1 BF : 0 10 20 H : 1 BF : 0 90 H : 2 BF : +1 75 H : 1 BF : 0 Left to insert: done
AVL Example H : 3 BF : 0 25 • This is the result. • Done. H : 2 BF : 0 15 75 H : 2 BF : 0 H : 1 BF : 0 10 20 H : 1 BF : 0 H : 1 BF : 0 50 90 H : 1 BF : 0 Left to insert: done