CSE Homework 1

Similar documents
Binary Search Trees. Data in each node. Larger than the data in its left child Smaller than the data in its right child

From Last Time: Remove (Delete) Operation

Full and Complete Binary Trees

Data Structures Fibonacci Heaps, Amortized Analysis

Outline BST Operations Worst case Average case Balancing AVL Red-black B-trees. Binary Search Trees. Lecturer: Georgy Gimel farb

Binary Search Trees (BST)

How To Create A Tree From A Tree In Runtime (For A Tree)

Fundamental Algorithms

Converting a Number from Decimal to Binary

Ordered Lists and Binary Trees

Analysis of Algorithms I: Binary Search Trees

Section IV.1: Recursive Algorithms and Recursion Trees

A binary search tree or BST is a binary tree that is either empty or in which the data element of each node has a key, and:

Binary Search Trees CMPSC 122

Introduction Advantages and Disadvantages Algorithm TIME COMPLEXITY. Splay Tree. Cheruku Ravi Teja. November 14, 2011

Chapter 14 The Binary Search Tree

Symbol Tables. Introduction

A binary heap is a complete binary tree, where each node has a higher priority than its children. This is called heap-order property

Binary Heaps * * * * * * * / / \ / \ / \ / \ / \ * * * * * * * * * * * / / \ / \ / / \ / \ * * * * * * * * * *

Data Structures and Algorithms

Binary Heaps. CSE 373 Data Structures

Previous Lectures. B-Trees. External storage. Two types of memory. B-trees. Main principles

Binary Trees and Huffman Encoding Binary Search Trees

CSE 326: Data Structures B-Trees and B+ Trees

Output: struct treenode{ int data; struct treenode *left, *right; } struct treenode *tree_ptr;

root node level: internal node edge leaf node Data Structures & Algorithms McQuain

B-Trees. Algorithms and data structures for external memory as opposed to the main memory B-Trees. B -trees

Lecture 1: Course overview, circuits, and formulas

Lecture 6: Binary Search Trees CSCI Algorithms I. Andrew Rosenberg

Lecture Notes on Binary Search Trees

Algorithms Chapter 12 Binary Search Trees

Binary Search Trees. A Generic Tree. Binary Trees. Nodes in a binary search tree ( B-S-T) are of the form. P parent. Key. Satellite data L R

Lecture Notes on Binary Search Trees

Persistent Binary Search Trees

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

Binary Search Trees. Ric Glassey

PES Institute of Technology-BSC QUESTION BANK

Optimal Binary Search Trees Meet Object Oriented Programming

Sample Induction Proofs

Algorithms and Data Structures

TREE BASIC TERMINOLOGIES

6 March Array Implementation of Binary Trees

An Immediate Approach to Balancing Nodes of Binary Search Trees

Binary Heap Algorithms

Rotation Operation for Binary Search Trees Idea:

Math 55: Discrete Mathematics

A binary search tree is a binary tree with a special property called the BST-property, which is given as follows:

Why Use Binary Trees?

Heaps & Priority Queues in the C++ STL 2-3 Trees

Cpt S 223. School of EECS, WSU

1) The postfix expression for the infix expression A+B*(C+D)/F+D*E is ABCD+*F/DE*++

CSE373: Data Structures and Algorithms Lecture 3: Math Review; Algorithm Analysis. Linda Shapiro Winter 2015

Big Data and Scripting. Part 4: Memory Hierarchies

Sample Questions Csci 1112 A. Bellaachia

Data Structures, Practice Homework 3, with Solutions (not to be handed in)

GRAPH THEORY LECTURE 4: TREES

Questions 1 through 25 are worth 2 points each. Choose one best answer for each.

Mathematical Induction. Lecture 10-11

UNIVERSITY OF LONDON (University College London) M.Sc. DEGREE 1998 COMPUTER SCIENCE D16: FUNCTIONAL PROGRAMMING. Answer THREE Questions.

Class Overview. CSE 326: Data Structures. Goals. Goals. Data Structures. Goals. Introduction

Data Structures CSC212 (1) Dr Muhammad Hussain Lecture - Binary Search Tree ADT

B+ Tree Properties B+ Tree Searching B+ Tree Insertion B+ Tree Deletion Static Hashing Extendable Hashing Questions in pass papers

ECE 250 Data Structures and Algorithms MIDTERM EXAMINATION /5:15-6:45 REC-200, EVI-350, RCH-106, HH-139

GENERATING THE FIBONACCI CHAIN IN O(log n) SPACE AND O(n) TIME J. Patera

6 Creating the Animation

Introduction to data structures

Exam study sheet for CS2711. List of topics

Analysis of a Search Algorithm

CS 103X: Discrete Structures Homework Assignment 3 Solutions

Analysis of Algorithms I: Optimal Binary Search Trees

Data Structures and Algorithms(5)

Union-Find Problem. Using Arrays And Chains

Binary Search Trees. basic implementations randomized BSTs deletion in BSTs

S. Muthusundari. Research Scholar, Dept of CSE, Sathyabama University Chennai, India Dr. R. M.

In-Memory Database: Query Optimisation. S S Kausik ( ) Aamod Kore ( ) Mehul Goyal ( ) Nisheeth Lahoti ( )

Introduction to Algorithms March 10, 2004 Massachusetts Institute of Technology Professors Erik Demaine and Shafi Goldwasser Quiz 1.

Algorithms and Data Structures Written Exam Proposed SOLUTION

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

A Comparison of Dictionary Implementations

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Mathematics for Algorithm and System Analysis

IMPLEMENTING CLASSIFICATION FOR INDIAN STOCK MARKET USING CART ALGORITHM WITH B+ TREE

8 Divisibility and prime numbers

Solutions for Practice problems on proofs

Introduction to Learning & Decision Trees

Binary search algorithm

Binary Search Trees. Each child can be identied as either a left or right. parent. right. A binary tree can be implemented where each node

The following themes form the major topics of this chapter: The terms and concepts related to trees (Section 5.2).

Question 1a of 14 ( 2 Identifying the roots of a polynomial and their importance )

Learning Outcomes. COMP202 Complexity of Algorithms. Binary Search Trees and Other Search Trees

CSE 326, Data Structures. Sample Final Exam. Problem Max Points Score 1 14 (2x7) 2 18 (3x6) Total 92.

BSc (Hons) Business Information Systems, BSc (Hons) Computer Science with Network Security. & BSc. (Hons.) Software Engineering

Answer: (a) Since we cannot repeat men on the committee, and the order we select them in does not matter, ( )

Math 55: Discrete Mathematics

Data Structure with C

ER E P M A S S I CONSTRUCTING A BINARY TREE EFFICIENTLYFROM ITS TRAVERSALS DEPARTMENT OF COMPUTER SCIENCE UNIVERSITY OF TAMPERE REPORT A

- Easy to insert & delete in O(1) time - Don t need to estimate total memory needed. - Hard to search in less than O(n) time

Efficient representation of integer sets

Data Structure [Question Bank]

Data Structures. Level 6 C Module Descriptor

Transcription:

CSE 00 - Homework.8 (a) Give a precise expression for the minimum number of nodes in an AVL tree of height h. (b) What is the minimum number of nodes in an AVL tree of height? (a) Let S(h) be the minimum number of nodes in an AVL tree T of height h. The subtrees of an AVL tree with mimimum number of nodes must also have minimum number of nodes. Also, at least one of the left and right subtrees of T is an AVL tree of height h. Since the height of left and right subtrees can differ by at most, the other subtree must have height h. Then, we have the following recurrence relation: S(h) = S(h ) + S(h ) +. () We also know the base cases: S(0) = and S() =. One method to solve a recurrence relation is to guess the solution and prove it by induction. Observe that the recurrence relation in () is very similar to the recurrence relation of Fibonacci numbers. When we look at the first a few numbers of the sequence S(h), it is not difficult to guess S(h) = F(h + ). Now, let s prove that S(h) = F(h + ) by induction. Base cases: S(0) =, F() =. So, S(0) = F(). S() =, F() =. So, S() = F(). Induction hyprothesis: Assume that the hypothesis S(h) = F(h + ) is true for h =,,k. Inductive step: Prove that it is also true for h = k +. S(k + ) = S(k) + S(k ) + = F(k + ) + F(k + ) + = F(k + ). We replace S(k) and S(k ) with their equivalence according to the hypothesis. S(k + ) = F(k + ). Hypothesis is also true for h = k +. Thus, it is true for all h. Then, we get (b) S() = F(8).. Show the result of inserting,,,,,,, 7. See Figure..0 The keys,,, k are inserted in order into an initially empty AVL tree. Prove that the resulting tree is perfectly balanced. We will prove the following more general statement by induction on k: The result of inserting any increasing sequence of k numbers into an initially empty AVL tree results in a perfectly balanced tree of height k.

Base case: k =. Tree has only one node. This is clearly perfectly balanced. Induction hypothesis: Assume hypothesis is true for k =,,...,h. Inductive step: Prove that it is true for k = h +, i.e. for the sequence,,, h+. After the first h insertions, by the induction hypothesis, the tree is perfectly balanced, with height h. h is at the root; the left subtree is a perfectly balanced tree of height h, and the right subtree is a perfectly balanced tree containing the numbers h + through h, also of height h. Each of the next h insertions ( h through h + h ) are inserted into the right subtree, and the entire sequence of numbers in the right subtree (now h + through h + h ) were inserted in order and are a sequence of h nodes. By the induction hypothesis, they form a perfectly balanced tree of height h. See Figure. The next insertion, of the number h + h, imbalances the tree at the root. The subsequent RR rotation forms a tree with root h at the root, and a perfectly balanced left subtree of height h. The right subtree consists of a perfectly balanced tree (of height h ), with the new node (containing h + h as the right child of its biggest element. Thus, the right subtree is as if the numbers h +, h + h had been inserted in order. By the induction hypothesis, subsequently inserting the numbers h + h + through h+ nodes form a perfectly balanced subtree of height h. Since the left and right subtrees are perfectly balanced (height h ), the whole tree is perfectly balanced.. Write a method to generate a perfectly balanced binary search tree of height h with keys through h+. What is the running time of your method? One way to implement is the following: public BSTNode genperfecttree (int h) { return genpefecttree (, Math.pow(, h + ) - ); } private BSTNode genperfecttree (int low, int high) { } if (low==high) return new BSTNode(low, null, null); int root = (low + high) / ; BSTNode left = genperfecttree (low, root - ); BSTNode right = genperfecttree (root +, high); return new BSTNode(root, left, right); The running time of this is O( h ) = O(n), where n is the number of nodes in the generated tree. Every node is visited just one time when it is generated..7 Teo trees T and T are isomorphic if T can be transformed into T by swapping left and right children of (some of) nodes in T. (a) Give a polynomial time algorithm to decide if two trees are isomorphic. (b) What is the running time of your program? One way to implement is the following:

public boolean areisomorphic (BinaryTreeNode root, BinaryTreeNode root) { /* If both are null trees, they are isomorphic. */ if (root == null && root == null) return true; /* If one is null and the other not, they are not isomorphic. */ if (root == null root == null) return false; /* If the elements at the roots are not the same, they are not isomorphic. */ if (root.element.compareto(root.element)!= 0) return false; /* Now, to be isomorphic, root.left must be isomorphic to root.left AND root.right must be iosmorphic to root.right, OR root.right must be isomorphic to root.left AND root.left must be iosmorphic to root.right. */ } return (areisomorphic(root.left, root.left) && areisomorphic(root.right, root.right)) (areisomorphic(root.right, root.left) && areisomorphic(root.left, root.right)); Worst case time complexity, as a function of the height of the smaller of the two trees, can be written by the following recurrence relation. T( ) = c // height of - means an empty tree. T(h) = T(h ) + c// up to four recursive calls for trees smaller in height by. We can solve this recurrence relation by back substitution. T(h) = T(h ) + c = ( T(h ) + c) + c = ( ( T(h ) + c) + c) + c = T(h ) + c ( + + ) i = i T(h i) + c k=0 k = i T(h i) + c ( i )/( ) = i T(h i) + (c/) ( i )

Stop when h i = (the base case), i.e. i = h +. T(h) = h+ T( ) + (c/) ( h+ ) = c h+ + (c/) ( h+ ) = (c + c/) (h+) (c/) Thus, the running time T(h) is exponential in h, i.e. O( h ). However, if h = O(logn), then it is O(n ). Extra Problem Prove that insertion into a binary search tree that is complete while maintaining completeness can not be done in O(logn) time. We will show that for any tree height, we can create a perfectly balanced binary search tree that can not be inserted into in O(logn) time, while leaving the tree perfectly balanced. For this problem, our perfectly balanced binary tree is a tree completely filled in, except possibly for the bottom level, which is filled from left to right. First, we show that if a sequence of consecutive integers of length k starting at x is in a perfectly balanced tree, then the leaves of the tree are numbered x,x +,x +,,x + k. So, for example, if k =, and x =, the leaves are numbered,,, and 7. See figure. We will prove this by induction on k. Base case: k = Since there is just one integer x in the sequence, it must be a leaf. Induction hypothesis: Assume it is true for k =,,h. Induction step: Prove that it is true for k = h +. The numbers stored in the tree are x,x +,,x + h+. The root stores the integer ((x + ( h + ) ) + x)/ = x + h. The left subtree stores the numbers x,x +,,x + h. This tree has (x + h ) x + = h nodes, so the induction hypothesis applies, and the leaves of this tree are x,x +,,x + h. The right subtree stores the numbers x + h,x + h +,,x + ( h + ). This tree has (x + ( h + ) ) (x + h ) + = h nodes, so the induction hypothesis applies, and the leaves of this tree are x + h,x + h +,,x + ( h + ). Thus, the sequence of the leaves in the entire tree is x,x+,,x+ h,x+ h,x+ h +,,x+ h+ and we have proven the statement for k = h +. Now consider the perfectly balanced tree containing the numbers,,..., h. Its leaves will be numbered,,,, h. Now remove the bottom right node from this tree (the one numbered h ), and insert into the tree the number 0. The numbers stored will be 0,,,, h. Since there are still h nodes, the above proof applies, and the leaf nodes will be numbered 0,,,, h. Since the leaf nodes are the only ones that have null pointers in a perfectly balanced tree, and since all of the leaf nodes are changing during the process of inserting 0, whatever algorithm does the insertion will have to visit all of the leaf nodes. Since there are O(n) leaf nodes, the algorithm must have worst case O(n) time complexity.

Insert Insert Insert Insert Insert Insert Insert Insert 7 7 Figure : Problem.

h- h- h- h- h- after inserting,,..., h -. h- h- after inserting,,..., h -,..., h + h- -. h h h- h- h- h- h- h- h- h- after inserting,,..., h -,..., h + h-. after inserting,,..., h -,..., h + h-,... h+ -. Figure : Problem.0