CS 211 Assignment 4, Fall 2007
Due October 25, 2007 11:59:59 PM
1 BigO Running Time Ordering
Arrange the following functions in increasing order of growth rate (where n is the input size):
n
2
, 2
n
, n log n, 100000000, 5000 log n, n log log n, n!, n
log n
, n
n
.
2 BigO Running Time
Determine the running time for the following pieces of code. No explanation is needed.
(i)
for(int i=0; i<n; i++)
total++;
(ii)
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
total++;
}
for(int k=0; i<100000000000*n; i++)
total++;
(iii)
for(int i=0; i<n; i++)
{
for(int j=0; j<n/2; j++)
{
n
= j%n;
}
n = i%(n+1);
}
(iv)
Assume g(n) is a function that takes O(n)
for(int i=0; i< n; i++)
for(int j=0; j<i; j++)
for(int k=0; k<j; k++)
g(n);
(v)
Assume p>=0
for(int i=0; i < n; i++)
{
while(i>p)
i++;
n++;
}
3 Nearest Neighbor Problem
3.1 Introduction
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Documenthow to use interfaces. To get a better understanding of why tree is a useful data structure,
you will implement an Nearest Neighbor algorithm (locating the nearest point to a given
point) in two ways: a linked list and a 2D tree.
3.2 2D Trees
Twodimensional trees
are dynamic, adaptable data structures which are very similar to
binary trees but divide up a geometric space in a manner convenient for use in range
searching and other problems. The idea is to build binary search trees with points in the
nodes, using the x or y coordinate of the points as keys in an alternating sequence.
The algorithm used for inserting points into 2D trees is the same as for normal binary
search trees with a small modification: Suppose we want to insert a point
p
with coordinates
(
x; y
) into a 2D tree with root node
r
.
1. At the root, if
p
has a smaller
y
coordinate than the point at the root, recrusively call
insert method with
p
on
r
’s left subtree (if one doesn’t exist, set
r
’s left child to be
p
);
else recursively call insert method with
p
on
r
’s right subtree (again, if it doesn’t exist,
set
p
to be
r
’s right child).
2. At each level alternate which coordinate is used for checking which subtree to go to: if in
the previous level of the tree we used the
y
coordinate as the key, then in this level use
the
x
coordinate, and in the next level use the
y
coordinate again, etc.
This is the end of the preview.
Sign up
to
access the rest of the document.
 Fall '07
 FRANCIS
 Tree structure, nearest point

Click to edit the document details