Data Structures AVL Trees

Total Page:16

File Type:pdf, Size:1020Kb

Data Structures AVL Trees CS350: Data Structures AVL Trees YORK COLLEGE OF PENNSYLVANIA YORK COLLEGE OF PENNSYLVANIA YORK COLLEGE OF PENNSYLVANIA YORK COLLEGE OF PENNSYLVANIA 'GHI<GJHK&L<MNK'GHI<GJHK&L<MNK 'GONJHK&P@JJHG 'GONJHK&P@JJHG MFIF&'<IJ@QH&'@OK MFIF&'<IJ@QH&'@OK !<GR%&'@ !<GR%&'@ James Moscola 'HGPOJ&N<F&012 'HGPOJ&N<F&012 !"#$%&'())*+,-.)/.&0123454670!"#$%&'())*+,-.)/.&0123454670 YORK COLLEGE OF PENNSYLVANIA 'GHI<GJHK&L<MNK Department of Engineering & Computer Science 8.9:;*&<:(#.="#>&1015?26511??8.9:;*&<:(#.="#>&1015?26511?? @A9/**/")*&<B!&C(>&1015?2D50633@A9/**/")*&<B!&C(>&1015?2D50633'GONJHK&P@JJHG York College of Pennsylvania 05?3352775?30? 05?3352775?30? MFIF&'<IJ@QH&'@OK EEEF+C:F(A; EEEF+C:F(A; !<GR%&'@ 'HGPOJ&N<F&012 !""#$%%&'$#()*$&+$,-$%.$"!""#$%%&'$#()*$&+$,-$%.$" !"#$%&'())*+,-.)/.&0123454670 8.9:;*&<:(#.="#>&1015?26511?? @A9/**/")*&<B!&C(>&1015?2D50633 05?3352775?30? EEEF+C:F(A; !""#$%%&'$#()*$&+$,-$%.$" CS350: Data Structures © James Moscola COLLEGE CATALOG 2009–2011 COLLEGE CATALOG COLLEGE CATALOG 2009–2011 COLLEGE CATALOG COLLEGE CATALOG 2009–2011 COLLEGE CATALOG College Catalog 2009–2011College Catalog 2009–2011 College Catalog 2009–2011 !""#$%&'()*+,--.../ 012$1$"..."34#3$4.56 !""#$%&'()*+,--.../ 012$1$"..."34#3$4.56 !""#$%&'()*+,--.../ 012$1$"..."34#3$4.56 Balanced Search Trees • Binary search trees are not guaranteed to be balanced given random insertions and deletions - Inserting a sorted lists of elements into a BST produces the worst case -- O(N) - Performance of an unbalanced tree can degrade as more elements are inserted • Balanced search tree operations, such as insert, insure that the a tree always remains balanced - An operation is not complete until it returns the tree to a balanced state Balanced Unbalanced tree tree CS350: Data Structures 2 AVL Trees • A type of balanced binary search tree • Named for its discoverers -- Adelson, Velskii, and Landis • Definition: An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1. CS350: Data Structures 3 AVL Trees 3 12 The height of each node is listed in blue 2 8 1 16 1 4 10 0 0 14 -1 0 2 6 0 An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1. CS350: Data Structures 4 AVL Trees 3 12 The height of each node is listed in blue 2 8 1 16 height differs by at most 1 1 4 10 0 0 14 -1 0 2 6 0 An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1. CS350: Data Structures 5 AVL Trees 3 12 The height of each node is listed in blue 2 8 1 16 height differs by at most 1 1 4 10 0 0 14 -1 0 2 6 0 An AVL tree is a binary search tree with the additional balance property that, for any node in the tree, the height of the left and right subtrees can differ by at most 1. The height of an empty subtree is -1. CS350: Data Structures 6 AVL Trees The binary tree after inserting node 1 ? 12 What are the node heights? ? 8 1 16 ? 4 10 0 0 14 -1 ? 2 6 0 ? 1 CS350: Data Structures 7 AVL Trees The binary tree after inserting node 1 4 12 What are the node heights? 3 8 1 16 2 4 10 0 0 14 -1 1 2 6 0 0 1 CS350: Data Structures 8 AVL Trees 4 12 3 8 1 16 height differs by more than 1 therefore, node 8 is invalid 2 4 10 0 0 14 -1 1 2 6 0 0 1 CS350: Data Structures 9 AVL Trees 4 12 height differs by 3 8 1 16 more than 1 height differs by therefore, node 12 more than 1 is unbalanced therefore, node 8 is unbalanced 2 4 10 0 0 14 -1 1 2 6 0 0 1 CS350: Data Structures 10 AVL Trees • When a node in the tree no longer satisfies the invariant required to be an AVL tree the tree must be rebalanced around that node • There are four ways in which an insertion into a tree may cause an imbalance to occur at some node X: (1) An insertion in the left subtree of the left child of X (2) An insertion in the right subtree of the left child of X symmetric (3) An insertion in the left subtree of the right child of X (4) An insertion in the right subtree of the right child of X CS350: Data Structures 11 Creating an Imbalance -- Case #1 (1) An insertion in the left subtree of the left child of X N2 1 N2 2 N1 0 -1 N1 1 -1 -1 -1 C 0 -1 C A B B A root of A subtree Node N1 and root of C differ increases height by more than 1 therefore N2 as does N1 and N2 violates AVL rules CS350: Data Structures 12 Fixing the Imbalance -- Case #1 Perform a single right rotation N1 becomes new root node N2 now right N2 2 1 N1 child of N1 0 N1 1 -1 0 N2 0 -1 C -1 -1 A B B C A B subtree now left child of N2 CS350: Data Structures 13 Creating an Imbalance -- Case #4 (symmetric with 1) (4) An insertion in the right subtree of the right child of X N2 N12 N1 N1 A A root of C subtree B C increases height B as does N1 and N2 C Node N1 and root of A differ by more than 1 therefore N2 violates AVL rules CS350: Data Structures 14 Fixing the Imbalance -- Case #4 (symmetric with 1) Perform a single left rotation N1 becomes new root node N2 now left N2 N1 child of N1 N1 N2 A C B A B C B subtree now right child of N2 CS350: Data Structures 15 Creating an Imbalance -- Case #2 (2) An insertion in the right subtree of the left child of X N3 1 N3 2 N1 0 -1 N1 1 -1 -1 -1 C -1 0 C A B A B Node N1 and root of C differ root of B subtree by more than 1 therefore N3 increases height violates AVL rules as does N1 and N3 CS350: Data Structures 16 Fixing the Imbalance -- Case #2 We know the node N2 exists because a node was just inserted into the B subtree N3 2 N3 2 N1 1 -1 N1 1 -1 -1 0 C -1 0 N2 C A A -1 -1 B B1 B2 CS350: Data Structures 17 Fixing the Imbalance -- Case #2 Perform a left-right double rotation Step #1 - Perform a single left rotation tree is still N3 2 between N1 and N2 N3 2 unbalanced N1 1 -1 N2 1 -1 -1 0 N2 C 0 N1 -1 C A -1 -1 -1 -1 B2 B1 B2 A B1 CS350: Data Structures 18 Fixing the Imbalance -- Case #2 Perform a left-right double rotation Step #2 - Perform a single right rotation N3 2 between N2 and N3 N2 1 N2 1 -1 N1 0 0 N3 0 N1 -1 C -1 -1 -1 -1 -1 -1 B2 A B1 B2 C A B1 CS350: Data Structures 19 Fixing the Imbalance -- Case #2 Another look at the left-right double rotation without the intermediate steps N3 N2 N1 N1 N3 N2 C A A B1 B2 C B1 B2 CS350: Data Structures 20 Fixing the Imbalance -- Case #3 (symmetric with 2) Here is a look at the right-left double rotation without the intermediate steps N3 N2 N1 N3 N1 A N2 C A B1 B2 C B1 B2 CS350: Data Structures 21 AVL Tree Insertion Example 3 12 2 8 1 16 1 4 10 0 0 14 -1 0 2 6 0 CS350: Data Structures 22 AVL Tree Insertion Example • Inserting node 1 ? 12 - First, recursively search for the location which to insert the node - Next, insert the node ? 8 1 16 - Finally, unwind the recursion update node heights along the way. Rebalance tree where ? 4 10 0 0 14 -1 necessary • To update node’s height, check its children ? 2 6 0 ? 1 CS350: Data Structures 23 AVL Tree Insertion Example ? 12 ? 8 1 16 ? 4 10 0 0 14 -1 ? 2 6 0 node 1 is a leaf node so its height is 0 0 1 CS350: Data Structures 24 AVL Tree Insertion Example ? 12 ? 8 1 16 node 2 - add 1 to the height ? 4 10 0 0 14 -1 of its highest child 1 2 6 0 0 1 CS350: Data Structures 25 AVL Tree Insertion Example ? 12 node 4 - add 1 to the height ? 8 1 16 of its highest child 2 4 10 0 0 14 -1 1 2 6 0 0 1 CS350: Data Structures 26 AVL Tree Insertion Example node 8 violates AVL ? 12 properties, so rebalance ? 8 1 16 height differs by more than 1 2 4 10 0 0 14 -1 1 2 6 0 0 1 CS350: Data Structures 27 AVL Tree Insertion Example Case #1: Must perform single ? 12 right rotation between node 8 and node 4 ? 8 1 16 2 4 10 0 0 14 -1 1 2 6 0 0 1 CS350: Data Structures 28 AVL Tree Insertion Example After rotation, update height of node 8 ? 12 Node 4 is balanced, so continue unwinding recursion 2 4 1 16 1 2 8 1 0 14 -1 0 1 6 0 10 0 CS350: Data Structures 29 AVL Tree Insertion Example node 12 - add 1 to the 3 12 height of its highest child 2 4 1 16 1 2 8 1 0 14 -1 0 1 6 0 10 0 CS350: Data Structures 30 Other Operations: find / remove • The find operation is the same as the unbalanced binary search tree • The remove operation works similarly to the remove operation from the unbalanced binary search tree with a few modifications - When a node is removed, the heights of its ancestors may need to be updated as the recursion is unwound -- fix imbalances as they are encountered just like with insertion CS350: Data Structures 31 Analysis of AVL Tree Operations • Time complexity of AVL Tree operations worst case average find O(log N) O(log N) insert O(log N) O(log N) remove O(log N) O(log N) CS350: Data Structures 32.
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]
  • 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.
    [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]