Do not be intimidated by the number of problems in this homework. Most of them
require a very short answer.
- Suppose I want to create a
searching application which supports the
following three operations:
- insert a record into the search structure
- find a record in the search structure
- delete a record from the search structure
Further suppose that:
- the data is inserted almost in ascending order (i.e.,
the data is not completely ordered, but is close to
being ordered).
- the number of insertions and deletions roughly equals the
number of finds.
- ordered traversals and find min/find max operations are never
required.
Which data structure, a linked list, unbalanced tree, balanced tree,
or hash table, would you choose to implement this search
application? Justify your answer.
- Give the preorder, inorder, and postorder expressions corresponding to
the following tree (i.e., show what would be printed out if the node
labels were printed using a preorder, inorder, and postorder traversal):
-
/ \
* e
/ \
* +
/ \ / \
a b c d
- Show the result of inserting 3, 1, 4, 6, 9, 2, 5, 7 into an initially empty
binary search tree.
- Consider the following tree:
---------50----------
/ \
----25---- ----75----
/ \ / \
10 40 60 90
/ / \ / /
2 35 45 55 85
\
57
For each of the following questions, start with the original tree when
performing the deletion (e.g., when deleting 25 in part b, assume that
2 is part of the tree):
- Draw the tree that results from deleting 2
- Draw the tree that results from deleting 25
- Draw the tree that results from deleting 50
- Recall from lecture that the height of a tree is defined as the length of the
maximum path from a node to one of its leaves. The height of an empty
subtree is defined to be -1 and the height of a leaf is 0. Formally
the definition can be written as:
height(empty tree) = -1
height(node) = max(height(left_child), height(right_child)) + 1
- Given this definition, what is the height of the tree in the
previous question (question 4) ?
- Write a recursive function to compute the height of a tree given a
pointer to a node as a parameter.
You can use the following definition for a node:
typedef struct tnode {
int key; // not needed for this problem
struct tnode *left_child;
struct tnode *right_child;
} node;
- In the previous problem does your function do a pre-order, infix,
or post-order
traversal of the tree? Why?
- What is the problem with the following function? How would you fix it?
int fact(n) {
return n * fact(n-1);
}
- Suppose that you have a data set with 1,000,000 randomly distributed
elements. Compute the
average number of searches required to a) find a key that exists, b)
determine that a key does
not exist in the following data structures. Use the big O notation to
do your calculation (e.g, if the average time was O(n2) for
a successful find, you would answer 1012, if it was O(1) you
would write 1. Do not write your
answers using exponents, this number was simply too big to write out).
You may make the following assumptions:
- The data is randomly distributed and specifically is not ordered
- For hashing don't worry about the scheme (separate chaining or
open addressing). Assume the hash table is at least twice as large
as the number of data elements, so that its average
Big-O time is assured.
- For linked lists take advantage of the fact that the list is ordered
and multiply the big O number by an appropriate fraction if
needed.
Data Structure | Key exists | Key does not exist |
Ordered linked list | | |
Unbalanced binary search tree | | |
Hash table | | |