AVL Tree + Heaps

Total Page:16

File Type:pdf, Size:1020Kb

AVL Tree + Heaps Assignment 1 Marking • Assignment 1 marking is very lenient • For example, in Q2a. If you use Master’s Theorem on the bound if the recurrence function you want to solve, most likely, you’ll need to verify the solution provided by Master’s Theorem • We did not penalise those answers to Q2a that uses Master’s Theorem without verification. • However, we will not be that lenient in marking the final exam. So, please: • Take a look at the solution example for Part A (in the class website) • And make sure you understand them. If you don’t, ask!!! Total Marks for this class • Final Mark = 0.2*Max(A1, E) + 0.2*Max(A2, E) + 0.2*Max(A3, E) + 0.4*E • A1, A2, A3: mark for Assignment 1, 2, 3 respectively • E: marks for final exam • Tips: Please do NOT ignore assignments. You really do NOT want to rely all your marks solely on the final exam Assignment 1 Issue • When checking for plagiarism, we found A1 questions have been uploaded to a homework outsourcing site (masquerading as a “legitimate homework help” site) on 6, 18, 19, 21 Aug • We’ve contacted them. They send us the answers and investigations are on-going. • Btw., most of the answers are wrong!!! • So, please do risk analysis!!! • Risks of outsourcing: • Getting caught, which will stay in one’s academic record • Being blackmailed by the service provider • Gain: Wrong answers for a redeemable assignment!!! Assignment 2 • Due: Mon 30 Sep 23:59; Grace period Tue 1 Oct 13:00 • Additional clarifications: 10 Sep • We’ve sent the announcement via email (you need to be enrolled in piazza) • Please redownload if you have downloaded before 10 Sep • This week tutorial will solely be on help for A2 • Hope this alleviate the issue of not enough tutorial time spent for assignment help • Drop in session: • Friday 20 Sep, 27 Sep. Both on 8am-10am at CSIT N115/116 • If needed, you can join other tutorial groups • Please e-mail [email protected] first, to ensure the tutorial group you intend to attend will have space for you Before the Break üWhat is Transform & Conquer in Algorithm Design? üInstance Simplification üCommon method: Pre-sorting üA bit more about sorting üRepresentation Change üSupporting Data Structure: üBinary Search Tree üRed-Black Tree • AVL Tree • Heaps • Examples • Problem Reduction Today üWhat is Transform & Conquer in Algorithm Design? üInstance Simplification üCommon method: Pre-sorting üA bit more about sorting üRepresentation Change üSupporting Data Structure: üBinary Search Tree üRed-Black Tree • AVL Tree • Heaps • Examples • Problem Reduction COMP3600/6466 – Algorithms Transform & Conquer 3 [Lev 6.3, CLRS ch. 6] Hanna Kurniawati https://cs.anu.edu.au/courses/comp3600/ Today • AVL Tree • What is it? • Transformation • Insertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion What is AVL Tree? • The first self-balancing binary search tree, invented in 1962 by Adel’son-Vel’skii and Landis (written in Russian) • The goal is the same as in red-black tree, which is we want to have the height of the tree to be �(log �), with � being the #nodes in the tree • Intuitively, an AVL tree maintains balance by ensuring the height of the left and right sub-tree of all nodes in the tree are almost the same (i.e., differ by at most 1) What is AVL Tree? • More formally, an AVL tree is a binary search tree where • Each node is augmented with the balance factor of the node Balance factor of node � = height(leftSubtree(�)) – height(rightSubtree(�)) Height of a tree/sub-tree is the length of the longest path from the root of the tree/sub-tree to a leaf Empty height (e.g., when � has no left/right subtree) is -1 • The balance factor of each node in the tree must either be either 0, +1, or -1 Example Why Is The Height of AVL Tree �(log �)? • We’ll show the height of an AVL tree by computing the minimum number of nodes (aka worst case) that an AVL tree of height ℎ could have • Suppose �+ is the minimum number of nodes for an AVL tree of height ℎ . • In AVL tree, the minimum number of nodes is achieved when every node in the tree have a balance factor of +/-1 • Therefore, �+ = 1 + �+/0 + �+/1 • Solve the recurrence Solving the Recurrence • �+ = 1 + �+/0 + �+/1 Today • AVL Tree üWhat is it? • Transformation • Insertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion Key to AVL • Similar to red-black tree, AVL tree relies on transformation operation, called rotation, to re-balance the tree Transformation: Rotation • Four types: 2 classes, 2 types in each class • Single Left Rotation (L- rotation) • L-rotation on node �: Rotate the edge connecting � and its left child • Single Right Rotation (R- rotation): • Mirror image of L-rotation Transformation: Rotation • Four types: 2 classes, 2 types in each class • Double Left Right Rotation (LR-rotation) • LR-rotation of node �: L- rotation on the root of �’s left subtree followed by R- rotation on � • Double Right Left Rotation (RL-rotation) • Mirror image of LR-rotation Today • AVL Tree üWhat is it? üTransformation • Insertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion Insertion • Essentially, perform binary search tree insertion and then rebalance whenever necessary • To insert a node � to an AVL tree T: • Insert � as if T is a usual binary search tree • Let � be the parent of the newly inserted node • While � is not root • If � violates the AVL property, rotate to restore AVL property • Update Balance Factor of � • Let � = �. ������ Insertion – Rebalancing Cases • Insertion of left-left grandchild cause inbalance • R-rotation • Insertion of right-right grandchild cause inbalance • Same as above but other way around: L-rotation Insertion – Rebalancing Cases • Insertion of right-left grandchild cause inbalance • RL-rotation • Insertion of left-right grandchild cause inbalance • Same as above but other way around: LR-rotation Example Today • AVL Tree üWhat is it? üTransformation üInsertion • Deletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion Deletion • Similar to insertion: Essentially, binary search tree deletion and then rebalance whenever necessary • Recall binary search tree deletion has 3 cases based on #children of the node being deleted • 0 children: Just delete à Nodes that might change height are those in the path from deleted node to root • 1 child: Delete it, connect child to parent à Nodes that might change height are those in the path from deleted node to root • 2 children: Replace the deleted node with its successor, successor leaf à Nodes that might change height are those in the path from deleted successor leaf to root • Rebalance along the path where nodes might change height Deletion – Rebalancing Cases • Left-left grandchild becomes too tall • R-rotation • Right-right grandchild becomes too tall • Same as above but other way around: L-rotation Insertion – Rebalancing Cases • Right-left grandchild becomes too tall • RL-rotation • Left-right grandchild becomes too tallcause inbalance • Same as above but other way around: LR-rotation Example Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps • What is it? • Heapify • Insertion • Extract/Deletion What is A Heap? • A heap is a binary tree that satisfies heap property: • A heap is a complete binary tree • The nodes contain data similar to binary search tree: • Each node has a key, in which node order are based on • Each node may contain additional information • The parent node has key greater than the keys in its children • Complete binary tree: A perfect binary tree where at the last level, some rightmost leaves may be missing • Perfect binary tree: A tree where all interior nodes have 2 children and all leaves are at the same level • Since a heap is a complete tree, it can be implemented easily with an array • Left & right children becomes index of the array that holds the left & right child nodes What is A Heap? • There’s 2 types of heap: Max-heap and Min-heap • The one we discussed in the previous slide is Max-heap • Throughout this class, we’ll assume the heap is Max-heap unless stated otherwise • Min-heap is similar to Max-heap but, the parent node has key less than the keys in its children Example What is A Heap? • Main Operations: • Heapify to ensure heap properties are satisfied • Insert a node to an existing heap • ExtractMax (for Min-heap, this operation is ExtractMin) to retrieve an element from the heap • All of the above operations are �(log �), where � is the number of nodes in the tree Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps üWhat is it? • Heapify • Insertion • Extract/Deletion Heapify: Maintaining Heap Property • Intuitively, heapify a node means traversing the tree from root until a suitable place (to ensure heap order) is found for the node • Pseudo-code [CLRS sec. 6.2] Example Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps üWhat is it? üHeapify • Insertion • Extract/Deletion Building a heap • Build a heap from an array of numbers, pseudo-code Example Inserting a node to a heap • Can also be used to build a heap when data is received dynamically • Intuitively, add a new node at the bottom right most heap and then find a suitable position for the new node • Pseudo-code [CLRS pp.164] Example Today üAVL Tree üWhat is it? üTransformation üInsertion üDeletion • Heaps üWhat is it? üHeapify üInsertion • Extract/Deletion ExtractMax • Intutitively: • Output the root node • Swap the root node with the last node in the heap (i.e., the bottom and right most leaf) • Decrease the heap size by 1, essentially removing the last leaf node (which is now the same as the root node we just extracted) • Heapify the new root of the tree ExtractMax • Pseudo-code [CLRS pp.163] • The algorithm only needs to traverse a path of the tree once.
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]
  • Game Trees, Quad Trees and Heaps
    CS 61B Game Trees, Quad Trees and Heaps Fall 2014 1 Heaps of fun R (a) Assume that we have a binary min-heap (smallest value on top) data structue called Heap that stores integers and has properly implemented insert and removeMin methods. Draw the heap and its corresponding array representation after each of the operations below: Heap h = new Heap(5); //Creates a min-heap with 5 as the root 5 5 h.insert(7); 5,7 5 / 7 h.insert(3); 3,7,5 3 /\ 7 5 h.insert(1); 1,3,5,7 1 /\ 3 5 / 7 h.insert(2); 1,2,5,7,3 1 /\ 2 5 /\ 7 3 h.removeMin(); 2,3,5,7 2 /\ 3 5 / 7 CS 61B, Fall 2014, Game Trees, Quad Trees and Heaps 1 h.removeMin(); 3,7,5 3 /\ 7 5 (b) Consider an array based min-heap with N elements. What is the worst case running time of each of the following operations if we ignore resizing? What is the worst case running time if we take into account resizing? What are the advantages of using an array based heap vs. using a BST-based heap? Insert O(log N) Find Min O(1) Remove Min O(log N) Accounting for resizing: Insert O(N) Find Min O(1) Remove Min O(N) Using a BST is not space-efficient. (c) Your friend Alyssa P. Hacker challenges you to quickly implement a max-heap data structure - "Hah! I’ll just use my min-heap implementation as a template", you think to yourself.
    [Show full text]
  • Heaps a Heap Is a Complete Binary Tree. a Max-Heap Is A
    Heaps Heaps 1 A heap is a complete binary tree. A max-heap is a complete binary tree in which the value in each internal node is greater than or equal to the values in the children of that node. A min-heap is defined similarly. 97 Mapping the elements of 93 84 a heap into an array is trivial: if a node is stored at 90 79 83 81 index k, then its left child is stored at index 42 55 73 21 83 2k+1 and its right child at index 2k+2 01234567891011 97 93 84 90 79 83 81 42 55 73 21 83 CS@VT Data Structures & Algorithms ©2000-2009 McQuain Building a Heap Heaps 2 The fact that a heap is a complete binary tree allows it to be efficiently represented using a simple array. Given an array of N values, a heap containing those values can be built, in situ, by simply “sifting” each internal node down to its proper location: - start with the last 73 73 internal node * - swap the current 74 81 74 * 93 internal node with its larger child, if 79 90 93 79 90 81 necessary - then follow the swapped node down 73 * 93 - continue until all * internal nodes are 90 93 90 73 done 79 74 81 79 74 81 CS@VT Data Structures & Algorithms ©2000-2009 McQuain Heap Class Interface Heaps 3 We will consider a somewhat minimal maxheap class: public class BinaryHeap<T extends Comparable<? super T>> { private static final int DEFCAP = 10; // default array size private int size; // # elems in array private T [] elems; // array of elems public BinaryHeap() { .
    [Show full text]
  • L11: Quadtrees CSE373, Winter 2020
    L11: Quadtrees CSE373, Winter 2020 Quadtrees CSE 373 Winter 2020 Instructor: Hannah C. Tang Teaching Assistants: Aaron Johnston Ethan Knutson Nathan Lipiarski Amanda Park Farrell Fileas Sam Long Anish Velagapudi Howard Xiao Yifan Bai Brian Chan Jade Watkins Yuma Tou Elena Spasova Lea Quan L11: Quadtrees CSE373, Winter 2020 Announcements ❖ Homework 4: Heap is released and due Wednesday ▪ Hint: you will need an additional data structure to improve the runtime for changePriority(). It does not affect the correctness of your PQ at all. Please use a built-in Java collection instead of implementing your own. ▪ Hint: If you implemented a unittest that tested the exact thing the autograder described, you could run the autograder’s test in the debugger (and also not have to use your tokens). ❖ Please look at posted QuickCheck; we had a few corrections! 2 L11: Quadtrees CSE373, Winter 2020 Lecture Outline ❖ Heaps, cont.: Floyd’s buildHeap ❖ Review: Set/Map data structures and logarithmic runtimes ❖ Multi-dimensional Data ❖ Uniform and Recursive Partitioning ❖ Quadtrees 3 L11: Quadtrees CSE373, Winter 2020 Other Priority Queue Operations ❖ The two “primary” PQ operations are: ▪ removeMax() ▪ add() ❖ However, because PQs are used in so many algorithms there are three common-but-nonstandard operations: ▪ merge(): merge two PQs into a single PQ ▪ buildHeap(): reorder the elements of an array so that its contents can be interpreted as a valid binary heap ▪ changePriority(): change the priority of an item already in the heap 4 L11: Quadtrees CSE373,
    [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]
  • Leftist Heap: Is a Binary Tree with the Normal Heap Ordering Property, but the Tree Is Not Balanced. in Fact It Attempts to Be Very Unbalanced!
    Leftist heap: is a binary tree with the normal heap ordering property, but the tree is not balanced. In fact it attempts to be very unbalanced! Definition: the null path length npl(x) of node x is the length of the shortest path from x to a node without two children. The null path lengh of any node is 1 more than the minimum of the null path lengths of its children. (let npl(nil)=-1). Only the tree on the left is leftist. Null path lengths are shown in the nodes. Definition: the leftist heap property is that for every node x in the heap, the null path length of the left child is at least as large as that of the right child. This property biases the tree to get deep towards the left. It may generate very unbalanced trees, which facilitates merging! It also also means that the right path down a leftist heap is as short as any path in the heap. In fact, the right path in a leftist tree of N nodes contains at most lg(N+1) nodes. We perform all the work on this right path, which is guaranteed to be short. Merging on a leftist heap. (Notice that an insert can be considered as a merge of a one-node heap with a larger heap.) 1. (Magically and recursively) merge the heap with the larger root (6) with the right subheap (rooted at 8) of the heap with the smaller root, creating a leftist heap. Make this new heap the right child of the root (3) of h1.
    [Show full text]
  • Data Structures and Programming Spring 2016, Final Exam
    Data Structures and Programming Spring 2016, Final Exam. June 21, 2016 1 1. (15 pts) True or False? (Mark for True; × for False. Score = maxf0, Right - 2 Wrongg. No explanations are needed. (1) Let A1;A2, and A3 be three sorted arrays of n real numbers (all distinct). In the comparison model, constructing a balanced binary search tree of the set A1 [ A2 [ A3 requires Ω(n log n) time. × False (Merge A1;A2;A3 in linear time; then pick recursively the middle as root (in linear time).) (2) Let T be a complete binary tree with n nodes. Finding a path from the root of T to a given vertex v 2 T using breadth-first search takes O(log n) time. × False (Note that the tree is NOT a search tree.) (3) Given an unsorted array A[1:::n] of n integers, building a max-heap out of the elements of A can be performed asymptotically faster than building a red-black tree out of the elements of A. True (O(n) for building a max-heap; Ω(n log n) for building a red-black tree.) (4) In the worst case, a red-black tree insertion requires O(1) rotations. True (See class notes) (5) In the worst case, a red-black tree deletion requires O(1) node recolorings. × False (See class notes) (6) Building a binomial heap from an unsorted array A[1:::n] takes O(n) time. True (See class notes) (7) Insertion into an AVL tree asymptotically beats insertion into an AA-tree. × False (See class notes) (8) The subtree of the root of a red-black tree is always itself a red-black tree.
    [Show full text]
  • Heapsort Previous Sorting Algorithms G G Heap Data Structure P Balanced
    Previous sortinggg algorithms Insertion Sort 2 O(n ) time Heapsort Merge Sort Based off slides by: David Matuszek http://www.cis.upenn.edu/~matuszek/cit594-2008/ O(n) space PtdbMttBPresented by: Matt Boggus 2 Heap data structure Balanced binary trees Binary tree Recall: The dthfdepth of a no de iitditis its distance f rom th e root The depth of a tree is the depth of the deepest node Balanced A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children Left-justified n-2 (Max) Heap property: no node has a value greater n-1 than the value in its parent n Balanced Balanced Not balanced 3 4 Left-jjyustified binary trees Buildinggp up to heap sort How to build a heap A balanced binary tree of depth n is left- justified if: n it has 2 nodes at depth n (the tree is “full”)or ), or k How to maintain a heap it has 2 nodes at depth k, for all k < n, and all the leaves at dept h n are as fa r le ft as poss i ble How to use a heap to sort data Left-justified Not left-justified 5 6 The heappp prop erty siftUp A node has the heap property if the value in the Given a node that does not have the heap property, you can nodide is as large as or larger t han th e va lues iiin its give it the heap property by exchanging its value with the children value of the larger child 12 12 12 12 14 8 3 8 12 8 14 8 14 8 12 Blue node has Blue node has Blue node does not Blue node does not Blue node has heap property heap property have heap property have heap property heap property All leaf nodes automatically have
    [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]