Solutions. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then.

Similar documents
Algorithms. AVL Tree

Analysis of Algorithms

Fundamental Algorithms

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

AVL Tree Definition. An example of an AVL tree where the heights are shown next to the nodes. Adelson-Velsky and Landis

COMP Analysis of Algorithms & Data Structures

Midterm solutions. n f 3 (n) = 3

COMP Analysis of Algorithms & Data Structures

CSE 332, Spring 2010, Midterm Examination 30 April 2010

AVL Trees Goodrich, Tamassia, Goldwasser AVL Trees 1

Balanced Binary Search Trees

Properties of red-black trees

COMP171. AVL-Trees (Part 1)

We will show that the height of a RB tree on n vertices is approximately 2*log n. In class I presented a simple structural proof of this claim:

ECE250: Algorithms and Data Structures AVL Trees (Part A)

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

CSI33 Data Structures

Figure 4.1: The evolution of a rooted tree.

Solutions. (a) Claim: A d-ary tree of height h has at most 1 + d +...

Inf 2B: AVL Trees. Lecture 5 of ADS thread. Kyriakos Kalorkoti. School of Informatics University of Edinburgh

Lecture 6: Analysis of Algorithms (CS )

Search Trees - 1 Venkatanatha Sarma Y

CSCI2100B Data Structures Trees

Search Trees. Undirected graph Directed graph Tree Binary search tree

Red-Black Trees. Based on materials by Dennis Frey, Yun Peng, Jian Chen, and Daniel Hood

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

COMP Analysis of Algorithms & Data Structures

Analysis of Algorithms

Dynamic Access Binary Search Trees

Dynamic Access Binary Search Trees

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

Efficient Access to Non-Sequential Elements of a Search Tree

AVL Trees. (AVL Trees) Data Structures and Programming Spring / 17

Part 2: Balanced Trees

Friday Four Square! 4:15PM, Outside Gates

Chapter 4: Trees. 4.2 For node B :

We assume uniform hashing (UH):

CSCI 136 Data Structures & Advanced Programming. Lecture 25 Fall 2018 Instructor: B 2

Lecture 16 Notes AVL Trees

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

CS 261 Data Structures. AVL Trees

CMSC351 - Fall 2014, Homework #2

Lecture 13: AVL Trees and Binary Heaps

Binary search trees (chapters )

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

Search Trees. Chapter 11

Algorithms. Deleting from Red-Black Trees B-Trees

Lecture 16 AVL Trees

Binary search trees (chapters )

10/23/2013. AVL Trees. Height of an AVL Tree. Height of an AVL Tree. AVL Trees

Lecture: Analysis of Algorithms (CS )

CSci 231 Final Review

Solutions for Algorithm Design Exercises and Tests

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

CHAPTER 10 AVL TREES. 3 8 z 4

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

MAT 7003 : Mathematical Foundations. (for Software Engineering) J Paul Gibson, A207.

EXAM ELEMENTARY MATH FOR GMT: ALGORITHM ANALYSIS NOVEMBER 7, 2013, 13:15-16:15, RUPPERT D

AVL Trees / Slide 2. AVL Trees / Slide 4. Let N h be the minimum number of nodes in an AVL tree of height h. AVL Trees / Slide 6

Course Review. Cpt S 223 Fall 2009

Advanced Set Representation Methods

21# 33# 90# 91# 34# # 39# # # 31# 98# 0# 1# 2# 3# 4# 5# 6# 7# 8# 9# 10# #

lecture17: AVL Trees

Binary search trees. We can define a node in a search tree using a Python class definition as follows: class SearchTree:

Topics. Trees Vojislav Kecman. Which graphs are trees? Terminology. Terminology Trees as Models Some Tree Theorems Applications of Trees CMSC 302

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

TREES. Trees - Introduction

CS60020: Foundations of Algorithm Design and Machine Learning. Sourangshu Bhattacharya

Binary Search Trees Treesort

LECTURE 18 AVL TREES

Section 05: Solutions

AVL Trees (10.2) AVL Trees

Multi-Way Search Trees

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

Search Trees (Ch. 9) > = Binary Search Trees 1

Trees. Reading: Weiss, Chapter 4. Cpt S 223, Fall 2007 Copyright: Washington State University

Binary Search Trees, etc.

Questions. 6. Suppose we were to define a hash code on strings s by:

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

March 20/2003 Jayakanth Srinivasan,

Course Review for Finals. Cpt S 223 Fall 2008

Balanced search trees

Section 05: Solutions

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

Data Structures and Algorithms

Lecture 8: The Traveling Salesman Problem

Announcements. Midterm exam 2, Thursday, May 18. Today s topic: Binary trees (Ch. 8) Next topic: Priority queues and heaps. Break around 11:45am

Ch04 Balanced Search Trees

Binary Trees, Binary Search Trees

CS 315 Data Structures mid-term 2

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

RANK-PAIRING HEAPS BERNHARD HAEUPLER 1, SIDDHARTHA SEN 2,4, AND ROBERT E. TARJAN 3,4

Balanced Trees Part Two

Computer Science 302 Spring 2007 Practice Final Examination: Part I

Lesson 21: AVL Trees. Rotation

Efficient Non-Sequential Access and More Ordering Choices in a Search Tree

Recursively Defined Functions

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

Final Exam. EECS 2011 Prof. J. Elder - 1 -

Balanced Search Trees. CS 3110 Fall 2010

Transcription:

Assignment 3 1. Exercise [11.2-3 on p. 229] Modify hashing by chaining (i.e., bucketvector with BucketType = List) so that BucketType = OrderedList. How is the runtime of search, insert, and remove affected? 2. Exercise [11.2-5 on p. 229] Assume hashing by chaining as above (and as in fsu:thashtable<>). Denote by U the universe of keys. (This can be the set of all objects of the data type stored in the table, or some subset of that constrained by the particular use of the table.) Show that if U km, where m is the number of buckets, then there is a subset of U of size k consisting of keys that all hash to the same bucket. (Note that k is just an integer, not the size of anything.) 3. For the following binary tree, complete the table showing each step in a traversal and the number of edges covered by each step (begin, next,..., next): iteration step current location no of edge moves Q -------------- ---------------- ---------------- / \ 1. initialize W E 2. ++ \ / \ 3. ++ R T Y... 7. ++ (null) 4. Describe an order in which vertices of a BST can be saved to ensure that reconstructing a BST from the saved data will result in the same tree. Argue correctness of your assertion. 5. Use the result of Assignment 1 Problem 3(e) [problem 4-5(e) in the textbook] to prove: Height-balanced binary trees satisfy height(n) = Θ(log n), where n = number of vertices in the tree. 1

2 Solutions Problem 1: Modify hashing by chaining (i.e., bucketvector with BucketType = List) so that BucketType = OrderedList. How is the runtime of search, insert, and remove affected? Effect on Search and Remove: Sequential search in an ordered list is, on average, 1/2 the size of the list. The expected search time is unchanged asymptotically [Θ(1) when the number of buckets is Θ(n)], although the measured runtimes would be decreased somewhat. Effect on Insert: Insert consists of search followed by insertion into the ordered list. This increases the insert time by 1/2 the size of the bucket, on average. Again, the expected insert time is not changed asymptotically, but the measured times would be increased somewhat. Problem 2: Assume hashing by chaining as above (and as in fsu:thashtable<>). Denote by U the universe of keys. (This can be the set of all objects of the data type stored in the table, or some subset of that constrained by the particular use of the table.) Show that if U nm, where m is the number of buckets, then there is a subset of U of size n consisting of keys that all hash to the same bucket. Suppose we insert all elements of U into the table, and let n(b) be the number of elements of U that hash to bucket b. Then m 1 U = n(b) nm. 0 Since there are m items in the summation, at least one n(b) must be at least as large as n, for otherwise U = m 1 0 n(b) < m 1 0 n = nm. Problem 3: For the following binary tree, complete the table showing each step in a traversal and the number of edges covered by each step (begin, next,..., next): iteration step current location no of edge moves Q -------------- ---------------- ---------------- / \ 1. initialize W E 2. ++ \ / \... R T Y 7. ++ (null)

3 Here is the completed table: iteration step current location no of edge moves Q -------------- ---------------- ---------------- / \ 1. initialize W 2 W E 2. ++ R 1 \ / \ 3. ++ Q 2 R T Y 4. ++ T 2 5. ++ E 1 6. ++ Y 1 7. ++ (null) 3 Note the total of 12 edge moves, which is equal to 2n, n = 6. This is the number of edge moves during an entire traversal, since each edge is traversed twice (once descending, once ascending) and there are two extra moves, ( jumping on and jumping off ), for a todal of 2e + 2 = 2(e + 1) = 2n. Problem 4: Describe an order in which vertices of a BST can be saved to ensure that reconstructing a BST from the saved data will result in the same tree. Argue correctness of your assertion. Save the node data in level order (using a level order traversal, or a level order iterator) or pre-order (using a preorder traversal or preorder iterator). When reconstructing the BST, the data will be inserted in this order, because a former left child is encountered after its former parent and before its former right child. But this means the order will also have been preserved, so that the new insertion will end up as the left child of the previously inserted former parent. Similarly for the former right child. Problem 5: Use the result of Assignment 1 Problem 3(e) [problem 4-5(e) in the textbook] to prove: Height-balanced binary trees satisfy height(n) = Θ(log n), where n = number of vertices in the tree. An AVL tree is a binary tree in which the heights of the left and right subtrees of each node differ by at most 1. Note this definition is equivalent to the recursive version: an AVL tree is a binary tree in which the heights of the left and right subtrees of the root differ by at most 1 and in which the left and right subtrees are again AVL trees. The name AVL derives from the names of the two inventors of the technology, G.M. Adelson-Velskii and E.M. Landis [An algorithm for the organization of information, 1962.] We will adopt the more neutral terminology height-balanced tree (abbreviated HB tree or HBT) in discussing the abstract data type and its

implementations, but retain the attributive names when discussing the underlying algorithms. Because an HB tree (HBT) is a binary search tree (BST), there is a well defined binary search algorithm in an HBT that follows descending paths. An important feature of HBTs is that they have height Θ(log n), where n is the number of nodes in the HBT, so an HBT must be fairly bushy. (In stark contrast, a BST can be severely leggy, with height n 1. We can define bushy and leggy asymptotically as having height O(log n) and Ω(n), respectively.) In the following, let H and n denote the height and number of vertices of a tree, respectively. Theorem 1. For any height-balanced tree, H = Θ(log n). Proof. First note that H Ω(log n) for any binary tree, because the number of vertices cannot exceed the number of possible vertex locations: n 1 + 2 +... + 2 H = 2 H+1 1. Taking the logarithm base 2 of each side, we get the result. We concentrate now on second part of the claim, that H O(log n) assuming the binary tree is height balanced. Let n(h) be the minimum number of vertices an HB tree of height H can have. Clearly n(0) = 1, since a tree of height 0 consists exactly of the root vertex. Also n(1) = 2, by looking at all cases. As an inductive step, note that an HB tree of height H with minimal vertex count must have one subtree of height H 1 and another subtree of height H 2 (otherwise a vertex could be removed and n(h) would not be minumal). Thus n(h) = n(h 1) + n(h 2) + 1. In summary, we have the following recurrance relation: n(0) = 1 n(1) = 2 n(h) = n(h 1) + n(h 2) + 1 4 Note the similarity with the Fibannaci recursion given by: f(0) = 0 f(1) = 1 f(h) = f(h 1) + f(h 2)

5 Assertion 1: n(h) > f(h + 2) 1 Proof: Base cases: n(0) = 1 and f(2) 1 = 1 1 = 0, so n(0) > f(2) 1. n(1) = 2 and f(3) 1 = 2 1 = 1, so n(1) > f(2) 1. Inductive step: Applying the definitions of the two recursions and the inductive hypothesis, we have: n(h + 1) = n(h) + n(h 1) + 1 > (f(h + 2) 1) + (f(h + 1) 1) + 1 = f(h + 2) + f(h + 1) 1 = f(h + 3) 1 This proves Assertion 1. Because both sides of the inequality are integers, we can rephrase the previous assertion as: Assertion 2: n(h) >= f(h + 2) Exercise 4-5 in your textbook concludes that f(h + 2) >= φ H / 5, where φ = (1 + 5)/2, the golden ratio. Whence we obtain: Assertion 3: n(h) >= φ H / 5 Taking the logarithm (base φ) of both sides, we see that H O(log n(h)). Because n(h) is the minimum number of vertices an HBT of height H can have, it follows that H O(log n) which completes the proof of Theorem 1. Theorem 2. BST search in an HBT has worst case run time O(log n). Proof. The BST search algorithm consists of a loop that examines nodes in a descending path, starting at the root. Such a path has length no greater than the height of the tree, which is Θ(log n) by Theorem 1.

6 Theorem 3. HBT insert and remove have worst case run time O(log n). Proof. The challenge is to make sure that the HBT properties are maintained as we insert and remove elements. It turns out that the HBT properties do not necessarily hold after an ordinary BST insert or remove operation, but that there are repair algorithms that bring the resulting BST back into compliance with the HBT definition. These algorithms restructure the BST by pruning and re-hanging subtrees and are called rotations. Rotations are constant time algorithms, and they are combined into repair algorithms that iterate along a descending path in the HBT. These repair algorithms thus have runtime bounded by O(log n). It follows that BST insert or remove, followed by HBT repair, has run time O(log n + log n) = O(log n). Note that the constants associated with insert and remove will be significantly larger than those for search, so much so that it is sometimes more efficient to build ordinary BST structures, if the insertions are sufficiently random to ensure a bushy tree, and thus not pay the cost of maintaining the HBT property.