240 likes | 476 Views
AVL Tree. Recursive definition of AVL Tree. An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are again AVL trees. History of AVL Trees.
E N D
Recursive definition of AVL Tree An AVL tree is a binary search tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are again AVL trees.
History of AVL Trees “To optimize search times by keeping the tree very nearly balanced at all times.” • The method for achieving this goal was described in 1962 by two Russian mathematicians, G. M. ADEL’SON-VEL’SKII and E. M. LANDIS, and the resulting binary search trees are called AVL trees in their honor.
AVL Trees • AVL trees achieve the goal that searches, insertions, and removals in a tree with n nodes can all be achieved in time that is O(log n), even in the worst case. • The height of an AVL tree with n nodes, as we shall establish, can never exceed 1.44 lg n, and thus even in the worst case, the behavior of an AVL tree could not be much below that of a random binary search tree. • In almost all cases, however, the actual length of a search is very nearly lg n, and thus the behavior of AVL trees closely approximates that of the ideal, completely balanced binary search tree.
A completely balanced tree In a completely balanced tree, the left and right subtrees of any node would have the same height.
Tree Balancing Binary search trees designed for fast searching!! • But the order of insertion into a binary search tree determines the shape of the tree and hence the efficiency with which tree can be searched. • If it grows so that the tree is as “fat or bushy” as possible (fewest levels) then it can be searched most efficiently. • However, if the tree grows “as a long chain”, with many more items in one subtree than another, then the search time degrades from logarithmic to linear. • Optimally, a BST with n nodes should have log2n levels. • In the worst case, there could be n levels (one node per level), in which binary search degenerates to sequential search.
Tree Balancing If the abbreviations were inserted alphabetically, • i.e. DE, GA, IL, IN, MA, MI, NY, OH, PA, RI, TX, VT, WY • DE would be the root, GA its right child, IL the right child of GA, IN the right child of IN, etc. • BST degenerates into a linked list • search time increases to O(n) • Need a way to keep BST height-balanced!
AVL Trees An AVL tree is a binary tree that is height-balanced: • The difference in height between the left and right subtrees at any point in the tree is restricted. • Define the balance factorof node x to be the height of x’s left subtreeminus the height of its right subtree. • An AVL tree is a BST in which the balance factor of each node is 0, -1, or 1
Convention in diagrams: In drawing diagrams, we shall show a left-higher node by `/,' a node whose balance factor is equal by ` - ,' and a right-higher node by `\.'
Keeping AVL trees balanced When a new item inserted into a balanced binary tree • resulting tree may become unbalanced • tree can be rebalanced • transform subtree rooted at the node that is the nearest ancestor of the new node unacceptable balance factor • This transformation carried out by rotation: the relative order of nodes in a subtree is shifted, changing the root, and the number of nodes in the left and the right subchildren. • Four standard types of rotations are performed on AVL trees.
Rotations of an AVL Tree When right_tree is right higher, is illustrated in Figure 10.19. The action needed in this case is called a left rotation; we have rotated the node right_tree upward to the root, dropping root down into the left subtree of right_tree; the sub-tree T2 of nodes with keys between those of root and right_tree now becomes the right subtree of root rather than the left subtree of right_tree.
Double Rotation When the balance factor of right_tree is left higher, is slightly more complicated. It is necessary to move two levels, to the node sub_tree that roots the left subtree of right_tree, to find the new root. This process is shown in Figure 10.20 and is called a double rotation, because the transformation can be obtained in two steps by first rotating the subtree with root right_tree to the right (so that sub_tree becomes its root), and then rotating the tree pointed to by root to the left (moving sub_tree up to become the new root).
Examples of insertions requiring single and double rotations
Analysis of AVL Trees • The sparsest possible AVL tree with n nodes has height about 1.44 lg ncompared to: • A perfectly balanced binary search tree with n nodes has height about lg n. • A random binary search tree, on average, has height about 1.39 lg n. • A degenerate binary search tree has height as large as n.
Analysis of AVL Trees Hence the algorithms for manipulating AVL trees are guaranteed to take no more than about 44 percent more time than the optimum. In practice, AVL trees do much better than this on average, perhaps as small as lg n + 0.25.