• Wrong document? Swap it for free
  • Written by students who passed
  • Immediately available after payment
  • Read online or as PDF
Sell
Where do you study
Your language
Document preview thumbnail
Preview 3 out of 26 pages
Exam (elaborations)

Algorithms Test 2 Questions With Answers

Document preview thumbnail
Preview 3 out of 26 pages

Algorithms Test 2 Questions With Answers

Content preview

Algorithms Test 2 Questions With Answers
Question 1.
Determine an LCS of <1,0,0,1,0,1,0,1> and <0,1,0,1,1,0,1,1,0>
Correct Answer: The LCS is <1, 0, 0, 1, 1, 0> , Or It can be <1,0,1,0,1,0>

,Question 2.
Write pseudocode for the proce-
dure CONSTRUCT-OPTIMAL-BST(root) which, given the table root, outputs the structure of
an optimal binary search tree. For the example in Figure 15.8, your procedure should
print out the structure
k2 is the root
k1 is the left child of k2
d0 is the left child of k1
d1 is the right child of k1
k5 is the right child of k2
k4 is the left child of k5
k3 is the left child of k4
d2 is the left child of k3
d3 is the right child of k3 * d4 is the right child of k4
d5 is the right child of k5
Correct Answer: #include <iostream>
using namespace std;
const int MaxVal = 9999;
const int n = 5;
// Search probability root and virtual keys
double p[n + 1] = {-1,0.15,0.1,0.05,0.1,0.2}; double q[n + 1] =
{0.05,0.1,0.05,0.05,0.05,0.1};
int root[n + 1][n + 1];//Record root node
double w[n + 2][n + 2];//
Subtree probability sum
double e[n + 2][n + 2];//Subtree Expected Cost
void optimalBST(double *p,double *q,int n)
{
//Initializes only children with virtual keys
for (int i = 1;i <= n + 1;++i)
{
w[i][i - 1] = q[i - 1];
e[i][i - 1] = q[i - 1];
}
//From bottom to top, step by step from left to right for (int len = 1;len <=
n;++len)
{
for (int i = 1;i <= n - len + 1;++i)
{
int j = i + len - 1;
e[i][j] = MaxVal;
w[i][j] = w[i][j - 1] + p[j] + q[j];

,//Find the root of the subtree with the lowest cost for (int k = i;k <= j;++k)
{
double temp = e[i][k - 1] + e[k + 1][j] + w[i][j]; if (temp < e[i][j])
{
e[i][j] = temp;
root[i][j] = k;
}
}
}
}
}
//Output optimal binary search tree root of all subtrees
void printRoot()
{
cout << "The root of each subtree" << endl;
for (int i = 1;i <= n;++i)
{
for (int j = 1;j <= n;++j)
{
cout << root[i][j] << " ";
}
cout << endl;
}
cout << endl;
}
//Print the structure of the optimal binary search tree
//Prints the [i,j] subtree, which is the left subtree and right subtree of root r
void printOptimalBST(int i,int j,int r)
{
int rootChild = root[i][j];//Subtree Root Node
if (rootChild == root[1][n])
{
//Output the root of the entire tree
cout << "k" << rootChild << "Root" << endl; printOptimalBST(i,rootChild -
1,rootChild); printOptimalBST(rootChild + 1,j,rootChild); return;
}
if (j < i - 1)
return;
else if (j == i - 1)//Encountered a virtual key
{
if (j < r)
cout << "d" << j << "Yes" << "k" << r << "Left Child" << endl;

Document information

Uploaded on
December 3, 2026
Number of pages
26
Written in
2026/2027
Type
Exam (elaborations)
Contains
Questions & answers
$14.99

Wrong document? Swap it for free Within 14 days of purchase and before downloading, you can choose a different document. You can simply spend the amount again.
Written by students who passed
Immediately available after payment
Read online or as PDF

Seller avatar
Reputation scores are based on the amount of documents a seller has sold for a fee and the reviews they have received for those documents. There are three levels: Bronze, Silver and Gold. The better the reputation, the more your can rely on the quality of the sellers work.
Favorgrades
3.8
(200)
Sold
865
Followers
376
Items
64193
Last sold
1 day ago



Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their tests and reviewed by others who've used these notes.

Didn't get what you expected? Choose another document

No worries! You can instantly pick a different document that better fits what you're looking for.

Pay as you like, start learning right away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and aced it. It really can be that simple.”

Alisha Student

Working on your references?

Create accurate citations in APA, MLA and Harvard with our free citation generator.

Working on your references?

Frequently asked questions