Ch04 Balanced Search Trees

Total Page:16

File Type:pdf, Size:1020Kb

Ch04 Balanced Search Trees Presentation for use with the textbook Algorithm Design and Applications, by M. T. Goodrich and R. Tamassia, Wiley, 2015 Ch04 Balanced Search Trees 6 v 3 8 z 4 1 1 Why care about advanced implementations? Same entries, different insertion sequence: Not good! Would like to keep tree balanced. 2 1 Balanced binary tree The disadvantage of a binary search tree is that its height can be as large as N-1 This means that the time needed to perform insertion and deletion and many other operations can be O(N) in the worst case We want a tree with small height A binary tree with N node has height at least (log N) Thus, our goal is to keep the height of a binary search tree O(log N) Such trees are called balanced binary search trees. Examples are AVL tree, and red-black tree. 3 Approaches to balancing trees Don't balance May end up with some nodes very deep Strict balance The tree must always be balanced perfectly Pretty good balance Only allow a little out of balance Adjust on access Self-adjusting 4 4 2 Balancing Search Trees Many algorithms exist for keeping search trees balanced Adelson-Velskii and Landis (AVL) trees (height-balanced trees) Red-black trees (black nodes balanced trees) Splay trees and other self-adjusting trees B-trees and other multiway search trees 5 5 Perfect Balance Want a complete tree after every operation Each level of the tree is full except possibly in the bottom right This is expensive For example, insert 2 and then rebuild as a complete tree 6 5 Insert 2 & 4 9 complete tree 2 8 1 5 8 1 4 6 9 6 6 3 AVL - Good but not Perfect Balance AVL trees are height-balanced binary search trees Balance factor of a node height(left subtree) - height(right subtree) An AVL tree has balance factor calculated at every node For every node, heights of left and right subtree can differ by no more than 1 Store current heights in each node 7 7 Height of an AVL Tree N(h) = minimum number of nodes in an AVL tree of height h. Basic case: N(0) = 1, N(1) = 2 Inductive case: h N(h) = N(h-1) + N(h-2) + 1 Theorem (from Fibonacci analysis) h N(h) > where 1.618, the golden ratio. h-2 h-1 8 8 4 Height of an AVL Tree h N(h) > (1.618) Suppose we have n nodes in an AVL tree of height h. n > N(h) (because N(h) was the minimum) h n > hence log n > h (relatively well balanced tree!!) h < 1.44 log2n (i.e., Find takes O(logn)) 9 9 Node Heights Tree A (AVL) Tree B (AVL) height=2 BF=1-0=1 2 6 6 1 0 1 1 4 9 4 9 0 0 0 0 0 1 5 1 5 8 height of node = h balance factor = hleft-hright empty height = -1 10 10 5 Node Heights after Insert 7 Tree A (AVL) Tree B (not AVL) 2 3 balance factor 1-(-1) = 2 6 6 1 1 1 2 4 9 4 9 0 0 0 0 0 1 -1 1 5 8 1 5 8 0 7 height of node = h balance factor = hleft-hright empty height = -1 11 11 Insert and Rotation in AVL Trees Insert operation may cause balance factor to become 2 or –2 for some node only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft- hright) is 2 or –2, adjust tree by rotation around the node 12 12 6 Single Rotation in an AVL Tree 2 2 6 6 1 2 1 1 4 9 4 8 0 0 1 0 0 0 0 1 5 8 1 5 7 9 0 7 13 13 Insertions in AVL Trees Let the node that needs rebalancing be . Cases: 1 3 4 2 There are 4 cases: Outside Cases (require single rotation) : 1. Insertion into left subtree of left child of . (left-left) 2. Insertion into right subtree of right child of . (right-right) Inside Cases (require double rotation) : 3. Insertion into right subtree of left child of . (left-right) 4. Insertion into left subtree of right child of . (right-left) The rebalancing is performed through four separate rotation algorithms. 14 14 7 AVL Insertion: Outside Case h+2 Consider a valid AVL subtree j h+1 k h h h Z X Y Differ by 1 15 15 AVL Insertion: Outside Case j Inserting into X destroys the AVL h+2 property at node j k h h+1 h Z Y X 16 16 8 AVL Insertion: Outside Case j Do a “rotation to right” k h h+1 h Z Y X 17 17 Single right rotation j Do a “right rotation” k h h+1 h Z Y X 18 18 9 Outside Case Completed “Right rotation” done! k (“Left rotation” is mirror symmetric) j h+1 h h X Y Z AVL property has been restored! 19 19 AVL Insertion: Inside Case Consider a valid AVL subtree j k h h h Z X Y 20 20 10 AVL Insertion: Inside Case Inserting into Y Does “right rotation” destroys the j restore balance? AVL property at node j k h h h+1 Z X Y 21 21 AVL Insertion: Inside Case “One rotation” k does not restore balance… now k is h j out of balance X h+1 h Z Y 22 22 11 AVL Insertion: Inside Case Consider the structure of subtree Y… j k h h h+1 Z X Y 23 23 AVL Insertion: Inside Case Y = node i and j subtrees V and W k h h i h+1 Z X h or h-1 V W 24 24 12 AVL Insertion: Inside Case j We will do a left-right “double rotation” . k i Z X V W 25 25 Double rotation : first rotation j left rotation complete i k Z W X V 26 26 13 Double rotation : second rotation j Now do a right rotation i k Z W X V 27 27 Double rotation : second rotation right rotation complete Balance has been i restored k j h h or h-1 h X V W Z 28 28 14 Implementation Class BinaryNode KeyType: Key height int: Height key BinaryNode: LeftChild BinaryNode: RightChild left right Constructor(KeyType: key) Key = key Once you have Height = 0 performed a End Constructor rotation (single or End Class double) you won’t need to go back up the tree 29 29 Java-like Pseudo-Code rotateToRight( BinaryNode: x ) { BinaryNode y = x.LeftChild; x.LeftChild = y.RightChild; y.RightChild = x; return y; } 30 15 Java-like Pseudo-Code rotateToLeft( BinaryNode: x ) { BinaryNode y = x.rightChild; x.rightChild = y.leftChild; y.leftChild = x; return y; } 31 Double Rotation Implement Double Rotation in two lines. n DoubleRotateToLeft(n : binaryNode) { rotateToRight(n.rightChild); rotateToLeft(n); } X DoubleRotateToRight(n : binaryNode) { Z rotateToLeft(n.leftChild); rotateToRight(n); V W } 32 32 16 Insertion in AVL Trees Insert at the leaf (as for all BST) only nodes on the path from insertion point to root node have possibly changed in height So after the Insert, go back up to the root node by node, updating heights If a new balance factor (the difference hleft- hright) is 2 or –2, adjust tree by rotation around the node 33 33 Insert in ordinary BST Algorithm insert(k, v) input: insert key k into the tree rooted by v output: the tree root with k adding to v. if isNull (v) return newNode(k) if k ≤ key(v) // duplicate keys are okay leftChild(v) insert (k, leftChild(v)) else if k key(v) rightChild(v) insert (k, rightChild(v)) return v 34 34 17 Insert in AVL trees Insert(v : binaryNode, x : element) : { if v = null then {v new node; v.data x; height 0;} else case v.data = x : ; //Duplicate do nothing v.data > x : v.leftChild Insert(v.leftChild, x); // handle left-right and left-left cases if ((height(v.leftChild)- height(v.rightChild)) = 2)then if (v.leftChild.data > x ) then //outside case v = RotateToRight (v); else //inside case v = DoubleRotateToRightt (v);} v.data < x : v.righChild Insert(v.rightChild, x); // handle right-right and right-left cases … … Endcase v.height max(height(v.left),height(v.right)) +1; return v; } 35 35 Example of Insertions in an AVL Tree 2 20 0 1 Insert 5, 40 10 30 0 0 25 35 36 36 18 Example of Insertions in an AVL Tree 2 3 20 20 1 1 1 2 10 30 10 30 0 0 0 0 0 1 5 25 35 5 25 35 0 40 Now Insert 45 37 37 Single rotation (outside case) 3 3 20 20 1 2 1 2 10 30 10 30 0 2 0 0 0 5 25 35 5 25 40 1 00 40 35 45 Imbalan 1 ce 0 45 Now Insert 34 38 38 19 Double rotation (inside case) 3 3 20 20 1 3 1 2 10 30 10 35 0 2 0 0 1 5 Imbalance 25 40 5 30 40 1 0 1 35 45 0 0 25 34 45 Insertion of 34 0 34 39 39 In Class Exercises Build an AVL tree with the following values: 15, 20, 24, 10, 13, 7, 30, 36, 25 40 20 15, 20, 24, 10, 13, 7, 30, 36, 25 20 15 15 24 20 10 24 13 20 20 13 24 15 24 10 15 13 10 41 15, 20, 24, 10, 13, 7, 30, 36, 25 20 13 13 24 10 20 10 15 7 15 24 7 30 13 36 10 20 7 15 30 24 36 42 21 15, 20, 24, 10, 13, 7, 30, 36, 25 13 13 10 20 10 20 7 15 30 7 15 24 24 36 30 25 13 25 36 10 24 7 20 30 15 25 36 43 Possible Quiz Questions Build an AVL tree by inserting the following values in the given order: 1, 2, 3, 4, 5, 6.
Recommended publications
  • Lecture 26 Fall 2019 Instructors: B&S Administrative Details
    CSCI 136 Data Structures & Advanced Programming Lecture 26 Fall 2019 Instructors: B&S Administrative Details • Lab 9: Super Lexicon is online • Partners are permitted this week! • Please fill out the form by tonight at midnight • Lab 6 back 2 2 Today • Lab 9 • Efficient Binary search trees (Ch 14) • AVL Trees • Height is O(log n), so all operations are O(log n) • Red-Black Trees • Different height-balancing idea: height is O(log n) • All operations are O(log n) 3 2 Implementing the Lexicon as a trie There are several different data structures you could use to implement a lexicon— a sorted array, a linked list, a binary search tree, a hashtable, and many others. Each of these offers tradeoffs between the speed of word and prefix lookup, amount of memory required to store the data structure, the ease of writing and debugging the code, performance of add/remove, and so on. The implementation we will use is a special kind of tree called a trie (pronounced "try"), designed for just this purpose. A trie is a letter-tree that efficiently stores strings. A node in a trie represents a letter. A path through the trie traces out a sequence ofLab letters that9 represent : Lexicon a prefix or word in the lexicon. Instead of just two children as in a binary tree, each trie node has potentially 26 child pointers (one for each letter of the alphabet). Whereas searching a binary search tree eliminates half the words with a left or right turn, a search in a trie follows the child pointer for the next letter, which narrows the search• Goal: to just words Build starting a datawith that structure letter.
    [Show full text]
  • Search Trees
    Lecture III Page 1 “Trees are the earth’s endless effort to speak to the listening heaven.” – Rabindranath Tagore, Fireflies, 1928 Alice was walking beside the White Knight in Looking Glass Land. ”You are sad.” the Knight said in an anxious tone: ”let me sing you a song to comfort you.” ”Is it very long?” Alice asked, for she had heard a good deal of poetry that day. ”It’s long.” said the Knight, ”but it’s very, very beautiful. Everybody that hears me sing it - either it brings tears to their eyes, or else -” ”Or else what?” said Alice, for the Knight had made a sudden pause. ”Or else it doesn’t, you know. The name of the song is called ’Haddocks’ Eyes.’” ”Oh, that’s the name of the song, is it?” Alice said, trying to feel interested. ”No, you don’t understand,” the Knight said, looking a little vexed. ”That’s what the name is called. The name really is ’The Aged, Aged Man.’” ”Then I ought to have said ’That’s what the song is called’?” Alice corrected herself. ”No you oughtn’t: that’s another thing. The song is called ’Ways and Means’ but that’s only what it’s called, you know!” ”Well, what is the song then?” said Alice, who was by this time completely bewildered. ”I was coming to that,” the Knight said. ”The song really is ’A-sitting On a Gate’: and the tune’s my own invention.” So saying, he stopped his horse and let the reins fall on its neck: then slowly beating time with one hand, and with a faint smile lighting up his gentle, foolish face, he began..
    [Show full text]
  • Comparison of Dictionary Data Structures
    A Comparison of Dictionary Implementations Mark P Neyer April 10, 2009 1 Introduction A common problem in computer science is the representation of a mapping between two sets. A mapping f : A ! B is a function taking as input a member a 2 A, and returning b, an element of B. A mapping is also sometimes referred to as a dictionary, because dictionaries map words to their definitions. Knuth [?] explores the map / dictionary problem in Volume 3, Chapter 6 of his book The Art of Computer Programming. He calls it the problem of 'searching,' and presents several solutions. This paper explores implementations of several different solutions to the map / dictionary problem: hash tables, Red-Black Trees, AVL Trees, and Skip Lists. This paper is inspired by the author's experience in industry, where a dictionary structure was often needed, but the natural C# hash table-implemented dictionary was taking up too much space in memory. The goal of this paper is to determine what data structure gives the best performance, in terms of both memory and processing time. AVL and Red-Black Trees were chosen because Pfaff [?] has shown that they are the ideal balanced trees to use. Pfaff did not compare hash tables, however. Also considered for this project were Splay Trees [?]. 2 Background 2.1 The Dictionary Problem A dictionary is a mapping between two sets of items, K, and V . It must support the following operations: 1. Insert an item v for a given key k. If key k already exists in the dictionary, its item is updated to be v.
    [Show full text]
  • AVL Tree, Bayer Tree, Heap Summary of the Previous Lecture
    DATA STRUCTURES AND ALGORITHMS Hierarchical data structures: AVL tree, Bayer tree, Heap Summary of the previous lecture • TREE is hierarchical (non linear) data structure • Binary trees • Definitions • Full tree, complete tree • Enumeration ( preorder, inorder, postorder ) • Binary search tree (BST) AVL tree The AVL tree (named for its inventors Adelson-Velskii and Landis published in their paper "An algorithm for the organization of information“ in 1962) should be viewed as a BST with the following additional property: - For every node, the heights of its left and right subtrees differ by at most 1. Difference of the subtrees height is named balanced factor. A node with balance factor 1, 0, or -1 is considered balanced. As long as the tree maintains this property, if the tree contains n nodes, then it has a depth of at most log2n. As a result, search for any node will cost log2n, and if the updates can be done in time proportional to the depth of the node inserted or deleted, then updates will also cost log2n, even in the worst case. AVL tree AVL tree Not AVL tree Realization of AVL tree element struct AVLnode { int data; AVLnode* left; AVLnode* right; int factor; // balance factor } Adding a new node Insert operation violates the AVL tree balance property. Prior to the insert operation, all nodes of the tree are balanced (i.e., the depths of the left and right subtrees for every node differ by at most one). After inserting the node with value 5, the nodes with values 7 and 24 are no longer balanced.
    [Show full text]
  • AVL Insertion, Deletion Other Trees and Their Representations
    AVL Insertion, Deletion Other Trees and their representations Due now: ◦ OO Queens (One submission for both partners is sufficient) Due at the beginning of Day 18: ◦ WA8 ◦ Sliding Blocks Milestone 1 Thursday: Convocation schedule ◦ Section 01: 8:50-10:15 AM ◦ Section 02: 10:20-11:00 AM, 12:35-1:15 PM ◦ Convocation speaker Risk Analysis Pioneer John D. Graham The promise and perils of science and technology regulations 11:10 a.m. to 12:15 p.m. in Hatfield Hall. ◦ Part of Thursday's class time will be time for you to work with your partner on SlidingBlocks Due at the beginning of Day 19 : WA9 Due at the beginning of Day 21 : ◦ Sliding Blocks Final submission Non-attacking Queens Solution Insertions and Deletions in AVL trees Other Search Trees BSTWithRank WA8 Tree properties Height-balanced Trees SlidingBlocks CanAttack( ) toString( ) findNext( ) public boolean canAttack(int row, int col) { int columnDifference = col - column ; return currentRow == row || // same row currentRow == row + columnDifference || // same "down" diagonal currentRow == row - columnDifference || // same "up" diagonal neighbor .canAttack(row, col); // If I can't attack it, maybe // one of my neighbors can. } @Override public String toString() { return neighbor .toString() + " " + currentRow ; } public boolean findNext() { if (currentRow == MAXROWS ) { // no place to go until neighbors move. if (! neighbor .findNext()) return false ; // Neighbor can't move, so I can't either. currentRow = 0; // about to be bumped up to 1. } currentRow ++; return testOrAdvance(); // See if this new position works. } You did not have to write this one: // If this is a legal row for me, say so. // If not, try the next row.
    [Show full text]
  • Balanced Binary Search Trees – AVL Trees, 2-3 Trees, B-Trees
    Balanced binary search trees – AVL trees, 2‐3 trees, B‐trees Professor Clark F. Olson (with edits by Carol Zander) AVL trees One potential problem with an ordinary binary search tree is that it can have a height that is O(n), where n is the number of items stored in the tree. This occurs when the items are inserted in (nearly) sorted order. We can fix this problem if we can enforce that the tree remains balanced while still inserting and deleting items in O(log n) time. The first (and simplest) data structure to be discovered for which this could be achieved is the AVL tree, which is names after the two Russians who discovered them, Georgy Adelson‐Velskii and Yevgeniy Landis. It takes longer (on average) to insert and delete in an AVL tree, since the tree must remain balanced, but it is faster (on average) to retrieve. An AVL tree must have the following properties: • It is a binary search tree. • For each node in the tree, the height of the left subtree and the height of the right subtree differ by at most one (the balance property). The height of each node is stored in the node to facilitate determining whether this is the case. The height of an AVL tree is logarithmic in the number of nodes. This allows insert/delete/retrieve to all be performed in O(log n) time. Here is an example of an AVL tree: 18 3 37 2 11 25 40 1 8 13 42 6 10 15 Inserting 0 or 5 or 16 or 43 would result in an unbalanced tree.
    [Show full text]
  • Performance Analysis of Bsts in System Software∗
    Performance Analysis of BSTs in System Software∗ Ben Pfaff Stanford University Department of Computer Science [email protected] Abstract agement and networking, and the third analyzes a part of a source code cross-referencing tool. In each Binary search tree (BST) based data structures, such case, some test workloads are drawn from real-world as AVL trees, red-black trees, and splay trees, are of- situations, and some reflect worst- and best-case in- ten used in system software, such as operating system put order for BSTs. kernels. Choosing the right kind of tree can impact performance significantly, but the literature offers few empirical studies for guidance. We compare 20 BST We compare four variants on the BST data struc- variants using three experiments in real-world scenar- ture: unbalanced BSTs, AVL trees, red-black trees, ios with real and artificial workloads. The results in- and splay trees. The results show that each should be dicate that when input is expected to be randomly or- preferred in a different situation. Unbalanced BSTs dered with occasional runs of sorted order, red-black are best when randomly ordered input can be relied trees are preferred; when insertions often occur in upon; if random ordering is the norm but occasional sorted order, AVL trees excel for later random access, runs of sorted order are expected, then red-black trees whereas splay trees perform best for later sequential should be chosen. On the other hand, if insertions or clustered access. For node representations, use of often occur in a sorted order, AVL trees excel when parent pointers is shown to be the fastest choice, with later accesses tend to be random, and splay trees per- threaded nodes a close second choice that saves mem- form best when later accesses are sequential or clus- ory; nodes without parent pointers or threads suffer tered.
    [Show full text]
  • Lecture 13: AVL Trees and Binary Heaps
    Data Structures Brett Bernstein Lecture 13: AVL Trees and Binary Heaps Review Exercises 1.( ??) Interview question: Given an array show how to shue it randomly so that any possible reordering is equally likely. static void shue(int[] arr) 2. Write a function that given the root of a binary search tree returns the node with the largest value. public static BSTNode<Integer> getLargest(BSTNode<Integer> root) 3. Explain how you would use a binary search tree to implement the Map ADT. We have included it below to remind you. Map.java //Stores a mapping of keys to values public interface Map<K,V> { //Adds key to the map with the associated value. If key already //exists, the associated value is replaced. void put(K key, V value); //Gets the value for the given key, or null if it isn't found. V get(K key); //Returns true if the key is in the map, or false otherwise. boolean containsKey(K key); //Removes the key from the map void remove(K key); //Number of keys in the map int size(); } What requirements must be placed on the Map? 4. Consider the following binary search tree. 10 5 15 1 12 18 16 17 1 Perform the following operations in order. (a) Remove 15. (b) Remove 10. (c) Add 13. (d) Add 8. 5. Suppose we begin with an empty BST. What order of add operations will yield the tallest possible tree? Review Solutions 1. One method (popular in programs like Excel) is to generate a random double corre- sponding to each element of the array, and then sort the array by the corresponding doubles.
    [Show full text]
  • CS302ES Regulations
    DATA STRUCTURES Subject Code: CS302ES Regulations : R18 - JNTUH Class: II Year B.Tech CSE I Semester Department of Computer Science and Engineering Bharat Institute of Engineering and Technology Ibrahimpatnam-501510,Hyderabad DATA STRUCTURES [CS302ES] COURSE PLANNER I. CourseOverview: This course introduces the core principles and techniques for Data structures. Students will gain experience in how to keep a data in an ordered fashion in the computer. Students can improve their programming skills using Data Structures Concepts through C. II. Prerequisite: A course on “Programming for Problem Solving”. III. CourseObjective: S. No Objective 1 Exploring basic data structures such as stacks and queues. 2 Introduces a variety of data structures such as hash tables, search trees, tries, heaps, graphs 3 Introduces sorting and pattern matching algorithms IV. CourseOutcome: Knowledge Course CO. Course Outcomes (CO) Level No. (Blooms Level) CO1 Ability to select the data structures that efficiently L4:Analysis model the information in a problem. CO2 Ability to assess efficiency trade-offs among different data structure implementations or L4:Analysis combinations. L5: Synthesis CO3 Implement and know the application of algorithms for sorting and pattern matching. Data Structures Data Design programs using a variety of data structures, CO4 including hash tables, binary and general tree L6:Create structures, search trees, tries, heaps, graphs, and AVL-trees. V. How program outcomes areassessed: Program Outcomes (PO) Level Proficiency assessed by PO1 Engineeering knowledge: Apply the knowledge of 2.5 Assignments, Mathematics, science, engineering fundamentals and Tutorials, Mock an engineering specialization to the solution of II B Tech I SEM CSE Page 45 complex engineering problems.
    [Show full text]
  • Data Structures Lecture 11
    Fall 2021 Fang Yu Software Security Lab. Data Structures Dept. Management Information Systems, National Chengchi University Lecture 11 Search Trees Binary Search Trees, AVL trees, and Splay Trees 3 Binary Search Trees ¡ A binary search tree is a binary tree storing keys (or key-value ¡ An inorder traversal of a binary entries) at its internal nodes and search trees visits the keys in an satisfying the following increasing order 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. 6 We have key(u) ≤ key(v) ≤ key(w) 2 9 ¡ External nodes do not store items 1 4 8 4 Search Algorithm TreeSearch(k, v) ¡ To search for a key k, we trace if T.isExternal (v) a downward path starting at the return v root if k < key(v) return TreeSearch(k, T.left(v)) ¡ The next node visited depends else if k key(v) on the comparison of k with the = return v key of the current node else { k > key(v) } ¡ If we reach a leaf, the key is return TreeSearch(k, T.right(v)) not found < 6 ¡ Example: get(4): 2 9 > ¡ Call TreeSearch(4,root) 1 4 = 8 ¡ The algorithms for floorEntry and ceilingEntry are similar 5 Insertion 6 ¡ To perform operation put(k, o), < we search for key k (using 2 9 > TreeSearch) 1 4 8 > ¡ Assume k is not already in the tree, and let w be the leaf w reached by the search 6 ¡ We insert k at node w and expand w into an internal node 2 9 ¡ Example: insert 5 1 4 8 w 5 6 Deletion 6 ¡ To perform operation remove(k), < we search for key k 2 9 > ¡ Assume key k is in the tree, and 1 4 v 8 let v be
    [Show full text]
  • Balanced Search Trees
    Balanced Search Trees ● Binary search trees keep keys ordered, with efficient lookup ➤ Insert, Delete, Find, all are O(log n) in average case ➤ Worst case is bad ➤ Compared to hashing? Advantages? ● Balanced trees are guaranteed O(log n) in the worst case ➤ Fundamental operation is a rotation: keep tree roughly balanced ➤ AVL tree was the first one, still studied since simple conceptually ➤ Red-Black tree uses rotations, harder to code, better in practice ➤ B-trees are used when data is stored on disk rather than in memory CPS 100 12.1 Rotations and balanced trees ● Height-balanced trees ➤ For every node, left and right subtree heights differ by at most 1 ➤ After insertion/deletion need to rebalance Are these trees height- ➤ Every operation leaves tree in balanced? a balanced state: invariant property of tree ● Find deepest node that’s unbalanced ➤ On path from root to insert/deleted node ➤ Rebalance at this unbalanced point only CPS 100 12.2 Rotation to rebalance N ● When a node N is unbalanced N height differs by 2 (must be more than one) C A ➤ Change N->left->left • doLeft A B B C ➤ Change N->left->right • doLeftRight Tree * doLeft(Tree * root) ➤ Change N->right->left { • doRightLeft Tree * newRoot = root->left; ➤ Change N->right->right root->left = newRoot->right; • doRight newRoot->right = root; ● First/last cases are symmetric return newRoot; ● Middle cases require two rotation } ➤ First of the two puts tree into doLeft or doRight CPS 100 12.3 Rotation to rebalance ????? N ● Suppose we add a new node in right N subtree of left child
    [Show full text]
  • Lecture 16 Notes AVL Trees
    Lecture 16 Notes AVL Trees 15-122: Principles of Imperative Computation (Summer 1 2015) Frank Pfenning 1 Introduction Binary search trees are an excellent data structure to implement associative arrays, maps, sets, and similar interfaces. The main difficulty is that they are efficient only when they are balanced. Straightforward sequences of insertions can lead to highly unbalanced trees with poor asymptotic com- plexity and unacceptable practical efficiency. For example, if we insert n elements with keys that are in strictly increasing or decreasing order, the complexity for n insertions will be O(n2). On the other hand, if we can keep the height to O(log n), as it is for a perfectly balanced tree, then the complexity is bounded by O(n ∗ log n). The tree can be kept balanced by dynamically rebalancing the search tree during insert or search operations. We have to be careful not to destroy the ordering invariant of the tree while we rebalance. Because of the impor- tance of binary search trees, researchers have developed many different algorithms for keeping trees in balance, such as AVL trees, red/black trees, splay trees, or randomized binary search trees. They differ in the invariants they maintain (in addition to the ordering invariant), and when and how the rebalancing is done. In this lecture we use AVL trees, which is a simple and efficient data structure to maintain balance, and is also the first that has been proposed. It is named after its inventors, G.M. Adelson-Velskii and E.M. Landis, who described it in 1962.
    [Show full text]