|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object | +--com.mhhe.clrs2e.OptimalBinarySearchTree
Implements the dynamic-programming algorithm to determine the structure of an optimal binary search tree, as described in Section 15.5 of Introduction to Algorithms, Second edition. No method to print an optimal solution is provided, since that would give away the solution to Exercise 15.5-1.
Field Summary | |
private int |
n
How many keys are in the binary search tree. |
private int[][] |
root
The root of an optimal binary search tree for subproblem. |
Constructor Summary | |
OptimalBinarySearchTree(double[] p,
double[] q)
Determines the structure of an optimal binary search tree, given an array of probabilities of successful searches for keys and an array of probabilities of unsuccessful searches. |
Method Summary | |
int |
getNumberOfKeys()
Returns the value of n. |
int[][] |
getRootTable()
Returns the table of roots of subtrees. |
void |
optimalBST(double[] p,
double[] q,
int n)
Determines the structure of an optimal binary search tree, given an array of probabilities of successful searches for keys and an array of probabilities of unsuccessful searches. |
Methods inherited from class java.lang.Object |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
private int n
private int[][] root
root[i][j]
is the root of g an optimal binary
search tree containing the keys ki,
..., kj, for 1 ≤ i
≤ j ≤ n. Other positions of
root
are unused.
Constructor Detail |
public OptimalBinarySearchTree(double[] p, double[] q)
root
and stores
the subproblem roots in it. Keys are numbered 1 to n.
p
- The array of probabilities of successful searches.
p[i]
is the probability that a search is for
ki. Entry p[0]
is
unused.q
- The array of probabilities of unsuccessful searches.
q[i]
is the probability that a search falls
between keys ki and
ki+1. q[0]
is the
probability that a search falls to the left of
k1, and q[n]
is the probability
that a search falls to the right of
kn.Method Detail |
public void optimalBST(double[] p, double[] q, int n)
root
is assumed to be already
allocated. Keys are numbered 1 to n. Implements the
Optimal-BST procedure on page 361.
p
- The array of probabilities of successful searches.
p[i]
is the probability that a search is for
ki. Entry p[0]
is
unused.q
- The array of probabilities of unsuccessful searches.
q[i]
is the probability that a search falls
between keys ki and
ki+1. q[0]
is the
probability that a search falls to the left of
k1, and q[n]
is the probability
that a search falls to the right of
kn.public int getNumberOfKeys()
public int[][] getRootTable()
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |