Data Structures Mid Term Exam - Problem Solutions, Exams of Data Structures and Algorithms

Solutions to the mid term exam problems for the data structures course, cmsc 420, spring 2000. These problems involve binary trees, threaded binary trees, and ternary trees. The solutions cover topics such as inorder and postorder traversals, finding preorder successors, and constructing and querying ternary trees.

Typology: Exams

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
CMSC 420: Data Structures
Spring 2000
MID TERM EXAM
Problem 1. (6 points) Consider a binary tree
T
whose nodes are labeled with alphabetic charac-
ters. Suppose you are told that
e; a; i; j; d; b;f;g;h;c
is an inorder traversal of this tree, and that
a; j; i; e; f; h; g; b; c; d
isa postordertraversalof thistree. Doesthis informationallow youto uniquely
reconstruct a binary tree? If yes, draw such a tree. If not, explain why either no such tree exists,or
why more than one such tree exists.
Problem 2. (8 points) Let
T
1
be a binary tree, and let
T
2
be a version of
T
1
which is threaded in pre-
order. Assume the existence of fields LTAG and RTAG (with “t” and “l” as possible values denoting
thread/link) respectively which are used to check if the left/right child is a linkor a thread. Suppose
you are given a pointer
P
to a node in tree
T
2
. Is it possible to find the preorder successor of the node
P
is pointing to? If this is possible, give an algorithm in pseudo-code. Otherwise explain why it is
not possible.
Problem 3. (16 points) You are familiar with quadtrees and
2
-d trees to represent
2
-dimensional
data. In this problem, we define a new data structure to represent
2
-d data called a ternary-tree. As in
the case of quadtrees and
2
-d trees, the root of a ternary tree represents a rectangular region
R
.Every
node
N
has fields
N:x; N:y
representing a point in
2
-space, fields
N:lef t; N :rig ht:N :top; N:bot
denoting the extremities of the rectangle (implicitly)represented by node
N
(and in which the point
N:x; N:y
lies), and three link fields N.NW,N.NE,N.SO denotingthe regions obtained by splitting
the rectangle sp ecified by
N:lef t; N :rig ht:N :top; N:bot
into three parts— the northwest, northeast,
and southern regions obtainedby first drawing a horizontal line through
N:x; N:y
to obtain two parts
the southern part (pointed to by N.SO and the northern part. The northern part is then split in two
by drawing a vertical line through
N:x; N:y
to obtain the regions associated with the N.NW,N.NE
children of
N
. Assume all regions are closed on the left and bottom, and open on the top and right.
1. (2points) Suppose youknow allthe fieldsof a node
N
. Howwould youcompute
M:lef t; M :top
from the fields of
N
when
M
is the SO child on
N
.
2. (4 points) Construct the tree obtained when you insert the following points in the order shown:
(50,50), (25,30), (45,75),(20,35). Show all fields of all nodes.
3. (10 points) Write, in pseudocode, an algorithm that takes as input, the root
T
of a ternary tree,
and a point
(
a; b
)
, and returns the “furthest neighbor of
(
a; b
)
in
T
, i.e. it returns that point
in
T
which is furthest away from
(
a; b
)
. When writing this algorithm, you may assume the
existence of functions called
mindist; maxdist
which take three arguments - an
x
coordi-
nate, a
y
-coordinate, and a pointer to a node
N
in the ternary tree.
mindist
(
a; b; N
)
returns
min
f
d
((
a; b
)
;
(
x; y
))
j
(
x:y
)
isin therectangularregion associatedwith
N
g
while
maxdist
(
a; b; N
)
returns
max
f
d
((
a; b
)
;
(
x; y
))
j
(
x:y
)
isin therectangularregion associatedwith
N
g
.
d
(
a; b
)
;
(
x; y
))
here is the distance between
(
x; y
)
and
(
a; b
)
.
1

Partial preview of the text

Download Data Structures Mid Term Exam - Problem Solutions and more Exams Data Structures and Algorithms in PDF only on Docsity!

CMSC 420: Data Structures

Spring 2000

MID TERM EXAM

Problem 1. (6 points) Consider a binary tree T whose nodes are labeled with alphabetic charac-

ters. Suppose you are told that e; a; i; j; d; b; f ; g ; h; c is an inorder traversal of this tree, and that

a; j; i; e; f ; h; g ; b; c; d is a postorder traversal of this tree. Does this information allow you to uniquely

reconstruct a binary tree? If yes, draw such a tree. If not, explain why either no such tree exists, or

why more than one such tree exists.

Problem 2. (8 points) Let T 1 be a binary tree, and let T 2 be a version of T 1 which is threaded in pre-

order. Assume the existence of fields LTAG and RTAG (with “t” and “l” as possible values denoting

thread/link) respectively which are used to check if the left/right child is a link or a thread. Suppose

you are given a pointer P to a node in tree T 2. Is it possible to find the preorder successor of the node

P is pointing to? If this is possible, give an algorithm in pseudo-code. Otherwise explain why it is

not possible.

Problem 3. (16 points) You are familiar with quadtrees and 2 -d trees to represent 2 -dimensional

data. In this problem, we define a new data structure to represent 2 -d data called a ternary-tree. As in

the case of quadtrees and 2 -d trees, the root of a ternary tree represents a rectangular region R. Every

node N has fields N :x; N :y representing a point in 2 -space, fields N :l ef t; N :r ig ht:N :top; N :bot

denoting the extremities of the rectangle (implicitly) represented by node N (and in which the point

N :x; N :y lies), and three link fields N.NW,N.NE,N.SO denoting the regions obtained by splitting

the rectangle specified by N :l ef t; N :r ig ht:N :top; N :bot into three parts — the northwest, northeast,

and southern regions obtained by first drawing a horizontal line through N :x; N :y to obtain two parts

— the southern part (pointed to by N.SO and the northern part. The northern part is then split in two

by drawing a vertical line through N :x; N :y to obtain the regions associated with the N.NW,N.NE

children of N. Assume all regions are closed on the left and bottom, and open on the top and right.

1. (2 points) Suppose you know all the fields of a node N. How would you compute M :l ef t; M :top

from the fields of N when M is the SO child on N.

2. (4 points) Construct the tree obtained when you insert the following points in the order shown:

(50,50), (25,30), (45,75),(20,35). Show all fields of all nodes.

3. (10 points) Write, in pseudo code, an algorithm that takes as input, the root T of a ternary tree,

and a point (a; b), and returns the “furthest neighbor” of (a; b) in T , i.e. it returns that point

in T which is furthest away from (a; b). When writing this algorithm, you may assume the

existence of functions called mindist; maxdist which take three arguments - an x coordi-

nate, a y -coordinate, and a pointer to a node N in the ternary tree. mindist(a; b; N ) returns

minfd((a; b); (x; y ))j(x:y ) is in the rectangular region associated with N g while maxdist(a; b; N )

returns maxfd((a; b); (x; y ))j(x:y ) is in the rectangular region associated with N g. d(a; b); (x; y ))

here is the distance between (x; y ) and (a; b).