What you can do:
- Start the exercise by pressing the "Random keys"-button or by clearing
the tree and adding keys one by one starting from an empty tree. (Enter each
key and its frequency in the two editable fields left to the buttons, then
press "Add/replace")
- You can add nodes, delete nodes and change the frequency of a node. (The
frequency is proportional to the probability of the node.) To choose the node,
either click on its index-number, key-number, or frequency in the table or
enter its key-value in the editable key field.
- When you click onto an entry in the optimal cost-matrix
, the
frequency-matrix
, or the optimal root index-matrix
, the corresponding
subtree only is shown in the optimal binary search tree, and only the arcs
corresponding to this optimal binary search tree are shown in the optimal root index-matrix.
What you should do:
- Enter nodes with the following keys and frequencies:
| key |
11 |
25 |
30 |
44 |
49 |
60 |
70 |
77 |
90 |
| frequ. |
4 |
18 |
2 |
3 |
5 |
6 |
7 |
9 |
1 |
- Begin with the Frequency matrix
: Find out, how the entries in this
matrix are calculated. Start at the diagonal and work up to the upper right
corner. In every step keep a part of your attention focused on the original
frequency-values. Finally give a formula, how
is calculated and
verify this formula.
- Continue with the Optimal cost matrix
:
- Verify that you get the entry
in this matrix by adding the value
and the Optimal cost-entries belonging to the optimal subtrees
(which are visible in the Optimal Binary Search Tree-window, when you click on
the entry
in one of the matrices). (You can highlight the cost of the
optimal subtrees by clicking onto the entries in the Optimal root-matrix,
where an arc ends.
- Justify, that the procedure described in exercise 2., if applied to
is equivalent to adding the frequency of the root +
times the
frequencies of the nodes in the n-th level, i.e. delivers the optimal cost of
the given tree.
- Now inspect the Optimal Root Index matrix
: a bit closer.
- What does the entry in
mean? What does the entry in
mean?
- Explain, how the values in
were constucted. At which point of the
algorithm these values are stored?
- Explain, how you would construct the tree (i.e. the arcs in
), when
the matrix
is given.
- Hint: Keep in mind, that the root of a binary tree splits it into a left
and a right subtree.
- Note that these arcs give a nice picture of the binary search tree, when
you incline your head 45 degrees to the right.
- Write down the complete algorithm in Java or in Maple.
- And just for fun - working with the tree you entered in exercise 1:
- Change the frequency of key 60 from "6" to "7".
- Change the frequency of key 25 from ''8'' to ''18''
Gaston Gonnet, Institute for Scientific Computing, ETH Zürich, Switzerland
2002-02-24
With assistance from SkillsOnline and Web Pearls