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:
// The Dictionary Interface.
interface Dictionary<K, V> {
// Reinitialize dictionary
void clear();
// Insert a record
// k: the key for the record being inserted.
// e: the record being inserted.
void insert(K key, V value);
// Remove and return a record.
// k: the key of the record to be removed.
// Return a matching record. If multiple records match "k", remove
// an arbitrary one. Return null if no record with key "k" exists.
V remove(K key);
// Return a record matching "k" (null if none exists).
// If multiple records match, return an arbitrary one.
// k: the key of the record to find
V find(K key);
// Return the number of records in the dictionary.
int size();
};
insert | remove | find | |
---|---|---|---|
Array | |||
BST | |||
Balanced BST |