11.07.2015 Views

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

Data Structures and Algorithm Analysis - Computer Science at ...

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Sec. 13.2 Balanced Trees 437th<strong>at</strong> is found to be unbalanced. Deletion is similar; however, consider<strong>at</strong>ion forunbalanced nodes must begin <strong>at</strong> the level of the deletemin oper<strong>at</strong>ion.Example 13.3 In Figure 13.4 (b), the bottom-most unbalanced node hasvalue 7. The excess node (with value 5) is in the right subtree of the leftchild of 7, so we have an example of Case 2. This requires a double rot<strong>at</strong>ionto fix. After the rot<strong>at</strong>ion, 5 becomes the left child of 24, 2 becomes the leftchild of 5, <strong>and</strong> 7 becomes the right child of 5.13.2.2 The Splay TreeLike the AVL tree, the splay tree is not actually a distinct d<strong>at</strong>a structure, but r<strong>at</strong>herreimplements the BST insert, delete, <strong>and</strong> search methods to improve the performanceof a BST. The goal of these revised methods is to provide guarantees on thetime required by a series of oper<strong>at</strong>ions, thereby avoiding the worst-case linear timebehavior of st<strong>and</strong>ard BST oper<strong>at</strong>ions. No single oper<strong>at</strong>ion in the splay tree is guaranteedto be efficient. Instead, the splay tree access rules guarantee th<strong>at</strong> a seriesof m oper<strong>at</strong>ions will take O(m log n) time for a tree of n nodes whenever m ≥ n.Thus, a single insert or search oper<strong>at</strong>ion could take O(n) time. However, m suchoper<strong>at</strong>ions are guaranteed to require a total of O(m log n) time, for an average costof O(log n) per access oper<strong>at</strong>ion. This is a desirable performance guarantee for anysearch-tree structure.Unlike the AVL tree, the splay tree is not guaranteed to be height balanced.Wh<strong>at</strong> is guaranteed is th<strong>at</strong> the total cost of the entire series of accesses will becheap. Ultim<strong>at</strong>ely, it is the cost of the series of oper<strong>at</strong>ions th<strong>at</strong> m<strong>at</strong>ters, not whetherthe tree is balanced. Maintaining balance is really done only for the sake of reachingthis time efficiency goal.The splay tree access functions oper<strong>at</strong>e in a manner reminiscent of the moveto-frontrule for self-organizing lists from Section 9.2, <strong>and</strong> of the p<strong>at</strong>h compressiontechnique for managing parent-pointer trees from Section 6.2. These accessfunctions tend to make the tree more balanced, but an individual access will notnecessarily result in a more balanced tree.Whenever a node S is accessed (e.g., when S is inserted, deleted, or is the goalof a search), the splay tree performs a process called splaying. Splaying moves Sto the root of the BST. When S is being deleted, splaying moves the parent of S tothe root. As in the AVL tree, a splay of node S consists of a series of rot<strong>at</strong>ions.A rot<strong>at</strong>ion moves S higher in the tree by adjusting its position with respect to itsparent <strong>and</strong> gr<strong>and</strong>parent. A side effect of the rot<strong>at</strong>ions is a tendency to balance thetree. There are three types of rot<strong>at</strong>ion.A single rot<strong>at</strong>ion is performed only if S is a child of the root node. The singlerot<strong>at</strong>ion is illustr<strong>at</strong>ed by Figure 13.7. It basically switches S with its parent in a

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!