Presentation is loading. Please wait.

Presentation is loading. Please wait.

AVL Tree.

Similar presentations


Presentation on theme: "AVL Tree."— Presentation transcript:

1 AVL Tree

2 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.

3 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.

4 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.

5 A completely balanced tree
In a completely balanced tree, the left and right subtrees of any node would have the same height.

6 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.

7 Tree Balancing

8 Tree Balancing i.e. DE, GA, IL, IN, MA, MI, NY, OH, PA, RI, TX, VT, WY
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!

9 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 factor of node x to be the height of x’s left subtree minus 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

10 Sample trees

11 Sample trees

12 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 `\.'

13 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.

14 Keeping AVL trees balanced

15 Keeping AVL trees balanced

16 Rotations of an AVL Tree
When right_tree is right higher, is illustrated in Figure 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.

17 Rotations of an AVL Tree Fig-10.19

18 Double Rotation

19 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 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).

20 Examples of insertions requiring single and double rotations

21 Rebalancing cost/benefit

22 Analysis of AVL Trees The sparsest possible AVL tree with n nodes has height about 1.44 lg n compared 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.

23 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


Download ppt "AVL Tree."

Similar presentations


Ads by Google