COMP 250 Winter 2010 Exercises 6 - trees March 2010

Similar documents
COMP 250 Fall binary trees Oct. 27, 2017

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

March 20/2003 Jayakanth Srinivasan,

MID TERM MEGA FILE SOLVED BY VU HELPER Which one of the following statement is NOT correct.

Largest Online Community of VU Students

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

Formal Languages and Automata Theory, SS Project (due Week 14)

CMSC351 - Fall 2014, Homework #2

MIDTERM EXAMINATION Spring 2010 CS301- Data Structures

1. Convert the following decimal numbers to binary and back again.

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

COMP 250 Fall Homework #4

There are many other applications like constructing the expression tree from the postorder expression. I leave you with an idea as how to do it.

COMP 250 Fall Solution - Homework #4

CE 221 Data Structures and Algorithms

An Introduction to Trees

How much space does this routine use in the worst case for a given n? public static void use_space(int n) { int b; int [] A;

Advanced Java Concepts Unit 5: Trees. Notes and Exercises

CSCE 2014 Final Exam Spring Version A

CS24 Week 8 Lecture 1

University of Palestine. Final Exam 2 nd semester 2014/2015 Total Grade: 50

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

COMP 250 Fall priority queues, heaps 1 Nov. 9, 2018

TREES. Trees - Introduction

Review of the Lectures 21-26, 30-32

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

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

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

Stacks, Queues and Hierarchical Collections

Overview of Presentation. Heapsort. Heap Properties. What is Heap? Building a Heap. Two Basic Procedure on Heap

Hierarchical Structures

A Simple Syntax-Directed Translator

COMP 250 Midterm #2 March 11 th 2013

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

Tree. Virendra Singh Indian Institute of Science Bangalore Lecture 11. Courtesy: Prof. Sartaj Sahni. Sep 3,2010

COS 226 Midterm Exam, Spring 2009

INF2220: algorithms and data structures Series 1

COMP 250 Fall heaps 2 Nov. 3, 2017

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

Basic Data Structures (Version 7) Name:

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Stacks, Queues and Hierarchical Collections. 2501ICT Logan

Priority queues. Priority queues. Priority queue operations

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Chapter 4 Trees. Theorem A graph G has a spanning tree if and only if G is connected.

CS 314 Exam 2 Spring

CS171 Final Practice Exam

Lecture 26. Introduction to Trees. Trees

COMP 250 Winter Homework #4

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Why Do We Need Trees?

Code No: R Set No. 1

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Priority Queues and Binary Heaps

1 Complexity (10 minutes)

ITI Introduction to Computing II

COMP 250 Winter Homework #4. Due date: March 20 th, 23:59pm

Faculty of Science FINAL EXAMINATION COMP-250 A Introduction to Computer Science School of Computer Science, McGill University

Solution to CSE 250 Final Exam

Recitation 9. Prelim Review

In addition to the correct answer, you MUST show all your work in order to receive full credit.

Delhi Noida Bhopal Hyderabad Jaipur Lucknow Indore Pune Bhubaneswar Kolkata Patna Web: Ph:

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Priority Queues and Heaps. Heaps of fun, for everyone!

Summer Final Exam Review Session August 5, 2009

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

Multi-Way Search Tree

Leftist Heaps and Skew Heaps. (Leftist Heaps and Skew Heaps) Data Structures and Programming Spring / 41

ITI Introduction to Computing II

ECE 242 Data Structures and Algorithms. Trees IV. Lecture 21. Prof.

Computer Science Foundation Exam

Cpt S 122 Data Structures. Course Review Midterm Exam # 1

Chapter 5. Binary Trees

Computer Science E-22 Practice Final Exam

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

Priority Queues and Binary Heaps. See Chapter 21 of the text, pages

Priority Queues. 1 Introduction. 2 Naïve Implementations. CSci 335 Software Design and Analysis III Chapter 6 Priority Queues. Prof.

MLR Institute of Technology

Friday, March 30. Last time we were talking about traversal of a rooted ordered tree, having defined preorder traversal. We will continue from there.

CS 231 Data Structures and Algorithms Fall Recursion and Binary Trees Lecture 21 October 24, Prof. Zadia Codabux

CSE 143 Final Part 1, August 18, 2011 Sample Solution

COSC 2007 Data Structures II Final Exam. Part 1: multiple choice (1 mark each, total 30 marks, circle the correct answer)

12 Abstract Data Types

Advanced Tree Data Structures

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

Algorithms, Spring 2014, CSE, OSU Lecture 2: Sorting

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

INF2220: algorithms and data structures Series 1

COMP 250 Fall recurrences 2 Oct. 13, 2017

INSTITUTE OF AERONAUTICAL ENGINEERING

EE 368. Week 6 (Notes)

Binary Trees, Binary Search Trees

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

EXAMINATIONS 2015 COMP103 INTRODUCTION TO DATA STRUCTURES AND ALGORITHMS

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

Computer Science Foundation Exam

3137 Data Structures and Algorithms in C++

403: Algorithms and Data Structures. Heaps. Fall 2016 UAlbany Computer Science. Some slides borrowed by David Luebke

Trees! Ellen Walker! CPSC 201 Data Structures! Hiram College!

Transcription:

Questions 1. Consider the polynomial 5y 2 3y + 2. (a) Write the polynomial as an expression tree that obeys the usual ordering of operations. Hint: to clarify the ordering for yourself, first write the expression with brackets indicating the order of operations. (b) Write the polynomial as postfix expression. 2. Give the order of nodes visited in a pre-, in-, post-, and level-order traversal. 3 6 2 \ 8 4 7 / / 5 9 3. Convert the following infix expressions to postfix expressions. You must assume that expressions with multiple +, - (or *, / ) are evaluated left to right, and that *, / has precedence over *, - i.e. the usual ordering of operations. [The wording in the question was modified March 15.] (a) a*b/(c-d) (b) a/b+(c-d) (c) a/b+c-d 4. Evaluate the following postfix expressions made out of single digit numbers and the usual integer operators. (a) 26+35-/ (c) 234-/5* (d) (b) 234*5*- 6342^*+5-5. Suppose you have a complete binary tree with n = 2 h+1 1 nodes, i.e. all levels of the tree up to h are filled. What is the sum of the pathlengths from the root to all nodes? 1

6. Consider the following binary numbers, which are each between 0 and 1: Define p 1 =.0011, p 2 =.0101, p 3 =.0001, p 4 =.0101, p 5 =.001 F i = i k=1 (a) Write each F i as a binary number. p k, i = 1, 2, 3, 4, 5. (b) Draw a complete binary tree of height h = 4 and label the leaves with labels from {1,...,5 increasing left to right, such that p i is the fraction of leaves with label i. 7. Two binary trees A and B are said to be isomorphic if, by swapping the left and right subtrees of certain (not necessarily all) nodes of A, one can obtain a tree identical to B. For example, the following two binary trees are isomorphic. 5 5 8 4 4 8 2 6 2 6 3 2 2 3 Write a recursive algorithm isisomorphic( n1, n2) for checking if two trees are isomorphic. The arguments should be references to the root nodes of the two trees. To test if two non-null nodes are equal, assume each node has a key and check if the keys of the two nodes are equal. (That way, our definition of equal does not involve other fields defined at each node, namely references to children.) 8. Give a non-recursive version of the binary search tree operations find, findminimum, findmaximum 9. Suppose you have an array with the following characters in it. f b u e l a k d w Show how the array evolves after each of the n = 9 loops of the slow buildheap method. 2

10. Consider the binary tree (with null child references not shown): "easy" "in" "march" \ "can" "be" "quizzes" / "fun" (a) What is the order of nodes visited in a pre-order traversal? (b) What is the order of nodes visited in a post-order traversal? (c) Represent this binary tree using an array (of references to strings), such that the parent/child indices in the array are the same as that used in a heap. (d) Transform this binary tree into a binary search tree of height 2, defined by the natural ordering on strings. 3

Answers 1. (a) + - 2 * * 5 ^ 3 y y 2 (b) 5 y 2 ^ * 3 y * - 2 + 2. PRE ORDER: 3 6 8 5 4 2 7 9 IN ORDER: 5 8 6 4 3 2 9 7 POST ORDER: 5 8 4 6 9 7 2 3 LEVEL ORDER: 3 6 2 8 4 7 5 9 3. The solution is the right column. I have written intermediate solutions which is what you should go through (in your head, or explicitly as I have done). IN-FIX BRACKETED IN-FIX BRACKETED POST-FIX POSTFIX (a) a*b/(c-d) ((a*b)/(c-d)) ((ab*)(cd-)/) ab*cd-/ (b) a/b+(c-d) ((a/b)+(c-d)) ((ab/)(cd-)+) ab/cd-+ (c) a/b+c-d (((a/b)+c)-d) (((ab/)c+)d-) ab/c+d- 4. (a) -4 i.e. 8 / (-2) (b) -58 i.e. 2-60 (c) -10 i.e. 2 * -5 (d) 49 i.e. 6 + (3*(4^2)) -5 You should be solving this problem using a stack. For the last example, you could do it like this: - First we push the first four numbers on the stack.. 6,3,4,2 Then apply ^ to the two two elements (4^2)... 6,3,16 Then apply * to the top two elements (3*16)... 6,48 Then apply + to the top two elements (6+48)... 54 Then push 5 onto the stack... 54,5 Then apply - to the top two elements (54-5) 49 4

5. Counting the root as node 1, we saw from our analysis of heaps that the pathlength from root to node i is log i. So the sum of these is n 2 i log i = i=1 l2 l where the expression on the right is a sum of two terms, namely the pathlength sum up to but not including level h, and the pathlength sum for level h only. Following a similar argument as lecture 24, we consider lx l 1 = dx l dx = d dx x l = d xh+1 (1 dx 1 x ) which you can now solve using basic calculus. Then substitute in x = 2, and plug into the above formula to get: l2 l 1 = (h + 1)2 h + 1 2 h+1 and so l2 l = (h + 1)2 h+1 + 2 2 h+2 = (h 1)2 h+1 + 2 But n = 2 h 1, and so h = log(n + 1), and so the sum of pathlengths from root to all nodes is (log(n + 1) 1)2(n + 1) i.e. roughly n log n. 6. (a) F 1 =.0011, F 2 =.1000, F 3 =.1001, F 4 =.1110, F 5 = 1.0000 (b) Note p 1 = 3 16, p 2 = 5 16, p 3 = 1 16, p 4 = 5 16, p 5 = 2 16 The answer is a binary tree of height 4 and the 16 nodes at level 4 are labelled 1112222234444455. 7. The idea of the algorithm is to check the various possible situations. First check that the roots match, namely both are not null and the keys are identical. Once the two trees pass that test, you compare the left and right children of the two trees. For the two trees to be isomorphic, either (1.) the left subtree of first tree is isomorphic to the left subtree of the second tree AND the right subtree of first tree is isomorphic to the right subtree of the second tree, or (2) the left subtree of first tree is isomorphic to the right subtree of the second tree AND the right subtree of first tree is isomorphic to the left subtree of the second tree. Here is Java code to solve this problem: 5

isisomorphic( n1, n2){ if(n1 == null && n2==null) return true; else if(n1 == null n2==null) return false; if( n1.key!= n2.key ) return false; if( isisomorphic(n1.getrightchild(), n2.getrightchild()) && isisomorphic(n1.getleftchild(), n2.getleftchild())){ return true if( isisomorphic(n1.getrightchild(), n2.getleftchild()) && isisomorphic(n1.getleftchild(), n2.getrightchild())){ return true return false; 8. find(root,key){ cur = root while ((cur!= null) or (cur.key!= key)) if (key < root.key) cur = cur.left else cur = cur.right return cur findminimum(root){ if (root == null) return null else{ while (cur.left!= null){ cur = cur.left return cur 6

findmaximum(root){ cur = null while ((cur!= null) or (cur.right!= null)) cur = cur.right return cur 9. The boundary between the heap and non-heap elements is marked with. 1 2 3 4 5 6 7 8 9 ------------------------------------- f b u e l a k d w f b u e l a k d w (added f) b f u e l a k d w (added b) b f u e l a k d w (added u) b e u f l a k d w (added e) b e u f l a k d w (added l) a e b f l u k d w (added a) a e b f l u k d w (added k) a d b e l u k f w (added d) a d b e l u k f w (added w) Note: A similar example was given at the end of lecture 24 for the build heap fast algorithm. 10. (a) easy, in, can, march, be, fun, quizzes (b) can, in, fun, be, quizzes, march, easy (c) 1 2 3 4 5 6 7 8 9 10 11 12 easy in march * can be quizzes * * * * fun where * is a null reference. Note that the strings are not stored in the array, but rather the array stores references to the strings. (d) fun can march be easy in quizzes 7