Binary Search and AVL Trees
Binary Search and AVL Trees
Brown
Adapted
from: Goodrich and Tamassia, Data Structures and Algorithms in Java, John Wiley & Son (1998).
25 September, 1999
25 September, 1999
Search
Algorithm: TreeSearch( k, v ) Input: A search key, k, and a node, v, of a binary search tree, T. Output: The node storing the key, k, or an external node (after all internal nodes with keys < k and before all internal nodes with keys > k). if v is an external node then return v if k = key( v ) then // node found return v else if k < key( v ) then return TreeSearch( k, T.leftChild( v ) ) else // k > key( v ) return TreeSearch( k, T.rightChild( v ) )
For this algorithm, the returned node requires one final test for an external node ( v.isExternal() ).
25 September, 1999 3
25 September, 1999
expandExternal()
A new Item will always be added at the bottom of the search tree, T. Each node visit costs O(1), thus, in the worst case, insert runs in O(h), the height of the tree, T.
5
25 September, 1999
25 September, 1999
25 September, 1999
To prevent the worst-case running time, a re-balancing scheme is needed to yield a tree that maintains the smallest possible height.
25 September, 1999
AVL Trees
An AVL Tree is a binary tree such that for every internal node v of T, the heights of the children of v can differ by at most 1.
Every
25 September, 1999
A binary search tree T is balanced if for every node v, the height of vs children differ by at most one. Inserting a node into an AVL tree involves performing an expandExternal( w ) on T, which may change the height of some nodes in T. If an insertion causes T to become unbalanced, then travel up the tree from the newly created node, w, until the first node is reached, x, that has an unbalanced grandparent, z. Note that x could be equal to w.
Height( y ) = Height( sibling( y ) ) + 2
z y
w Expand external at w.
25 September, 1999 10
In order to rebalance the subtree rooted at z, a restructuring is required. Rename x, y, and z to a, b, and c based on the order of the nodes in an in-order traversal. Let T0, T1, T2 and T3 be the subtrees located at x, y, and z. Four geometric structures are possible:
25 September, 1999
11
Restructuring
Single Rotation Replace the subtree rooted at z with a new subtree rooted at b. Let a be the left child of b, such that T0 and T1 are left and right subtrees of a. Let c be the right child of b, such that T2 and T3 are left and right subtrees of c.
25 September, 1999
Rotate y over to z.
12
Restructuring
Double Rotation Replace the subtree rooted at z with a new subtree rooted at b. Let a be the left child of b, such that T0 and T1 are left and right subtrees of a. Let c be the right child of b, such that T2 and T3 are left and right subtrees of c.
Removing a Node
Node has a single leaf Child
Remove the internal node above w by performing removeAboveExternal( w ), which may result in an unbalanced Tree. Let z be the first unbalanced node encountered while travelling up the tree from w. Let y be the child of z with the larger height, and let x be the child of y with the larger height. Perform a restructure on x to restore the balance at the subtree rooted at z. As this restructuring may upset the balance higher in the tree, continue to check for balance until the root of T is reached.
w
25 September, 1999 14
Removing a Node
Examples
25 September, 1999
The two nodes with values 50 and 78 both have a height of 2. In the first example, the right subtree is assigned x, in the second example the left subtree is assigned x.
15
An AVL Node extends the Item class to provide a variable for the node height.
public class AVLItem extends Item { private int height; public AVLItem( Object k, Object e, int h ) { super(k, e); height = h; } public int height() { return height; } public int setHeight( int h ) { int oldHeight = height; height = h; return oldHeight; } }
25 September, 1999
16
private Position tallerChild( Position p ) { // return a child of p with height no smaller than that of the other child if( height( T.leftChild(p) ) >= height( T.rightChild(p) ) ) return T.leftChild( p ); else return T.rightChild( p ); } private void rebalance( Position zPos ) { // traverse the path of T from zPos to the root; // for each node encountered, recompute its height and // perform a rotation if unbalanced
while ( !T.isRoot( zPos ) ) { zPos = T.parent( zPos ); setHeight( zPos ); if ( !isBalanced( zPos ) ) // perform a rotation { Position xPos = tallerChild( tallerChild( zPos ) ); zPos = ((RestructurableNodeBinaryTree) T).restructure( xPos ); setHeight( T.leftChild( zPos ) ); setHeight( T.rightChild( zPos ) ); setHeight( zPos ); } } }
25 September, 1999 18
Summary
A Binary Search Tree orders keys such that for any node, the left subtree has smaller key values, and the right subtree has larger key values. In the worst case, a Binary Search Tree may have height n, the number of nodes, and the running time is O(n). In the best case, the height is h = log (n+1) and the running time is O( log(n +1) ). In an AVL tree, the heights of the children can differ by at most 1. An AVL insertion/deletion may require a single or double rotation of nodes to maintain the height-balance property. The procedure for deleting a node from an AVL tree is the same as deleting a node from a Binary Search Tree followed by a restructuring if necessary. An AVL insert/delete runs in O( log n ) time.
20
25 September, 1999