ICS 311, Fall 2017, Problem Set 04, Topics 7 & 8

Similar documents
Binary Search Trees. What is a Binary Search Tree?

Algorithms, Spring 2014, CSE, OSU Lecture 4: Binary search trees

Different binary search trees can represent the same set of values (Fig.2). Vladimir Shelomovskii, Unitech, Papua New Guinea. Binary search tree.

Chapter 3. Graphs CLRS Slides by Kevin Wayne. Copyright 2005 Pearson-Addison Wesley. All rights reserved.

Binary search trees. Binary search trees are data structures based on binary trees that support operations on dynamic sets.

CS 380 ALGORITHM DESIGN AND ANALYSIS

Binary Search Tree. Sorting and Searching in a Dynamic Set. S.V. N. (vishy) Vishwanathan. University of California, Santa Cruz

Algorithms, Spring 2014, CSE, OSU Lecture 5: Red-black trees

Binary search trees 3. Binary search trees. Binary search trees 2. Reading: Cormen et al, Sections 12.1 to 12.3

Announcements. Problem Set 2 is out today! Due Tuesday (Oct 13) More challenging so start early!

We assume uniform hashing (UH):

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

CSE2331/5331. Topic 6: Binary Search Tree. Data structure Operations CSE 2331/5331

Figure 4.1: The evolution of a rooted tree.

Binary Trees. Recursive definition. Is this a binary tree?

Binary Tree. Preview. Binary Tree. Binary Tree. Binary Search Tree 10/2/2017. Binary Tree

Module 4: Priority Queues

Binary Search Trees, etc.

Binary Search Trees. July 13th, Computer Information Systems. c 2009 Vaidė Narváez

Solutions for Algorithm Design Exercises and Tests

Chapter 13. Michelle Bodnar, Andrew Lohr. April 12, 2016

Search Trees: BSTs and B-Trees

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

Properties of red-black trees

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 21, Antonio Carzaniga 1

Design and Analysis of Algorithms Lecture- 9: Binary Search Trees

How many leaves on the decision tree? There are n! leaves, because every permutation appears at least once.

Binary Search to find item in sorted array

Search Trees. Undirected graph Directed graph Tree Binary search tree

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

The divide-and-conquer paradigm involves three steps at each level of the recursion: Divide the problem into a number of subproblems.

Analyzing Complexity of Lists

CMSC 441: Algorithms. Hillol Kargupta, Professor.

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

Second Examination Solution

ץע A. B C D E F G H E, B ( -.) - F I J K ) ( A :. : ע.)..., H, G E (. י : י.)... C,A,, F B ( 2

Fundamental mathematical techniques reviewed: Mathematical induction Recursion. Typically taught in courses such as Calculus and Discrete Mathematics.

Trees and Graphs Shabsi Walfish NYU - Fundamental Algorithms Summer 2006

Quiz 1 Solutions. (a) f(n) = n g(n) = log n Circle all that apply: f = O(g) f = Θ(g) f = Ω(g)

CSC236 Week 5. Larry Zhang

Red-Black Trees. 1 Properties of red-black trees

CS 173 [A]: Discrete Structures, Fall 2012 Homework 8 Solutions

Chapter 4: Trees. 4.2 For node B :

Balanced search trees

SFU CMPT Lecture: Week 9

ECE250: Algorithms and Data Structures Midterm Review

would be included in is small: to be exact. Thus with probability1, the same partition n+1 n+1 would be produced regardless of whether p is in the inp

Multiple-choice (35 pt.)

Problem Set 5. MIT students: Each problem should be done on a separate sheet (or sheets) of three-hole punched paper.

Lecture 3. Recurrences / Heapsort

Algorithms. Red-Black Trees

CMSC351 - Fall 2014, Homework #2

Problem Set 5 Solutions

Exercise 1 : B-Trees [ =17pts]

Tree Travsersals and BST Iterators

Binary Search Trees Treesort

Indicate the option which most accurately completes the sentence.

Lecture 6. Binary Search Trees and Red-Black Trees

Instructions. Definitions. Name: CMSC 341 Fall Question Points I. /12 II. /30 III. /10 IV. /12 V. /12 VI. /12 VII.

Binary search trees. Support many dynamic-set operations, e.g. Search. Minimum. Maximum. Insert. Delete ...

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. ECE 345 Algorithms and Data Structures Fall 2012

Part 2: Balanced Trees

Today: Amortized Analysis (examples) Multithreaded Algs.

Introduction to Algorithms March 11, 2009 Massachusetts Institute of Technology Spring 2009 Professors Sivan Toledo and Alan Edelman Quiz 1

Data Structures and Algorithms Week 4

Midterm solutions. n f 3 (n) = 3

CS 241 Analysis of Algorithms

CS2 Algorithms and Data Structures Note 1

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

Binary search trees (BST) Binary search trees (BST)

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

Analysis of Algorithms

Lecture 23: Binary Search Trees

CSC236H Lecture 5. October 17, 2018

Multi-Way Search Trees

Solutions to Exam Data structures (X and NV)

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

CS 231 Data Structures and Algorithms Fall Binary Search Trees Lecture 23 October 29, Prof. Zadia Codabux

CPSC 311: Analysis of Algorithms (Honors) Exam 1 October 11, 2002

CSC148 Week 7. Larry Zhang

Searching Algorithms/Time Analysis

Binary Search Trees. Antonio Carzaniga. Faculty of Informatics University of Lugano. October 22, Antonio Carzaniga 1

Outline for Today. How can we speed up operations that work on integer data? A simple data structure for ordered dictionaries.

EECS 2011M: Fundamentals of Data Structures

Data Structures and Algorithms Chapter 4

Binary Trees. This recursive defnition prefgures the patern of most algorithms we use on this data structure, as we will see below.

CSE 373: AVL trees. Michael Lee Friday, Jan 19, 2018

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Week 8. BinaryTrees. 1 Binary trees. 2 The notion of binary search tree. 3 Tree traversal. 4 Queries in binary search trees. 5 Insertion.

Assume you are given a Simple Linked List (i.e. not a doubly linked list) containing an even number of elements. For example L = [A B C D E F].

Here is a recursive algorithm that solves this problem, given a pointer to the root of T : MaxWtSubtree [r]

V Advanced Data Structures

Treaps. 1 Binary Search Trees (BSTs) CSE341T/CSE549T 11/05/2014. Lecture 19

If you took your exam home last time, I will still regrade it if you want.

BST Deletion. First, we need to find the value which is easy because we can just use the method we developed for BST_Search.

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

Cpt S 223 Fall Cpt S 223. School of EECS, WSU

CSE 502 Class 16. Jeremy Buhler Steve Cole. March A while back, we introduced the idea of collections to put hash tables in context.

ECE250: Algorithms and Data Structures Binary Search Trees (Part A)

1 Leaffix Scan, Rootfix Scan, Tree Size, and Depth

Transcription:

ICS 311, Fall 2017, Problem Set 04, Topics 7 & 8 Due by midnight Tuesday 2/16. 35 points. #1. Peer Credit Assignment 1 Point Extra Credit for replying Please list the names of the other members of your peer group for class 1/25 and 1/27 and the number of points you think they deserve for their participation in group work on the two days combined. You have a total of 6 points to allocate across all of your peers. You can distribute the points equally, give them all to one person, or do something in between. You need not allocate all the points available to you. You cannot allocate any points to yourself! Points allocated to yourself will not be recorded. #2. Master Method Practice (6 pts) Use the Master Method to give tight Θ bounds for the following recurrence relations. Show a, b, and f ( n ). Then explain why it fits one of the cases, choosing ε where applicable. Write and simplify the final Θ result (a) T ( n ) = 2 T ( n /4) + n (b) T(n) = 3T(n/9) + n (c) T ( n ) = 7 T ( n /3) + n #3. Substitution (9 pts) Use substitution as directed below to solve T ( n ) = 7 T ( n /3) + n It is strongly recommended that you read page 85-86 "Subtleties" before trying this!

(a) First, use the result from the Master Method in 2c as your "guess" and inductive assumption. (When removing Θ, just use one constant c rather than c 1 and c 2.) Take the proof up to where it fails and say where and why it fails. Remember that the steps are: 1. Write the definition of T(n) 2. Substitute the inductive hypothesis into this guess on the right hand side. 3. Simplify the right hand side algebraically to try to derive the exact form of your guess. (b) Redo the proof, but subtracting dn from the guess to construct a new guess. (Once you have done part (a) you should see why we subtract dn: we want it to be inside our guess to counteract an extra term.) Don t forget to use the new guess when you do substitution. This time it should succeed: once you simplify the result to be similar to your guess you will solve for d to determine the exact guess. #4. Binary Search Tree Proof (9 pts) The procedure for deleting a node in a binary search tree relies on a fact that was given without proof in the notes: Lemma: If a node X in a binary search tree has two children, then its successor S has no left child and its predecessor P has no right child. In this exercise you will prove this lemma. Although the proof can be generalized to duplicate keys, for simplicity assume no duplicate keys. The proofs are symmetric. ( Hints: Rule out where the successor cannot be to narrow down to where it must be. Draw Pictures!!! ) (a) Prove by contradiction that the successor S cannot be an ancestor or cousin of X, so S must be in a subtree rooted at X. (b) Identify and prove the subtree of X that successor S must be in. (c) Show by contradiction that successor S cannot have a left child. (d) Indicate how this proof would be changed for predecessor.

#5. Deletion in Binary Search Trees (4 pts) Consider Tree-Delete (page 298, and also copied below). TREE-DELETE(T, z) 1 if z.left == NIL 2 TRANSPLANT(T, z, z.right) 3 elseif z.right == NIL 4 TRANSPLANT(T, z, z.left) 5 else y = TREE-MINIMUM(z.right) // successor 6 if y.p!= z 7 TRANSPLANT(T, y, y.right) 8 y.right = z.right 9 y.right.p = y 10 TRANSPLANT(T, z, y) 11 y.left = z.left 12 y.left.p = y (a) How does this code rely on the lemma you just proved in problem 4? (b) When node z has two children, we arbitrarily decide to replace it with its successor. We could just as well replace it with its predecessor. (Some have argued that if we choose randomly between the two options we will get more balanced trees.) Rewrite Tree-Delete to use the predecessor rather than the successor. Modify this code just as you need to and underline or boldface the changed portions. #6. Constructing Balanced Binary Search Trees (7 pts) Suppose you have some data keys sorted in an array and you want to construct a balanced binary search tree from them. Assume a tree node representation TreeNode that includes instance variables key, left, and right. (a) Write pseudocode (or Java if you wish) for an algorithm that constructs the tree and returns the root node. (We won't worry about making the enclosing BinaryTree class

instance.) You will need to use methods for making a new TreeNode, and for setting its left and right children. Hints: First, identify the array location of the key that would have to be the root of the balanced BST. Now think about how BinarySearch works on the array. Which item does it access first in any given subarray it is called with? Using a similar strategy a simple recursive algorithm is possible. (b) What is the Θ cost to construct the tree? Justify your answer. (c) Compare the expected runtime of BinarySearch on the array to the expected runtime of BST TreeSearch in the tree you just constructed. Have we saved time?