0% found this document useful (0 votes)
4 views5 pages

Group D - 8

The document outlines a practical exercise on constructing an Optimal Binary Search Tree (OBST) using a given sequence of sorted keys and their search probabilities. It details the learning objectives, outcomes, theory behind OBST, algorithms for computing costs and constructing the tree, and provides a sample program code in C++. The exercise aims to enhance understanding of OBST and object-oriented programming features.

Uploaded by

nikita.khawase
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views5 pages

Group D - 8

The document outlines a practical exercise on constructing an Optimal Binary Search Tree (OBST) using a given sequence of sorted keys and their search probabilities. It details the learning objectives, outcomes, theory behind OBST, algorithms for computing costs and constructing the tree, and provides a sample program code in C++. The exercise aims to enhance understanding of OBST and object-oriented programming features.

Uploaded by

nikita.khawase
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Practical No.

8 Group D

Aim: To illustrate the Optimal Binary Search Tree (OBST).

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:

• To understand concept of OBST.


• To understand concept & features like extended binary search tree.

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.

Software required: g++ / gcc compiler- / 64 bit fedora.

Input:
1. No.of Element.
2. key values
3. Key Probability

Outcome

• Learn object oriented Programming features.


• Understand & implement extended binary search tree.

Conclusion : This program gives us the knowledge OBST, Extended binary search tree.

Questions:

1. What is the difference between BST and OBST?


2. What is the concept of OBST?
3. How do you optimize a binary tree?
4. What are the advantages of OBST?

Program Code:

#include <iostream>
#include <vector>

using namespace std;

const int MAX_N = 100; // Maximum number of keys

// Function to create a table for storing


// results of subproblems
struct Result {
int cost;
int root;
};
// Function to find the optimal binary search tree (OBST)
Result optCost(int keys[], float probs[], int i, int j) {
if (j < i) {
return {0, 0}; // Base case: empty subtree
}

// Initialize variables for minimum cost and root


int minCost = INT_MAX;
int minRoot = 0;

// Iterate through possible roots for the subtree


for (int r = i; r <= j; r++) {
int leftCost = optCost(keys, probs, i, r - 1).cost;
int rightCost = optCost(keys, probs, r + 1, j).cost;
int totalCost = leftCost + rightCost + probs[r]; // Sum of costs and probability of root

// Update minimum cost and root if a better solution is found


if (totalCost < minCost) {
minCost = totalCost;
minRoot = r;
}
}

return {minCost, minRoot};


}

// Function to construct the OBST using the calculated root


void constructOBST(int keys[], float probs[], int i, int j, int root) {
if (i > j) {
return; // Base case: empty subtree
}

if (i == j) {
cout << keys[i] << " "; // Print leaf node
return;
}

cout << keys[root] << " "; // Print root node

// Recursive calls to construct left and right subtrees


constructOBST(keys, probs, i, root - 1, optCost(keys, probs, i, root - 1).root);
constructOBST(keys, probs, root + 1, j, optCost(keys, probs, root + 1, j).root);
}

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 << "Enter the probabilities for each key: ";


for (int i = 0; i < n; i++) {
cin >> probs[i];
}

Result result = optCost(keys, probs, 0, n - 1);

cout << "The minimum cost of the OBST is: " << result.cost << endl;
cout << "The root of the OBST is: " << keys[result.root] << endl;

cout << "The inorder traversal of the OBST is: ";


constructOBST(keys, probs, 0, n - 1, result.root);

return 0;
}

Output:

Enter the number of keys: 3


Enter the keys in sorted order: 10 12 20
Enter the probabilities for each key: 34 8 50
The minimum cost of the OBST is: 92
The root of the OBST is: 10
The inorder traversal of the OBST is: 10 12 20

You might also like