32
/ \
16 45
/ \
5 38
Use the Binary Search Tree insertion algorithm to insert the keys
1, 2, 3, 4, 5, 6, 7 into a initially empty BST (in that order!).
What is the height of the resulting tree?
What is the (exact) average number of nodes that must be
visited to find a value that is in this tree assuming that all
values 1-7 are equally likely to be searched for? (Remember: An
average case analysis is performed by adding up the costs for
each possible case, then dividing by the number of cases.)
In general, if I add the numbers 1 through n to a binary tree
(in order), what is the average number of nodes that will be
accessed during the find operation in the resulting tree?
Draw the binary search tree with the smallest possible height
containing the keys 1, 2, 3, 4, 5, 6, 7.
remove(4)
is called.Here is an interface describing a simple Dictionary ADT:
public interface Dictionary<K, V> {
/**
* Insert an entry into the dictionary. Return the previous value, or null if
* there was no previous value associated with this key
*/
V put(K key, V value);
/**
* Return the value associated with the provided key, or null if this key is not
* in the dictionary.
*/
V get(K key);
/**
* Remove the indicated entry from the dictionary. Returns true if something was
* actually removed.
*/
boolean remove(K key);
/**
* Return true if the provided key is in the dictionary.
*/
boolean contains(K key);
/**
* Return the number of entries in the dictionary.
*/
int size();
}
insert | remove | find | |
---|---|---|---|
Array | |||
BST | |||
Balanced BST |