CS 314H Honors Data Structures Fall 2017 Programming Assignment #6 Treaps Due November 12/15/17, 2017

Similar documents
CS 314H Algorithms and Data Structures Fall 2012 Programming Assignment #6 Treaps Due November 11/14/16, 2012

B Tree. Also, every non leaf node must have at least two successors and all leaf nodes must be at the same level.

Lecture 16 Notes AVL Trees

Binary Trees. Binary Search Trees

Search Trees. Chapter 11

Splay Trees Goodrich, Tamassia, Dickerson. Splay Trees 1

Lecture Notes on AVL Trees

AVL Trees. Reading: 9.2

Splay Trees. Splay Trees 1

Splay Trees 3/20/14. Splay Trees. Splay Trees are Binary Search Trees. note that two keys of equal value may be wellseparated (7,T) (1,Q) (1,C) (5,H)

Week 5. CS 400 Programming III

Week 2. TA Lab Consulting - See schedule (cs400 home pages) Peer Mentoring available - Friday 8am-12pm, 12:15-1:30pm in 1289CS

B + -trees. Kerttu Pollari-Malmi

Week 3 Web site:

Lecture 5. Treaps Find, insert, delete, split, and join in treaps Randomized search trees Randomized search tree time costs

Balanced Binary Search Trees. Victor Gao

Lecture 6: Analysis of Algorithms (CS )

CSE100. Advanced Data Structures. Lecture 8. (Based on Paul Kube course materials)

Sorting and Searching

9. Heap : Priority Queue

Sorting and Searching

Recall: Properties of B-Trees

Algorithms. Deleting from Red-Black Trees B-Trees

Splay Trees. (Splay Trees) Data Structures and Programming Spring / 27

Programming II (CS300)

Computational Optimization ISE 407. Lecture 16. Dr. Ted Ralphs

CS102 Binary Search Trees

(2,4) Trees. 2/22/2006 (2,4) Trees 1

Advanced Tree Structures

Data Structures and Algorithms Week 4

Midterm solutions. n f 3 (n) = 3

Outline. Definition. 2 Height-Balance. 3 Searches. 4 Rotations. 5 Insertion. 6 Deletions. 7 Reference. 1 Every node is either red or black.

DATA STRUCTURES AND ALGORITHMS. Hierarchical data structures: AVL tree, Bayer tree, Heap

Optional: Building a processor from scratch

Lecture: Analysis of Algorithms (CS )

2-3 Tree. Outline B-TREE. catch(...){ printf( "Assignment::SolveProblem() AAAA!"); } ADD SLIDES ON DISJOINT SETS

Friday Four Square! 4:15PM, Outside Gates

Uses for Trees About Trees Binary Trees. Trees. Seth Long. January 31, 2010

CS Fall 2010 B-trees Carola Wenk

CISC 235: Topic 4. Balanced Binary Search Trees

CSCI 136 Data Structures & Advanced Programming. Lecture 22 Fall 2018 Instructor: Bills

Data Structures and Algorithms

CS 157: Assignment 6

Data Structures and Algorithms

CS 350 : Data Structures B-Trees

Balanced Trees Part Two

It s Not Complex Just Its Solutions Are Complex!

Algorithms. AVL Tree

COSC160: Data Structures Balanced Trees. Jeremy Bolton, PhD Assistant Teaching Professor

Week 2. CS 400 Programming III. Read: Module 2 readings before lecture

Search Trees. COMPSCI 355 Fall 2016

g(x) h(x) f (x) = Examples sin x +1 tan x!

12.4 The Ellipse. Standard Form of an Ellipse Centered at (0, 0) (0, b) (0, -b) center

CSC 263 Lecture 4. September 13, 2006

UNIT III BALANCED SEARCH TREES AND INDEXING

CS350: Data Structures B-Trees

CMPS 2200 Fall 2017 B-trees Carola Wenk

Data Structures and Algorithms Chapter 4

2.4 Polynomial and Rational Functions

Multi-way Search Trees! M-Way Search! M-Way Search Trees Representation!

Multi-way Search Trees. (Multi-way Search Trees) Data Structures and Programming Spring / 25

Section 4 SOLUTION: AVL Trees & B-Trees

CSE 530A. B+ Trees. Washington University Fall 2013

CS350: Data Structures Heaps and Priority Queues

Parallel and Sequential Data Structures and Algorithms Lecture (Spring 2012) Lecture 16 Treaps; Augmented BSTs

Search Trees - 2. Venkatanatha Sarma Y. Lecture delivered by: Assistant Professor MSRSAS-Bangalore. M.S Ramaiah School of Advanced Studies - Bangalore

AVL Trees Heaps And Complexity

CMSC 425: Lecture 10 Basics of Skeletal Animation and Kinematics

CS251-SE1. Midterm 2. Tuesday 11/1 8:00pm 9:00pm. There are 16 multiple-choice questions and 6 essay questions.

Some Search Structures. Balanced Search Trees. Binary Search Trees. A Binary Search Tree. Review Binary Search Trees

Roberto s Notes on Differential Calculus Chapter 8: Graphical analysis Section 5. Graph sketching

1-1. Functions. Lesson 1-1. What You ll Learn. Active Vocabulary. Scan Lesson 1-1. Write two things that you already know about functions.

Advanced Set Representation Methods

Heaps Outline and Required Reading: Heaps ( 7.3) COSC 2011, Fall 2003, Section A Instructor: N. Vlajic

Balanced Search Trees. CS 3110 Fall 2010

Module 4: Dictionaries and Balanced Search Trees

CMPT 225. Red black trees

Data Structure and Algorithm Homework #3 Due: 2:20pm, Tuesday, April 9, 2013 TA === Homework submission instructions ===

Tree traversals and binary trees

Balanced Trees Part One

LINEAR PROGRAMMING. Straight line graphs LESSON

Multiway Search Trees. Multiway-Search Trees (cont d)

Section 1.4 Limits involving infinity

CS2223: Algorithms Sorting Algorithms, Heap Sort, Linear-time sort, Median and Order Statistics

Polynomial and Rational Functions

CS 171: Introduction to Computer Science II. Binary Search Trees

Readings. Priority Queue ADT. FindMin Problem. Priority Queues & Binary Heaps. List implementation of a Priority Queue

Data Structures and Algorithms. Roberto Sebastiani

Exponential and Logarithmic Functions

Augmenting Data Structures

Self-Balancing Search Trees. Chapter 11

Heaps. 2/13/2006 Heaps 1

Exponential and Logarithmic Functions

Module 4: Index Structures Lecture 13: Index structure. The Lecture Contains: Index structure. Binary search tree (BST) B-tree. B+-tree.

Lecture Overview. Readings. Recall: Binary Search Trees (BSTs) The importance of being balanced. AVL trees. Balance Insert. Other balanced trees

Computer Science 210 Data Structures Siena College Fall Topic Notes: Binary Search Trees

Principles of Algorithm Design

Check Skills You ll Need (For help, go to Lesson 1-2.) Evaluate each expression for the given value of x.

DELETION WITHOUT REBALANCING IN BINARY SEARCH TREES

Algorithms in Systems Engineering ISE 172. Lecture 16. Dr. Ted Ralphs

Transcription:

CS H Honors Data Structures Fall 207 Programming Assignment # Treaps Due November 2//7, 207 In this assignment ou will work individuall to implement a map (associative lookup) using a data structure called a treap, which is a combination of a tree and a heap. Your ke challenge in this assignment will be to carefull and thoroughl test our data structure, so ou will also be asked to design a testing program for our code. For this assignment, ou should not discuss testing strategies with other teams. You will again be writing Peer Reviews, with the following schedule. Treaps Program Due :00pm Sunda November 2 Peer Reviews Due :00pm Wednesda November Testing Report and Test Code Due :00pm Frida November 7 A treap is a binar search tree that uses randomiation to produce balanced trees. In addition to holding a ke-value pair (a map entr), each node of a treap holds a randoml chosen priorit value, such that the priorit values satisf the heap propert: Each node other than the root has a priorit that is at least as large as the priorities of its two children. An eample treap is shown in Figure, where the kes are shown at the top of each node and the priorities are shown at the bottom of each node. Notice that the kes obe the binar search tree (BST) propert and the priorities obe the heap propert. Because the kes obe the BST propert, a lookup operation can be performed just as with an BST. However, the insert and remove operations are slightl more comple. 0 7 7 0 2 Figure : A treap for a map with ke set {,,,,,, }. For each node, the ke is shown in the top half, while the priorit is shown in the bottom half. The priorit values are chosen at random, with larger numbers representing higher priorities. To insert a new node with ke k, we first perform the insertion at the appropriate leaf position according to the BST propert, eactl as in a binar search tree (See Figure 2). The node is assigned a randoml chosen priorit p, and because s parent ma have priorit less than p, the heap propert ma be violated. To restore the heap propert, we perform a rotation, making the parent of, as shown in Figure 2(b). Specificall, if is the left child of, then we rotate right around, and if is the right child of, then we rotate left around. Node now has a new parent, and the heap propert ma still be violated, requiring another rotation. In general, the heap propert is restored b rotating the new node up the treap as long as it has a parent with a lower priorit. Figure 2 shows an insertion requiring 2 rotations. To remove a node, we reverse the insertion. We rotate down the treap until it becomes a leaf, and then we simpl clip it off. At each step, the decision to rotate left or right is governed b the relative priorities of the children.

0 0 0 7 7 7 7 2 0 2 7 0 7 0 2 (a) (b) (c) Figure 2: Inserting new node into a treap. (a) The new node, with ke k= and priorit p=7, is added as a leaf according to the BST propert. The heap propert with respect to s parent is violated. (b) The situation after a right rotation around ; the heap propert with respect to s new parent is violated. (c) After a left rotation around, the heap propert is restored. The child with the higher priorit should become the new parent. Thus, if s left child has higher priorit than s right child, then we rotate right around. Conversel, if s right child has higher priorit than s left child, then we rotate left around. Figure illustrates a removal requiring 2 rotations. This removal reverses the insertion of Figure 2. All three map operations lookup, insert, and remove run in time O(h), where h is the height of the treap. It is not hard to show that a treap with n nodes has epected height Θ(log n). Note that the root of a treap is determined b the randoml chosen priorities. The node with the highest priorit is the root. Thus, the root node is equall likel to contain an of the map entries, regardless of the order in which the entries are inserted or removed. Consequentl, we epect that half of the entries will be in the left subtreap and the other half in the right subtreap. The analsis of treap height is therefore similar to the analsis of recursion depth in quicksort. 2 Your Assignment Implement a map using a treap. In particular, ou should implement the following interface. Your treap should store entries with kes that are Comparable objects and values that are an object. In both cases the Treap is generic on the eact tpe; kes have tpe K and values have tpe V. The lookup(k) method should return null if no entr with ke k is in the map. package assignment; public interface Treap<K etends Comparable<K>, V> etends Iterable<K> { V lookup(k ke); void insert(k ke, V value); V remove(k ke); Treap<K, V> [] split(k ke); void join(treap<k, V> t); void meld(treap<k, V> t) throws UnsupportedOperationEception; void difference(treap<k, V> t) throws UnsupportedOperationEception; double balancefactor() throws UnsupportedOperationEception; String tostring(); Iterator<K> iterator(); } 2

0 0 0 7 7 7 7 0 2 7 2 0 2 0 7 (a) (b) (c) Figure : Removing a node from a treap. (a) Node has two children, of which the left child has higher priorit. (b) After a right rotation around, node now has onl one child,. (c) After a left rotation around, node is now a leaf and can be clipped off like an ecessivel long toenail. A more detailed description of the interface is in Treap.java. Implement our treap-based map in TreapMap.java and make sure our implementation is generic; like Treap, TreapMap should take tpe parameters for K and V. The insert method. The remove method. Insertion into the treap should be implemented as outlined earlier. Removal from the treap should be implemented as outlined earlier. The split method. A treap T can be split, using a ke k, to produce two treaps, T and T 2, such that T contains all of the entries in T with ke less than k, and T 2 contains all of the entries in T with ke greater than or equal to k. To perform the split, we insert into T a new entr with ke k and priorit p =MAX PRIORITY, forming a new treap T. (MAX PRIORITY is defined b the Treap interface.) Because has the highest possible priorit, is the root of T, so the split has been accomplished with T being the left subtreap and T 2 being the right subtreap. You should not lose an value associated with k if k is alread in the treap, although it is ok if ou destro the old treap. The join method. The inverse of a split is join, in which two treaps, T and T 2, with all kes in T being smaller than all kes in T 2, are merged to form a new treap T. To perform the join, we create a new treap T with an arbitrar new root node and with T and T 2 as the left and right subtreaps. We then remove from T to form the joined result T. Split and join both take time O(h), where h is the height of the T (the input to split or the result of join). The epected height is Θ(log n), where n is the sie of T, so split and join both run in O(log n) epected time. More interestingl, split and join can be used as subroutines to meld two treaps or take the difference between two treaps. You ma implement these for additional karma. Testing Since the treap in this assignment is not part of a larger application, ou will not be able to use or test our treap without writing our own test program. Write a test suite to test our treap for correctness. You ma use JUnit or just write a program that manuall runs the appropriate tests. Your test suite should work with an implementation of the Treap interface. It should not find an errors in a correct implementation. For errors that it does find, it should produce output that would be useful to a human. You

are not required to test the portions that ou do not implement ourself, but ou should test everthing that ou do implement. Karma Three of the operations in the interface (balancefactor(), meld() and difference()) are optional. Implement them for etra karma. If ou do not implement them, throw an OperationNotSupportedEception. Meld A meld takes two treaps, T and T 2 and merges them into a new treap T, much like the Vulcan mind meld for which it is named. Unlike a join, a meld does not require an relationship between the kes in T and T 2. Meld is a naturall recursive procedure and should be able to meld two treaps of sie n and m (m n) in O(m log(n/m)) time. Describe how ou meld treaps and how our algorithm meets the specified asmptotic time bound..2 Difference The difference between two treaps, T and T 2, is a treap T containing the kes of T with an kes in T 2 removed. The difference can also be computed recursivel and also runs in O(m log(n/m)) time. Describe how ou take a difference and how our algorithm satisfies this time bound.. Diagnosing Problems Through Testing Tpicall, the goal of a test program is to identif bugs. With some additional work, ou can attempt to diagnose common problems b observing the behavior of the program. For eample, if the iterator misses one ke, it is likel that the missing ke is the first or last ke added. A test program can attempt to verif this hpothesis and provide a suggestion to the user. Can ou use our test program to assist in finding common mistakes?. Balance Statistics It would be useful to know how balanced or imbalanced our treap is. The balance factor is the ratio between the height of the treap and the minimum possible height. A perfectl balanced treap will have a balance factor of.0. Include observations on how well the treap seems to keep itself balanced in our report. What to Turn in Ecept for the specific names of the files, the directions for what to turn in are the same as for Assignment, but we repeat them below for our convenience.. Deadline Submit just our code. If our submission is late, our reviewers ma choose whether the wish to review our code, so it is in our best interest to not take an late das for this deadline. Regardless of whether ou submit our assignment on time, ou will be assigned several projects to review. Turn in TreapMap.java and an other files necessar to build and run our implementation. Be sure to comment all classes, methods, and fields, and an non-obvious portions of our assignment. Your code should be packaged in a src director and all of our classes should be in the assignment package. Make sure that our code compiles and that all classes have the correct names and are in the correct files. Not reall.

.2 Deadline 2 Your peer reviews are due. If ou turn these in late, our grade will be penalied.. Deadline Your report, our revised code (optional), testing report, and reviewer grades are due. You have the option to fi our code based on the feedback ou received. The testing report should cover an insights gained from the review process, including what ou did, how ou did it, what ou learned, what ou learned from the reviews of our own code, what this revealed about our code, and what changes ou made/would make given more time. Acknowledgments. We thank Bobb Blumofe for the original version of this assignment, and we thank Walter Chang, Arthur Peters, and Ashlie Martine for their subsequent modifications.