Problem Set 1 - Basic Data Structures | CMSC 420, Assignments of Data Structures and Algorithms

Material Type: Assignment; Class: Data Structures; Subject: Computer Science; University: University of Maryland; Term: Unknown 1989;

Typology: Assignments

Pre 2010

Uploaded on 02/13/2009

koofers-user-vaz
koofers-user-vaz 🇺🇸

10 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Problem Set #1: Basic Data Structures
Handed out on Feb. 12, due on Feb. 26 at the beginning of class. Remember: write your own
answers and use English or pseudocode when algorithms are requested. Late homeworks will not
be accepted (turn in whatever you have).
Problem 0. (Lewis & Denenburg) A checkerboard is a 2-dimensional array in which only elements
(i, j) for which i+jis even are ever used. Indices run from 0 to n1 in both dimensions. Explain
how to store a checkerboard in contiguous memory in a space-efficient way.
Problem 1. Suppose you are given a (strange) computer that can only perform the following
instructions (in addition to if and while):
S:= create stack makes a new stack S, and
i:= S.pop removes the top item from stack Sand places it in variable i, and
S.push(i)makes item ithe top item in stack S.
Solve the following problems:
a. Show how you can use these operations to implement a queue (operations makequeue,
enqueue, dequeue) a picture might help to explain your answer.
b. What’s the worst case running time of your dequeue implementation?
c. Over a series of nenqueues followed by ndequeues, how many pop operations does your
implementation perform?
1
234
567
8
910
Problem 2. Alevel order traversal of a binary tree visits each node in increasing
order of depth and from left to right within a level. The numbers in the figure
at right give a level-order traversal. Give a (short) algorithm for performing a
level-order traversal.
Problem 3. Describe how to reconstruct a binary tree if you are given both its
preorder and inorder traversals. Is it possible given only a preorder traversal?
(a) (b)
Problem 4. Suppose T1and T2are two ordered,binary trees.
Let r1and r2be the roots of tree T1and T2, respectively, and
denote the left and right children of a node uby LEFT(u) and
RIGHT(u). Two such trees are similar if they have the same
shape in other words, if their natural drawings look the same.
For example, (a) and (b) at right are not similar. Write recursive
function to test whether two such trees T1and T2have the same
shape.

Partial preview of the text

Download Problem Set 1 - Basic Data Structures | CMSC 420 and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Problem Set #1: Basic Data Structures

Handed out on Feb. 12, due on Feb. 26 at the beginning of class. Remember: write your own answers and use English or pseudocode when algorithms are requested. Late homeworks will not be accepted (turn in whatever you have).

Problem 0. (Lewis & Denenburg) A checkerboard is a 2-dimensional array in which only elements (i, j) for which i + j is even are ever used. Indices run from 0 to n − 1 in both dimensions. Explain how to store a checkerboard in contiguous memory in a space-efficient way.

Problem 1. Suppose you are given a (strange) computer that can only perform the following instructions (in addition to if and while):

S := create stack makes a new stack S, and i := S.pop removes the top item from stack S and places it in variable i, and S.push(i) makes item i the top item in stack S.

Solve the following problems:

a. Show how you can use these operations to implement a queue (operations makequeue, enqueue, dequeue) — a picture might help to explain your answer. b. What’s the worst case running time of your dequeue implementation? c. Over a series of n enqueues followed by n dequeues, how many pop operations does your implementation perform? 1

2 3 4 5 6 7 8 9 10

Problem 2. A level order traversal of a binary tree visits each node in increasing order of depth and from left to right within a level. The numbers in the figure at right give a level-order traversal. Give a (short) algorithm for performing a level-order traversal.

Problem 3. Describe how to reconstruct a binary tree if you are given both its preorder and inorder traversals. Is it possible given only a preorder traversal?

(a) (b)

Problem 4. Suppose T 1 and T 2 are two ordered, binary trees. Let r 1 and r 2 be the roots of tree T 1 and T 2 , respectively, and denote the left and right children of a node u by LEFT(u) and RIGHT(u). Two such trees are similar if they have the same shape — in other words, if their natural drawings look the same. For example, (a) and (b) at right are not similar. Write recursive function to test whether two such trees T 1 and T 2 have the same shape.