Chapter 6. Transform and Conquer Algorithms Binary Search Trees. AVL Trees CmSc 20 Intro to Algorithms 1. Binary Search Trees The left subtree of each node contains values that are smaller than the value in the given node. The right subtree of each node contains values that are greater than the value in the given node. Operations: 1. Search - compare the values and proceed either to the left or to the right. 2. Insert: unsuccessful search - insert the new node at the bottom where the search has stopped.. Delete: replace the value in the node with the smallest value in the right subtree or the largest value in the left subtree. Creating a binary search tree: Initially the tree is empty. The first element to be stored is placed in a node that would be the root of the tree. Each next element is placed through the "unsuccessful search" procedure. Retrieving the elements in sorted order - by in-order traversal Complexity of search in a binary tree: O(logN) The search at worst will proceed to the leaves, down a path in the tree. On average the height of a binary tree with N elements is O(sqrt(N)). The worst case is when the items to be inserted come sorted, then the tree degenerates to a list: C O S T 1
The tree is not balanced. Disadvantages of Binary search trees: The shape of the tree depends on the order of insertions, can be degenerated When inserting or searching for an element, the key of each visited node has to be compared with the key of the given element. Keys may be long and the run time may increase much. Solution to the problems Decreasing the height of the tree: Balanced trees: AVL Trees (Adelson-Velskii and Landis) Balance condition: the height of the tree to be O(logN) the left and the right subtrees of each node can differ by at most 1 It can be proved that if this condition is observed the height of the tree is O(logN). Decreasing the time for comparison: Digital trees: Insert and retrieve keys according to their bit representation. The length of the path is at most the length of the bit representation of the key. nodes Radix trees: solve the problem with long keys. Keys are stored at the external 2. AVL trees (Adelson-Velskii and Landis) Definition: An AVL tree is a binary search tree in which, for every node, the difference between the heights of its left and right subtrees, called the balance factor, is at most 1 (with the height of an empty tree defined as -1) Balance condition: the height of the tree to be O(logN) The left and the right subtrees of each node can differ by at most 1. Here we consider the height of a non-existing subtree to be -1. It can be proved that if this condition is observed the height of the tree is O(logN). Rotation of nodes When a new node is inserted the balance condition might be violated. We examine the nodes along the path from the new node to the root. If we find a node that has become 2
unbalanced one of its subtrees may have become longer by 2 than the other. We shall call this subtree - violating subtree, and the node which has this subtree - violated node. To restore the balance we rotate the nodes: Two cases are considered: 1. The new node is inserted to the same side, as the violating subtree. single rotation 2. The side of the new node is different from the side of the violating subtree double rotation Case 1 Violated node Violating subtree Case 2 Violated node 7 Violating subtree In the examples above, the violating subtree rooted at node is the right subtree of node. In case 1, the new inserted node is in the right subtree of node In case 2, the new inserted node 7 is in the left subtree of node
Case 1: Single L-rotation: the violating subtree is on the right side of the violated node (The R-rotation is symmetrical to the L-rotation - the violating subtree is on the left side of the violated node) The violated node has to be lowered, and the root of the violating tree - moved up. Links to be changed The tree after the rotation: 9 Rearranged links 6 4
Two basic rules: The left subtree of a right-sided node to be moved up is attached as a right subtree of the node that goes down The right subtree of a left-sided node to be moved up is attached as a left subtree of the node that goes down Case 2: Double RL- rotation (Double LR-rotation is symmetrical) Consider the violating subtree (rooted at node ) and its left subtree (rooted at node 6) where the new node is inserted. First we rotate the two roots node and node 6. Now the longer subtree is on the right side and we apply single rotation rotate node and node 6. : Below is the result of the first rotation: 6 6 9 7 7 9 In the second rotation node 6 goes up. Splay trees - Move to the top each accessed node with rotations, decreasing the height of the tree. Exercise: 1. Create a binary search tree for COMPUTERLAND. It will be unbalanced. 2. Create an AVL tree with COMPUTERLAND - rotating the nodes on insertion if necessary to keep the tree balanced.