CS 315 Data Structures mid-term 2

Similar documents
CS 315 Data Structures Spring 2012 Final examination Total Points: 80

8. Binary Search Tree

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

Computer Science Foundation Exam

CSE332 Summer 2010: Midterm Exam Sample Solutions

Fall, 2015 Prof. Jungkeun Park

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Exam Data structures DIT960/DAT036

University of Waterloo Department of Electrical and Computer Engineering ECE250 Algorithms and Data Structures Fall 2014

Dictionaries. Priority Queues

CSE 332 Autumn 2013: Midterm Exam (closed book, closed notes, no calculators)

CS 350 : Data Structures Binary Search Trees

CSE 332 Spring 2014: Midterm Exam (closed book, closed notes, no calculators)

CS102 Binary Search Trees

Exam Data structures DAT036/DAT037/DIT960

CS350: Data Structures Binary Search Trees

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

CS 261 Data Structures. AVL Trees

Data Structures in Java

Section 1: True / False (1 point each, 15 pts total)

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

Second Examination Solution

CSE 373 Autumn 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

University of Illinois at Urbana-Champaign Department of Computer Science. Final Examination

! Tree: set of nodes and directed edges. ! Parent: source node of directed edge. ! Child: terminal node of directed edge

Algorithms. AVL Tree

Lecture 23: Binary Search Trees

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

COMP 250 Midterm #2 March 11 th 2013

Lec 17 April 8. Topics: binary Trees expression trees. (Chapter 5 of text)

Computer Science Foundation Exam

CSE 373 Spring 2010: Midterm #1 (closed book, closed notes, NO calculators allowed)

CSCI2100B Data Structures Heaps

CSE 373 Winter 2009: Midterm #1 (closed book, closed notes, NO calculators allowed)

Binary Heaps. COL 106 Shweta Agrawal and Amit Kumar

Priority Queues. 04/10/03 Lecture 22 1

A set of nodes (or vertices) with a single starting point

Data Structures. Giri Narasimhan Office: ECS 254A Phone: x-3748

Binary Trees, Binary Search Trees

CSE373 Fall 2013, Midterm Examination October 18, 2013

CSCI Trees. Mark Redekopp David Kempe

Tree: non-recursive definition. Trees, Binary Search Trees, and Heaps. Tree: recursive definition. Tree: example.

BRONX COMMUNITY COLLEGE of the City University of New York DEPARTMENT OF MATHEMATICS AND COMPUTER SCIENCE

Trees. (Trees) Data Structures and Programming Spring / 28

CSE 332 Winter 2015: Midterm Exam (closed book, closed notes, no calculators)

CSI33 Data Structures

EECS 311 Data Structures Midterm Exam Don t Panic!

Introduction. for large input, even access time may be prohibitive we need data structures that exhibit times closer to O(log N) binary search tree

Lecture 15 Binary Search Trees

CS 2150 Final Exam, Spring 2018 Page 1 of 10 UVa userid:

Sample Solutions CSC 263H. June 9, 2016

CS 223: Data Structures and Programming Techniques. Exam 2

B-Trees. Version of October 2, B-Trees Version of October 2, / 22

Exam Datastrukturer. DIT960 / DIT961, VT-18 Göteborgs Universitet, CSE

BINARY SEARCH TREES cs2420 Introduction to Algorithms and Data Structures Spring 2015

CS211, LECTURE 20 SEARCH TREES ANNOUNCEMENTS:

COMP Analysis of Algorithms & Data Structures

Lecture Notes on Binary Search Trees

Test #2. Login: 2 PROBLEM 1 : (Balance (6points)) Insert the following elements into an AVL tree. Make sure you show the tree before and after each ro

Course goals. exposure to another language. knowledge of specific data structures. impact of DS design & implementation on program performance

CS-301 Data Structure. Tariq Hanif

ECE242 Data Structures and Algorithms Fall 2008

COMP Analysis of Algorithms & Data Structures

Lecture 15 Notes Binary Search Trees

CS 361, Lecture 21. Outline. Things you can do. Things I will do. Evaluation Results

9/29/2016. Chapter 4 Trees. Introduction. Terminology. Terminology. Terminology. Terminology

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

Sorted Arrays. Operation Access Search Selection Predecessor Successor Output (print) Insert Delete Extract-Min

Section 1: True / False (2 points each, 30 pts total)

CSE 332, Spring 2010, Midterm Examination 30 April 2010

Data Structures and Algorithms

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

CS 251, LE 2 Fall MIDTERM 2 Tuesday, November 1, 2016 Version 00 - KEY

Computer Science 302 Spring 2007 Practice Final Examination: Part I

CS 310: Priority Queues and Binary Heaps

Augmenting Data Structures

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

Balanced Search Trees. CS 3110 Fall 2010

CSE Data Structures and Introduction to Algorithms... In Java! Instructor: Fei Wang. Mid-Term Exam. CSE2100 DS & Algorithms 1

Midterm solutions. n f 3 (n) = 3

Trees 2: Linked Representation, Tree Traversal, and Binary Search Trees

COT 5407: Introduction. to Algorithms. Giri NARASIMHAN. 1/29/19 CAP 5510 / CGS 5166

CS 206 Introduction to Computer Science II

Priority Queues Heaps Heapsort

(D) There is a constant value n 0 1 such that B is faster than A for every input of size. n n 0.

CSE 326: Data Structures Lecture #8 Binary Search Trees

CSE 373 OCTOBER 11 TH TRAVERSALS AND AVL

Practice Midterm Exam Solutions

Dynamic Access Binary Search Trees

CS 216 Exam 1 Fall SOLUTION

Section 05: Solutions

CS 310 B-trees, Page 1. Motives. Large-scale databases are stored in disks/hard drives.

Binary Trees. BSTs. For example: Jargon: Data Structures & Algorithms. root node. level: internal node. edge.

You must print this PDF and write your answers neatly by hand. You should hand in the assignment before recitation begins.

University of Illinois at Urbana-Champaign Department of Computer Science. Second Examination

Self-Balancing Search Trees. Chapter 11

AVL Trees. See Section 19.4of the text, p

CMPSCI 187: Programming With Data Structures. Lecture #28: Binary Search Trees 21 November 2011

Section 4 SOLUTION: AVL Trees & B-Trees

Friday Four Square! 4:15PM, Outside Gates

Transcription:

CS 315 Data Structures mid-term 2 1) Shown below is an AVL tree T. Nov 14, 2012 Solutions to OPEN BOOK section. (a) Suggest a key whose insertion does not require any rotation. 18 (b) Suggest a key, if it exists, whose insertion requires a single rotation. 45 (c) Suggest a key, if it exists, whose insertion requires a double rotation. 50 (d) Given that the last operation was inserting the key 48 and that this insertion involved a rotation, exhibit the tree prior to inserting 48. One solution is as follows: (e) Exhibit that the AVL tree that results from inserting 39 into T.

2) (a) Suggest a data structure that can support the operations INSERT and FINDMIN with worstcase O(1) cost per operation. Your solution should include the description of the data members of the class that supports the operations (illustrated with a diagram), and actual code to implement INSERT and FINDMIN operations. FINDMIN will return the smallest key currently stored, but will not remove it. (Note: there is a simple solution to this problem.) Informal outline: Use an unsorted array KEYS to keep the keys inserted. Insertion can be done in O(1) time by simply adding the new key to the end of the array. A data member LastPtr will point to the last member of the array. In addition, keep a variable MinPtr that holds the index of the min key. This will allow us to perform FINDMIN in O(1) time. But this will require a small change to INSERT. Compare the new key with the current min and update MinPtr if needed. Code for INSERT and FINDMIN: void INSERT(<comparable> x) { KEYS[++LastPtr] = x; if (x < KEYS[MinPtr]) MinPtr = LastPtr; <comparable> FINDMIN() { return KEYS[MinPtr];

(b) Suggest a simple modification to a standard data structure studied in class to support the operations SEARCH, INSERT and DELETE in O(log n) time in the worst-case, and FINDMIN in O(1) time in the worst-case. You don t have to write any code for this problem. Describe the changes you would make to a standard data structure informally and explain how this would meet the performance goals. Since the set of operations we want to support includes all the dictionary operations and we require worst-case O(log n) performance, the only choice we have is to use AVL tree. But using AVL tree directly will not give us O(1) bound for FINDMIN. So we need to add an extra data member - a pointer to the node containing the minimum key. (Tree* MinPtr) This will immediately allow us to implement FINDMIN in O(1) time. (Simply return MinPtr->key will do it.) However, this will require us to modify the code for insert and delete. A simple way to do this is as follows: after every INSERT or DELETE operation, start at the root of the tree and follow a chain of left links until you reach a node with no left child. Reset MinPtr to point to this node. Note that this additional cost is within acceptable bound for Insert and Delete operations. 3) When assigned to write a function checkbst to test if a given binary tree is a binary search tree, a student wrote the following program. (The binary tree has the usual fields key, left and right.) bool checkbst(tree* T) { // test if T points to a binary search tree if (T == NULL) return true; if!(checkbst(t->left)) return false; if!(checkbst(t->right)) return false; if (T->left!= NULL) && (T->left->key > T->key)) return false; if (T->right!= NULL) && (T->right->key < T->key) return false;

return true; (a) If it exists, exhibit a binary tree that is not a binary search tree for which the above program will produce a correct output. We want a non-bst that is correctly classified by the above function as a non- BST. One such tree is (b) If it exists, exhibit a binary tree that is not a binary search tree for which the above program will produce a wrong output. We want a non-bst that is incorrectly classified by the above function as a BST. One such tree is: (c) If it exists, exhibit a binary tree that is a binary search tree for which the above program will produce a wrong output. We want a BST that is incorrectly classified by the above function as a BST. Such tree does not exist since all the conditions tested by the above function are required to be satisfied by a BST. (d) If it exists, exhibit a binary tree that is a binary search tree for which the above program will produce a correct output. We want a BST that is correctly classified by the above function as a BST. One such tree is

4) Consider a binary search tree, in which in addition to the standard fields (key, left and right) each node has an integer field called size, which stores the number of elements in the subtree rooted at this node. In a range query we are given two key values x1 and x2 and wish to return a count of the number of elements in the tree whose key value x satisfies x1 < x <= x2. Write a function to implement this operation, and briefly explain how your algorithm works. Your code should closely resemble c++ and should run in O(h) time, where h is the height of the tree. Informal description of the algorithm: We write a helper function called CountLessThan that takes as input a tree pointer T and a key K, and returns the number of number of nodes in the tree that are < K. The range query can be answered by computing CountLessThan(x2) CountLessThan(x1), then adding 1 if x2 is in the tree. int range(tree* T, int x1, int x2) { return CountLessThan(T, x2) CountLessThan(T, x1)+ find(t, x2); Here we assume that find returns 0 or 1 depending on whether x2 is (is not) in the tree T. The function CountLessThan works as follows: it takes as input a tree pointer T and int K. If the root key x <= K, then we can recursively return CountLessThan(T->left, K). Else, x > K. Thus, all the keys in the left subtree together with the root key should be included in the count. Add to this, the value returned by the recursive call CountLessThan(T->right, K). Note that the number of times the call is made is O(h) and after each call, a constant number of additional operations are performed thus the total cost is O(h):

5) int CountLessThan(Tree* T, int K) { if (T== NULL) return 0; if (T->key <= K) return CountLessThn(T->left, K); else { int temp; if (T->left == NULL) temp = 0; else temp = T->left->size; return 1 + temp + CountLessThan(T->right, K); Here h(x) denotes the primary hash function and g(x) denotes the secondary hash function. (a) 2, 3, 4 (b) 2, 3, 6, 1 (c) 2, 9, 6, 3, 0 The last index is the position in which the key is inserted.