










Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!











Definition o All keys are distinct – why? o Key in the root of the left subtree is less than the root. o Key in the root of the right subtree is greater than the root. o Left and right subtrees are binary search trees. Figure 1 is an example: Figure 1 Binary Search Tree Where would you add X, B, and E? At seats, build tree from the nodes: B R L M T C A N P D What about deletion? o Since it is easier to delete from a leaf, we first replace the node with its inorder successor, and then delete the node that contained the inorder successor. The code for insertion is as follows. I like to create the new node outside the insert routine.
#include
class BinarySearchTree { protected: TreeNode
o Is it an AVL search tree? Figure 2 Possible AVL Trees and AVL Search Trees Properties of AVL trees: o The height of an AVL tree with n elements/node is ^ (log^ n ). o For every value of n , n 0 , there exists an AVL tree. o An n -element AVL search tree can be searched in ^ (^ height^ )(log n )time. o A new element can be inserted into an n -element AVL search tree so that the result is an n^ ^1 element AVL tree and such an insertion can be done in (log n )time. o An element can be deleted from an n -element AVL search tree so that the result is an n 1 element AVL tree and such a deletion can be done in (log^ n )time. From here on out, when I refer to an AVL tree, I mean an AVL search tree. The bound in the height of an AVL tree can be shown by the recursive routine shown below: o worst_tree (h) = /* Worst tree of height h */ o root left = worst_tree(h-1) o root right = worst_tree(h-2)
How is this done? (Same way as with the binary search tree.)
Insertions – just like BST, find the leaf where the node needs to go. After that node is placed in the AVL tree, the tree is rebalanced if necessary. We need to have a balance factor associated with each node x in the AVL tree. height of left subtree of x – height of right subtree of x See Figure 3 for an example of inserting 32 into an AVL tree and the subsequent rebalancing. Notice that the balance factor is contained in each of the nodes shown in the tree.
Figure 3 Example of Inserting into an AVL-Tree Observations about the unbalanced tree that results from an insertion. o In the unbalanced tree, the balance factors are limited to –2, –1, 0, 1, and 2. o A node with balance factor 2 has a balance factor of 1 before the insertion. Similarly, a node with balance factor –2, has a balance factor of –1 before the insertion. o The balance factors of only those nodes on the path from the root to the newly inserted node can change as a result of the insertion. o Let A denote the nearest ancestor of the newly inserted node whose balance factor is either –2 or 2. The balance factor of all nodes on the path from A to the newly inserted node was 0 prior to the insertion. In Figure 3(b), what is A? (Node 40.) There are four cases have to be consider when inserting into an AVL tree. We categorize these starting from A. o Inserted node is in the left subtree of A. New node is a left subtree of the left subtree of A – call LL rotation. New node is a right subtree of the left subtree of A – called LR rotation. o Inserted node is in the right subtree of A. New node is a left subtree of the right subtree of A – call RL rotation. New node is a right subtree of the right subtree of A – called RR rotation. The RR and RL rotations are symmetric to the LL and LR so we don’t need to discuss them. A generic LL type imbalance appears in Figure 4. Figure 4 LL Rotation
Figure 7 AVL tree with states
Deletions: Replace with inorder successor, delete, and rebalance. An R0 imbalance at A is rectified by performing the rotation shown in Figure 8. Figure 8 R0 Rotation Figure 9 shows how to handle an R1 imbalance. Figure 9 R1 Rotation
Following an R1 rotation, we must continue to examine nodes on the path to the root. Unlike the case of an insertion, one rotation may not suffice to restore balance following a deletion. The number of rotations needed is (log^ n ). The transformation needed when the imbalance is of type R–1 appears in Figure 10. Figure 10 R–1 Rotation R-1 rotations may possibly need to continue to the root. See the LR rotation for the definition of b. What happens if AVL property is extended so left and right differ by 2 or 3 or K? o Termed height balanced tree, HB[ K ]. o As K increases, worst case height increases, but time to rebalance is less. o There are symmetric rotations Consider the tree shown in Figure 11. We want to delete node 60 Figure 11 Delete node 60 Consider the tree shown in Figure 12. We want to delete node 70.
Splay trees support all the operations of binary trees. But they do not guarantee (log^ N )worst-case performance. Instead, its bounds are amortized, meaning that although individual operations can be expensive, any sequence of operations is guaranteed to behave as if each operation in the sequence exhibited logarithmic behavior.
Limitations of balanced search trees: o Balanced search trees require storing an extra piece of information per node. o They are complicated to implement, and as a result insertions and deletions are expensive and potentially error-prone. o We do not win when easy inputs occur. Balanced search trees have a second feature that seems improvable: o Their worst-case, average-case, and best-case performance are essentially identical. o It would be nice if the second access to the same piece of data was cheaper than the first. o The 90-10 rule – empirical studies suggest that in practice 90% of the accesses are to 10% of the data items. o It would be nice to get easy wins for the 90% case.
Given the above information, what can we do? Since the time to access in a binary tree is proportional to the depth of the accessed node, we can attempt to restructure the tree by moving most recently accessed items toward the root. Even if there are intervening operations, we would expect the node to remain close to the root and thus be quickly found. We could call this strategy the rotate-to-root strategy. Ends up having a O(log n) time bound for non-uniform access. For uniform access is actually worse than balanced trees. An application of the rotate-to-root strategy to node 3 is shown in Figure 13.
Figure 13 Rotate-to-Root When nodes are rotated to the root, other nodes go to lower levels in the tree. If we do not have the 90-10 rule, it is possible for a long sequence of bad accesses to occur.
A technique called splaying can be used so a logarithmic amortized bound can be achieved. We use rotations such as we’ve seen before. When possible, we want to move the node up two levels. The zig case. o Let X be a non-root node on the access path on which we are rotating. o If the parent of X is the root of the tree, we merely rotate X and the root as shown in Figure 14. Figure 14 Zig Case o This is the same as a normal single rotation. The zig-zag case. o In this case, X and both a parent P and a grandparent G. X is a right child and P is a left child (or vice versa). o This is the same as a double rotation. o This is shown in Figure 15.
Figure 17 Example of Splay Tree
Insertion – when an item is inserted, a splay is performed. o As a result, the newly inserted item becomes the root of the tree. Find – the last node accessed during the search is splayed o If the search is successful, then the node that is found is splayed and becomes the new root. o If the search is unsuccessful, the last node accessed prior to reaching the NULL pointer is splayed and becomes the new root. FindMin and FindMax – perform a splay after the access. DeleteMin and DeleteMax – these are important priority queue operations. o DeleteMin Perform a FindMin. This brings the minimum to the root, and by the binary search tree property, there is no left child. Use the right child as the new root and delete the node containing the minimum. o DeleteMax
Peform a FindMax. Set the root to the post-splay root’s left child and delete the post-splay root. Remove o Access the node to be deleted bringing it to the root. o Delete the root leaving two subtrees L (left) and R (right). o Find the largest element in L using a FindMax operation, thus the root of L will have no right child. (This is similar to replacing root with inorder predecessor) o Make R the right child of L’s root. o As an example, see Figure 18 that deletes 6. Figure 18 Remove from Splay Tree One bad operation is to find all the nodes in order.
Bottom-up splay trees are hard to implement. We look at top-down splay trees that maintain the logarithmic amortized bound. o This is the method recommended by the inventors of splay trees. Basic idea – as we descend the tree in our search for some node X, we must take the nodes that are on the access path, and move them and their subtrees out of the way. We must also perform some tree rotations to guarantee the amortized time bound. At any point in the middle of a splay, we have: o The current node X that is the root of its subtree. o Tree L that stores nodes less than X. o Tree R that stores nodes larger than X. Initially, X is the root of T, and L and R are empty.
Figure 21 Zig-Zag Notice that the zig-zag is simplified. o Making Y the root simplifies the coding because the action for the zig-zag becomes identical to the zig case and would be advantageous, as testing for a host of cases is time-consuming. o The disadvantage is that a descent of only one level results in more iterations in the splaying procedure. The only thing we have to do now is reassemble the splay tree. This is shown in Figure 22 Figure 22 Reassembling See Figure 23 for an example of these rotations when accessing 18.
Figure 23 Example 1 of Splaying Another example when finding c is shown in Figure 24.