Group D - 8
Group D - 8
8 Group D
Problem Statement :
D-18 Given sequence k = k1 <k2 < … <kn of n sorted keys, with a search probability pi for each key ki .
Build the Binary search tree that has the least search cost given the access probability for each key?
Learning Objectives:
Learning Outcome:
• Define class for Extended binary search tree using Object Oriented features.
• Analyze working of functions.
Theory:
An optimal binary search tree is a binary search tree for which the nodes are arranged on
levels such that the tree cost is minimum.
For the purpose of a better presentation of optimal binary search trees, we will consider
“extended binary search trees”, which have the keys stored at their internal nodes. Suppose “n”
keys k1, k2, … k n are stored at the internal nodes of a binary search tree. It is assumed that the
keys are given in sorted order, so that k1< k2 < … < kn.
An extended binary search tree is obtained from the binary search tree by adding successor
nodes to each of its terminal nodes as indicated in the following figure by squares:
In the extended tree:
• The squares represent terminal nodes. These terminal nodes represent unsuccessful
searches of the tree for key values. The searches did not end successfully, that is, because
they represent key values that are not actually stored in the tree;
• The round nodes represent internal nodes; these are the actual keys stored in the tree;
• Assuming that the relative frequency with which each key value is accessed is known,
weights can be assigned to each node of the extended tree (p1 … p6). They represent the
relative frequencies of searches terminating at each node, that is, they mark the successful
searches.
• If the user searches a particular key in the tree, 2 cases can occur:
• 1 – the key is found, so the corresponding weight „p‟ is incremented;
• 2 – the key is not found, so the corresponding „q‟ value is incremented.
GENERALIZATION:
The terminal node in the extended tree that is the left successor of k1 can be interpreted as
representing all key values that are not stored and are less than k1. Similarly, the terminal node in
the extended tree that is the right successor of kn, represents all key values not stored in the tree
that are greater than kn. The terminal node that is successes between ki and ki-1 in an inorder
traversal represent all key values not stored that lie between ki and ki - 1.
Algorithm:
We have the following procedure for determining R(i, j) and C(i, j) with 0 <= i <= j <= n:
PROCEDURE COMPUTE_ROOT(n, p, q; R, C)
begin
for i = 0 to n do C(i, i) ←0
W (i, i) ←q(i) for m = 0 to n do
for i = 0 to (n – m) do j ←i + m
W (i, j) ←W (i, j – 1) + p (j) + q (j)
*find C (i, j) and R (i, j) which minimize the tree cost
end
The following function builds an optimal binary search tree
FUNCTION CONSTRUCT(R, i, j)
begin
*build a new internal node N labeled (i, j) k ←R (i, j)
f i = k then
*build a new leaf node N‟ labeled (i, i) else
*N‟ ←CONSTRUCT(R, i, k)
*N‟ is the left child of node N if k = (j – 1) then
*build a new leaf node N‟‟ labeled (j, j) else
*N‟‟ ←CONSTRUCT(R, k + 1, j)
*N‟‟ is the right child of node N return N
end
COMPLEXITY ANALYSIS:
The algorithm requires O (n2) time and O (n2) storage. Therefore, as „n‟ increases it will run out
of storage even before it runs out of time. The storage needed can be reduced by almost half by
implementing the two-dimensional arrays as one-dimensional arrays.
Input:
1. No.of Element.
2. key values
3. Key Probability
Outcome
Conclusion : This program gives us the knowledge OBST, Extended binary search tree.
Questions:
Program Code:
#include <iostream>
#include <vector>
if (i == j) {
cout << keys[i] << " "; // Print leaf node
return;
}
int main() {
int n;
cout << "Enter the number of keys: ";
cin >> n;
int keys[MAX_N];
float probs[MAX_N];
cout << "Enter the keys in sorted order: ";
for (int i = 0; i < n; i++) {
cin >> keys[i];
}
cout << "The minimum cost of the OBST is: " << result.cost << endl;
cout << "The root of the OBST is: " << keys[result.root] << endl;
return 0;
}
Output: