Problem Set 4 Solutions

Similar documents
TREES. Trees - Introduction

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

Binary Trees

Binary Trees, Binary Search Trees

(2,4) Trees Goodrich, Tamassia (2,4) Trees 1

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

CSCI2100B Data Structures Trees

March 20/2003 Jayakanth Srinivasan,

Trees. Q: Why study trees? A: Many advance ADTs are implemented using tree-based data structures.

Section 5.5. Left subtree The left subtree of a vertex V on a binary tree is the graph formed by the left child L of V, the descendents

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

CSCI-401 Examlet #5. Name: Class: Date: True/False Indicate whether the sentence or statement is true or false.

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

Chapter 20: Binary Trees

Algorithms. Deleting from Red-Black Trees B-Trees

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

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

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

CS301 - Data Structures Glossary By

Advanced Set Representation Methods

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

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

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

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

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

Lecture 6: Analysis of Algorithms (CS )

Programming II (CS300)

Problem Set 6 Solutions

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

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

) $ f ( n) " %( g( n)

Search Trees - 1 Venkatanatha Sarma Y

Data Structures and Algorithms

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

(b) int count = 0; int i = 1; while (i<m) { for (int j=i; j<n; j++) { count = count + 1; i = i + 1; O(M + N 2 ) (c) int count = 0; int i,j,k; for (i=1

EE 368. Weeks 5 (Notes)

CE 221 Data Structures and Algorithms

logn D. Θ C. Θ n 2 ( ) ( ) f n B. nlogn Ο n2 n 2 D. Ο & % ( C. Θ # ( D. Θ n ( ) Ω f ( n)

COMP Analysis of Algorithms & Data Structures

Bioinformatics Programming. EE, NCKU Tien-Hao Chang (Darby Chang)

Trees : Part 1. Section 4.1. Theory and Terminology. A Tree? A Tree? Theory and Terminology. Theory and Terminology

Successor/Predecessor Rules in Binary Trees

FINALTERM EXAMINATION Fall 2009 CS301- Data Structures Question No: 1 ( Marks: 1 ) - Please choose one The data of the problem is of 2GB and the hard

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

CMSC351 - Fall 2014, Homework #2

n 2 C. Θ n ( ) Ο f ( n) B. n 2 Ω( n logn)

Trees. CSE 373 Data Structures

Assignment 1. Stefano Guerra. October 11, The following observation follows directly from the definition of in order and pre order traversal:

1. [1 pt] What is the solution to the recurrence T(n) = 2T(n-1) + 1, T(1) = 1

Search Trees. Undirected graph Directed graph Tree Binary search tree

( ) D. Θ ( ) ( ) Ο f ( n) ( ) Ω. C. T n C. Θ. B. n logn Ο

Trees. Truong Tuan Anh CSE-HCMUT

Garbage Collection: recycling unused memory

Lecture: Analysis of Algorithms (CS )

Motivation for B-Trees

Advanced Algorithms. Class Notes for Thursday, September 18, 2014 Bernard Moret

INF2220: algorithms and data structures Series 1

Friday Four Square! 4:15PM, Outside Gates

Lecture 11: Multiway and (2,4) Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Binary Search Trees. Analysis of Algorithms

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

D. Θ nlogn ( ) D. Ο. ). Which of the following is not necessarily true? . Which of the following cannot be shown as an improvement? D.

[ DATA STRUCTURES ] Fig. (1) : A Tree

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

Copyright 1998 by Addison-Wesley Publishing Company 147. Chapter 15. Stacks and Queues

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

An AVL tree with N nodes is an excellent data. The Big-Oh analysis shows that most operations finish within O(log N) time

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

INF2220: algorithms and data structures Series 1

Abstract Data Structures IB Computer Science. Content developed by Dartford Grammar School Computer Science Department

Data Structure - Binary Tree 1 -

CS127: B-Trees. B-Trees

Tree Structures. A hierarchical data structure whose point of entry is the root node

CSE 250 Final Exam. Fall 2013 Time: 3 hours. Dec 11, No electronic devices of any kind. You can open your textbook and notes

Course Review. Cpt S 223 Fall 2009

Section Summary. Introduction to Trees Rooted Trees Trees as Models Properties of Trees

Trees. Introduction & Terminology. February 05, 2018 Cinda Heeren / Geoffrey Tien 1

Trees 11/15/16. Chapter 11. Terminology. Terminology. Terminology. Terminology. Terminology

Problem Set 5 Solutions

CSE2331/5331. Topic 7: Balanced search trees. Rotate operation Red-black tree Augmenting data struct. CSE 2331/5331

Associate Professor Dr. Raed Ibraheem Hamed

2-3 and Trees. COL 106 Shweta Agrawal, Amit Kumar, Dr. Ilyas Cicekli

Chapter 4: Trees. 4.2 For node B :

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

Binary Trees. Examples:

Trees. Courtesy to Goodrich, Tamassia and Olga Veksler

Trees. A tree is a directed graph with the property

V Advanced Data Structures

Disk Accesses. CS 361, Lecture 25. B-Tree Properties. Outline

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

Stores a collection of elements each with an associated key value

COMP Analysis of Algorithms & Data Structures

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

( ) ( ) C. " 1 n. ( ) $ f n. ( ) B. " log( n! ) ( ) and that you already know ( ) ( ) " % g( n) ( ) " #&

Data Structures Brett Bernstein

Multi-Way Search Trees

Chapter 2: Basic Data Structures

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

Ensures that no such path is more than twice as long as any other, so that the tree is approximately balanced

Data Structures - Binary Trees and Operations on Binary Trees

Transcription:

Introduction to Algorithms October 24, 2003 Massachusetts Institute of Technology 6.046J/18.410J Professors Shafi Goldwasser and Silvio Micali Handout 18 Problem 4-1. Problem Set 4 Solutions Reconstructing Binary Trees Via Traversals Recall the binary tree data structure; recall three algorithms for traversing the tree: the inorder traversal, the preorder traversal, and the postorder traversal. (a) Suppose you are given the preorder traversal and the inorder traversal of a binary tree. Can you reconstruct the tree? If so, give an algorithm for doing so and prove its correctness. If not, give a counter example. Note that a binary tree is not necessarily a binary search tree. Solution: Yes, we can reconstruct the tree. We shall give a constructive proof that the tree can be built using the preorder and inorder sequences. Clearly the first element in the preorder is the root. We can then search for this element in the inorder traversal. The elements of the tree are now uniquely partitioned into left and right subtrees by this element. (ie., the subsequence of elements preceding the root in the inorder sequence is the inorder sequence for the left sub-tree and similarly the subsequence of elements succeeding the root is the inorder sequence for the right sub-tree) We can then recursively construct the left and right sub-trees from the preorder and inorder subsequences. (b) Suppose you are given the preorder and postorder traversals of a binary tree. Can you reconstruct the tree? If so, give an algorithm for doing so and prove its correctness. If not, give a counter example. Solution: No. If we have left only or right only subtrees in the tree then the preorder and postorder traversals are not necessarily unique. See the following example. Both the trees have preorder ( 1 2 3 ) and postorder ( 3 2 1 ). 1 1 2 2 3 3 Figure 1: Two Trees with the same preorder (1 2 3) and postorder (3 2 1)

2 Handout 18: Problem Set 4 Solutions (c) Suppose you are given the preorder traversal of a binary tree. In addition, you are told that the tree is a binary search tree. Can you reconstruct the tree? If so, give an algorithm for doing so and prove its correctness. If not, give a counter example. Solution: Yes. If it is known that the given tree is a binary search tree, then the inorder sequence of the tree is just the sorted order of the elements in the tree. Thus, from the preorder sequence we can obtain the inorder sequence by sorting the elements. We then can reconstruct the tree as in (a). Problem 4-2. Successors In 2-3 Trees A 2-3 tree is a special B-tree in which each node which is not a leaf has 2 or 3 children, and every path from the root to a leaf is of the same length. Note that the tree consisting of a single node is a 2-3 tree. 2:4 1:2 3:4 5:6 1 2 3 4 5 6 7 Figure 2: Seven keys stored in a 2-3 tree. An ordered set can be stored in a 2-3 tree by storing the elements of the set in the leaves of the tree. We assign the elements to leaves in increasing order from left to right. If is an internal node of the tree, then two pieces of information are stored in it: is the largest element stored in a leaf of the subtree whose root is the leftmost child of ; is the largest element stored in the subtree whose root is the middle child of. The values of and assigned to each internal node enable us to start at the root and search for an element in a manner analogous to binary search. Figure 2 provides an illustration. We wish to add the SUCCESSOR operation to 2-3 trees. Given a leaf in a 2-3 tree, the SUCCESSOR operation returns the leaf that comes after in the sorted order that the 2-3 tree determines. (a) Describe how the SUCCESSOR operation can be implemented on a 2-3 tree in time, where is the number of elements stored in the tree.

Handout 18: Problem Set 4 Solutions 3 Solution: For this part we assume that each node has a pointer to its parent. In order to find the SUCCESSOR of a leaf, we initially go up the tree to the first ancestor of that has a child to the right of the one we came from. That is, we find the first ancestor of, such that the subtree in which is located has a right sibling. Then we find the minimal leaf in the sibling subtree, by following the left children all the way to a leaf. The leaf that we find this way is the successor of. The worst case running time of this operation is, because we go up the tree once and down the tree once. (b) Describe how the 2-3 tree data structure can be modified to support the SUCCESSOR operation in time. Make sure you can still support INSERT, DELETE, and SEARCH in time. Solution: We modify the 2-3 tree data structure by doubly linking the leaves of the tree. The SUCCESSOR operation can now be implemented in time by simply taking the next element on the list. This modification does not affect the asymptotic running time of the SEARCH, INSERT, and DELETE operations. The SEARCH operation is not affected at all. The INSERT operation should now also insert the new leaf into the doubly linked list. One can get a handle on the leaf immediately before or immediately after the new leaf via the new leaf s parent. Splits occur only for internal nodes and do not involve the leaves. The DELETE operation should now also remove the deleted leaf from the doubly linked list. Merging of nodes is done only for internal nodes and does not affect the leaves ordering. The modification of the 2-3 tree data structure introduces only an extra work per operation, and therefore the SEARCH, INSERT, and DELETE operations can still be implemented in time. (c) Describe how the 2-3 tree data structure can be augmented in time using additional space at each node, so that for any!#"$&%', the )(+* successor of an element provided it exists can be found in time. Solution: We use a postorder traversal of the tree: Each leaf is assigned a value 1 and each non-leaf node is assigned the sum of the values of its children. Thus, each node, will be assigned a value -.,/, where -.,/ is the number of leaves in the subtree rooted at,. It takes time to augment the data structure. To find the 0(+* successor of an element in the tree, we need to determine the order statistic of, say it is 1, and then find the element with order statistic 1324. To determine the order statistic of a leaf element, we keep a sum 5 of the values of -.,6 for the nodes, that are to the left of nodes on which we have branched. For

4 Handout 18: Problem Set 4 Solutions example, suppose the root 7 has three children, 8:9;<8>=?8;@. If we branch to the second child (i.e. determine that element is in the subtree rooted at 8A= ), then we add -. 089B to the sum 5. If we branch to the third child, then we add both -. 08C9 and -. 08;=; to the sum 5. If we branch on the first child, we add nothing to 5. When we find the element, if the current sum is 5, then has order statistic 1EDF5G2IHJ2K, where H is the number of siblings to the left of. Then we search for the element with order statistic 1L2M. To do this, we keep a running sum 5 of the values -.,6 for the nodes, that are to the left of nodes on which we have branched. Then suppose we are at node, with three children 8C9;?8;=?8;@. If 5J2N-. 89O"P1Q2R, we branch on 89, since the subtree rooted at 89 would contain the element with order statistic 1Q2R. Otherwise, if 5J2N-G 089BG2N-. 8>=?O"S1Q2R, then we branch on 8>=, etc. We continue until we find the 1T2KU(+* element. Problem 4-3. For an integer, a permutation V of size is a one-to-one function from WXY;Z/![![A[\<^] to WX ;Z/A[![![><_]. Given a sequence of items `9>B a=![![![\ cb, we can apply V to that sequence to obtain the permuted sequence ed:f 90g cd:f =g![![![\ cdcf bg. For example, if Vh EDji, V kz EDl, and Vh imdnz, then V applied to the sequence M,A,F yields F,M,A. Let V be a permutation of size. If o%ph and Vh )qrvh sh/ then the pair tvh U> V Hu is called an inversion of the permutation. A single element out of place may cause many inversions. For example, the permutation Vh DFv/ Vh kzyhdw V 0imDFi/ Vh xxdyxz Vh kv DFZ has 6 inversions: kvu!, kv/<im, 0v/<xX, 0v/;Z, 0i6?Z, and x6;z. (a) What permutation of size has the most inversions? How many does it have? Solution: The b f bc}c90g most inversions is the reverse-sorted permutation, i.e. _<&{ A[![![ Z/!, which has = inversions. We can see that this is maximum, because every pair can only have one inversion and here every pair has an inversion. (b) Give an algorithm that determines the number of inversions in any permutation of size in OG time. Prove that it is correct and analyze its running time. Solution: Insert the elements into a (initially empty) balanced (i.e. AVL, RB) order statistics tree. When you insert the (+* element, you can use the order statistics to determine how many elements already in the tree are greater than. We add this number to our counter that records the total number of inversions. Our algorithm runs in time since the tree is always balanced and insert operations take time in RB tree. To determine the order statistic of the most recently inserted element, we use OS-RANK which determines the rank of an element in time proportional to the height of the tree, namely time.

Š i Handout 18: Problem Set 4 Solutions 5 Problem 4-4. Skip Lists A random skip list is an alternative data structure to a binary search tree. It has the desirable property that an INSERT or a FIND procedure can be conducted in time. A skip list is an ordered linked list of elements: each element is attached to it successor with a pointer. Additionally, some elements have pointers to elements farther down the list, allowing us to skip elements when searching for a particular item in the list. An example of a skip list is shown in Figure 3. level 3 9 level 2 3 9 level 1 2 3 6 9 13 20 Figure 3: A skip list with height 3. To create a skip list, we start with an empty skip list which contains two elements, ~ and { ~, with a pointer from ~ to { ~. Then for each element that we want to insert into the skip list, we let the height of the element be with probability Z *, i.e. we can toss a coin until it comes up heads on trial. If we determine that the height of should be 1, then we insert into the skip list and attach it with pointers to its successors at each of the first 1 levels. (If the height of element is higher than that of any element already in the skip list, then we increase the height of the ~ and { ~ elements so that they have the same height as element.) (a) Show that if we insert elements into an initally empty random skip list, the expected space required is. You can assume that an element uses one unit of space at each level in which it appears and that each pointer uses one unit of space. Solution: We can show that each element uses space in expectation. By linearity of expectation, the total space requirement of a random skip list is. An element uses at most 3 units of space at each level in which it appears. With probability = 9 an element uses i!h units of space. Let ƒ be the indicator variable that indicates whether or not element uses exactly H units of space. Thus, the expected number of units of space per element is ˆ oƒ D oƒ D H Z Dyi As shown in the solutions to problem 3-1, this sum at most 3, so the expected number of space units per element is at most 9. 9 H Z [

ˆ ˆ 6 Handout 18: Problem Set 4 Solutions (b) Show that if we insert elements into an initally empty random skip list, the expected number of levels, i.e. the maximum height of an element is at most E2Œ. Solution: Let Eƒ be an indicator random variable that is 1 if the maximum level is at least Ž and 0 otherwise. We want to compute ƒ+ 9 ˆ oƒ D ƒ+ 9 Eƒ. Note that Eƒ is 1 with probability at most E WXY b =) ]. This is because each element has height at least with probability YZ ƒ. If we union bound over all elements, we get that oƒ is 1 with probability at most Z ƒ. Thus, ƒ+ 9 oƒ " ƒ+ 6 Cb Š ƒ+ 9 2 ƒ+ 6 Cb Note that the second part of the summation, ƒ+ 6 Yb ˆ ƒ+ 9 oƒ DžE2Œ. Z ƒ DyE2 Z ƒ [ b 9 = 2 9 2 9 ƒ 6 Yb =) >š DS2 œh "žz. So (c) Let cƒ denote the element in a skip list that has value at level. A parent of an element cƒ in a skip list is the maximum valued element of value at at most at level e2ÿ. For example, in Figure 3, = is the parent of /9, iu9, and ZY u9. Prove that the expected number of children, i.e. elements on level with the same parent on level e2ÿ, of an element ƒ+ 9 is. Solution: Note that if an element aƒ+ 9 has 8 children, then each child has height exactly. Given that an element has height at least, the probability that it has height at least 2 is Z. Let 3 be the indicator random variable that is a 1 if Hƒ is a child of aƒ 9 and 0 otherwise. Note that aƒ+ 9 has exactly one child with probability :x, exactly two children with probability Z Y, exactly three children with probability im u, etc. So the expected number of children of element mƒ+ 9 is: which is. D H Z B 9 "žz/ (d) Suppose we follow the convention that we never insert any element into the skip list with height more than!. In other words, to assign a height to an element, we flip a coin until it comes up heads on trial. Then we let the height be E ewca.^]. Use this to determine the expected time for an INSERT procedure. Solution: This follows from the fact that in a search path, we branch on an element ƒ and then probe only the children of eƒ (at level ^{F ). Since from part (c), we know that, in expectation, each element has a constant number of children on each level, it

Handout 18: Problem Set 4 Solutions 7 follows by linearity of expectation that the expected time of an INSERT procedure is. Note that in the top level, all of the elements can be viewed as children of the same parent.