Chapter 3, Sections 1 and 2: Binary Search Trees AVL Trees 6 < 2 1 9 > 4 = Binary Search Trees 8 1 Ordered Dictionaries Keys are assumed to come from a total order. As opposed to the Ch. 2 stuff where the keys were not necessarily totally ordered. New operations: closestKeyBefore(k) closestElemBefore(k) closestKeyAfter(k) closestElemAfter(k) Binary Search Trees 2 Our Theme… All of the data structures in Chapter 3 are useful for implementing ordered dictionaries. Typically, we are looking at variations on an ordered tree Want searching to be efficient Need to maintain the properties that make searching efficient, so inserting and removing elements can be complicated Binary Search Trees 3 First, a general strategy and a simple (though not really efficient) implementation…. Binary Search Trees 4 Binary Search Binary search performs operation findElement(k) on a dictionary implemented by means of an array-based sequence, sorted by key similar to looking through a phone book for a name at each step, the number of candidate items is halved terminates after O(log n) steps Example: findElement(7) 0 1 3 4 5 7 1 0 3 4 5 m l 0 9 11 14 16 18 19 m l 0 8 1 1 3 3 7 h 8 9 11 14 16 18 19 8 9 11 14 16 18 19 8 9 11 14 16 18 19 h 4 5 7 l m h 4 5 7 l=m =h Binary Search Trees 5 Lookup Table A lookup table is a dictionary implemented by means of a sorted sequence We store the items of the dictionary in an array-based sequence, sorted by key We use an external comparator for the keys Performance: findElement takes O(log n) time, using binary search insertItem takes O(n) time since in the worst case we have to shift n items to make room for the new item removeElement take O(n) time since in the worst case we have to shift nitems to compact the items after the removal The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations) Binary Search Trees 6 Now for the Data Structures Pretty much all variations on the binary search tree theme Binary Search Trees 7 Binary Search Tree A binary search tree is a binary tree storing keys (or key-element pairs) at its internal nodes and satisfying the following property: Let u, v, and w be three nodes such that u is in the left subtree of v and w is in the right subtree of v. We have key(u) key(v) key(w) An inorder traversal of a binary search trees visits the keys in increasing order 6 2 1 9 4 8 External nodes do not store items Binary Search Trees 8 Search Algorithm findElement(k, v) To search for a key k, if T.isExternal (v) we trace a downward return NO_SUCH_KEY path starting at the root if k < key(v) The next node visited return findElement(k, T.leftChild(v)) depends on the else if k = key(v) outcome of the return element(v) comparison of k with else { k > key(v) } the key of the current return findElement(k, T.rightChild(v)) node 6 If we reach a leaf, the < key is not found and we 2 9 > return NO_SUCH_KEY 8 Example: 1 4 = findElement(4) Binary Search Trees 9 Insertion (Nice case) To perform operation insertItem(k, o), we search for key k Assume k is not already in the tree, and let let w be the leaf reached by the search We insert k at node w and expand w into an internal node (i.e. we add two external children to w) Example: insert 5 6 < 2 9 > 1 4 8 > w 6 2 1 Binary Search Trees 9 4 8 w 5 10 Insertion (Not so nice case) w If k is already in the tree, and let let w be the node at which the key k is first encountered We call TreeSearch(k.leftChild(w)) and recursively apply algorithm to node returned. Example: insert 2 6 < 2 9 1 4 6 w < 1 8 2 > 9 4 8 2 Binary Search Trees 11 Deletion To perform operation removeElement(k), we search for key k Assume key k is in the tree, and let let v be the node storing k If node v has a leaf child w, we remove v and w from the tree with operation removeAboveExternal(w) Example: remove 4 6 < 2 9 > 4 v 1 8 w 5 6 2 1 Binary Search Trees 9 5 8 12 Next node in inorder traversal leftmost node in subtree rooted at right child Deletion (cont.) We consider the case where the key k to be removed is stored at a node v whose children are both internal we find the internal node w that follows v in an inorder traversal we copy key(w) into node v we remove node w and its left child z (which must be a leaf) by means of operation removeAboveExternal(z) Example: remove 3 1 v 3 2 8 6 w 5 z 1 v 5 2 8 6 Binary Search Trees 9 9 13 Performance Consider a dictionary with n items implemented by means of a binary search tree of height h the space used is O(n) methods findElement , insertItem and removeElement take O(h) time The height h is O(n) in the worst case and O(log n) in the best case Binary Search Trees 14 AVL Trees 6 v 8 3 z 4 Binary Search Trees 15 AVL Tree Definition AVL trees are balanced. An AVL Tree is a 44 2 binary search tree 17 78 1 3 2 32 such that for every internal node v of T, the heights of the children of v can differ by at most 1. 4 88 50 1 48 62 1 An example of an AVL tree where the heights are shown next to the nodes: Binary Search Trees 16 1 n(2) Height of an AVL Tree 3 4 n(1) Fact: The height of an AVL tree storing n keys is O(log n). Proof: First, we’re trying to bound the height of a tree with n keys. Let’s come at this the reverse way, and find the lower bound on the minimum number of internal nodes, n(h), that a tree with height h must have. Claim that n(h) grows exponentially. From this it will be easy to show that height of tree with n nodes is O(log n). So, formally, let n(h) be the minimum number of internal nodes of an AVL tree of height h. We easily see that n(1) = 1 and n(2) = 2 Binary Search Trees 17 n(2) Height of an AVL Tree 3 n(1) 4 For n > 2, an AVL tree of height h with minimal number of nodes contains the root node, one AVL subtree of height n-1 and another of height n-2. That is, n(h) = 1 + n(h-1) + n(h-2) Knowing n(h-1) > n(h-2), we get n(h) > 2n(h-2). This indicates that n(h) at least doubles every time we increase h by two, which intuitively says that n(h) grows at least exponentially. Let’s show this… By induction, n(h) > 2n(h-2), n(h) > 4n(h-4), n(h) > 8n(n-6), …, which implies that n(h) > 2in(h-2i) This holds for any positive integer i such that h-2i is positive. So, choose i so that we get a base case. That is, we want i such that either h-2i = 1 or h-2i = 2. I.e. i = (h/2)-1 or i=(h/2)-(1/2). Binary Search Trees 18 n(2) Height of an AVL Tree Thus the i we seek is n( h) > 2 h2 1 1. h 2 3 n(1) 4 So, we have h 1 2 n(1 or 2) > 2 . Taking logarithms: h < 2log n(h) +2 Thus the height of an AVL tree is O(log n) Binary Search Trees 19 Insertion in an AVL Tree Insertion is as in a binary search tree (may violate AVL property!) Always done by expanding an external node. Example: 44 44 17 17 78 32 50 48 c=z 78 a=y 88 62 Note that only nodes on path from w to root can now be unbalanced 32 50 88 48 62 b=x 54 w before insertion of 54 Binary Search Trees after insertion 20 Restoring Balance We use a “search and repair” strategy Let z be first unbalanced node encountered on path from w up toward root Let y be child of z with higher height (not y must be an ancestor of w), and x be child of y with higher height (in case of tie, go with child that is ancestor of w) Binary Search Trees 21 Trinode Restructuring let (a,b,c) be an inorder listing of x, y, z perform the rotations needed to make b the topmost node of the three (other two cases are symmetrical) a=z a=z case 2: double rotation (a right rotation about c, then a left rotation about a) c=y b=y T0 T0 b=x c=x T1 T3 b=y T2 case 1: single rotation (a left rotation about a) T1 T3 a=z T0 b=x T2 c=x T1 T2 a=z T3 Binary Search Trees T0 c=y T1 T2 22 T3 Insertion Example, continued 44 2 5 z 17 32 3 1 1 1 1 50 2 1 7 78 2y 48 64 3 4 62 88 x 5 T3 54 unbalanced... T0 T2 T1 44 2 4 3 17 32 1 ...balanced 1 1 48 2 y 2 50 4 x z6 62 3 1 5 78 2 54 7 88 T2 Binary Search Trees T0 T1 23 T3 1 Restructuring (as Single Rotations) Single Rotations: a=z single rotation b=y c=x T0 T1 T3 T2 c =z a=z T0 single rotation b=y b=y c=x T1 T3 T2 b=y a=x c =z a=x T0 T1 T2 T3 T3 Binary Search Trees T2 T1 T0 24 Restructuring (as Double Rotations) double rotations: double rotation a=z c= y b=x a=z c= y b=x T0 T3 T2 T1 T0 T1 double rotation c=z a=y T2 T3 b=x a=y c=z b=x T3 T2 T0 T3 T2 T1 T0 T1 Binary Search Trees 25 Removal in an AVL Tree Removal begins as in a binary search tree, which means the node removed will become an empty external node. Its parent, w, may cause an imbalance. Example: 44 44 17 62 32 50 48 17 62 78 54 50 88 before deletion of 32 Binary Search Trees 48 78 54 88 after deletion 26 Rebalancing after a Removal Let z be the first unbalanced node encountered while travelling up the tree from w. Also, let y be the child of z with the larger height, and let x be the child of y with the larger height. We perform restructure(x) to restore balance at z. As this restructuring may upset the balance of another node higher in the tree, we must continue checking for balance until the root of T is reached a=z w 62 44 17 50 48 c=x 78 54 44 b=y 62 17 50 48 88 Binary Search Trees 78 88 54 27 Running Times for AVL Trees a single restructure is O(1) using a linked-structure binary tree find is O(log n) height of tree is O(log n), no restructures needed insert is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n) remove is O(log n) initial find is O(log n) Restructuring up the tree, maintaining heights is O(log n) Binary Search Trees 28
© Copyright 2025 Paperzz