Ans the following binary tree is the solution for the given records
N |
D |
H |
K |
S |
T |
U |
W |
P |
R |
M |
I |
E |
G |
A |
B |
C |
#include<iostream>
using namespace std;
class BTNode
{
int *keys; // An array of keys
int t; // Minimum degree (defines the range for number of keys)
BTNode **C; // An array of child pointers
int n; // Current number of keys
bool leaf; // Is true when node is leaf. Otherwise false
public:
BTNode(int _t, bool _leaf);
void insNonFull(int k);
void split(int i, BTNode *y);
void trav();
BTNode *srch(int k);
friend class BTree;
};
class BTree
{
BTNode *root; // Pointer to root node
int t; // Minimum degree
public:
BTree(int _t)
{
root = NULL; t = _t;
}
void trav()
{
if (root != NULL) root->trav();
}
BTNode* srch(int k)
{
return (root == NULL)? NULL : root->srch(k);
}
void ins(int k);
};
BTNode::BTNode(int t1, bool leaf1)
{
t = t1;
leaf = leaf1;
keys = new int[2*t-1];
C = new BTNode *[2*t];
n = 0;
}
void BTNode::trav()
{
int i;
for (i = 0; i < n; i++)
{
if (leaf == false)
C[i]->trav();
cout << " " << keys[i];
}
if (leaf == false)
C[i]->trav();
}
BTNode *BTNode::srch(int k)
{
int i = 0;
while (i < n && k > keys[i])
i++;
if (keys[i] == k)
return this;
if (leaf == true)
return NULL;
return C[i]->srch(k);
}
void BTree::ins(int k)
{
if (root == NULL)
{
root = new BTNode(t, true);
root->keys[0] = k; // Ins key
root->n = 1; // Update number of keys in root
}
else // If tree is not empty
{
if (root->n == 2*t-1)
{
BTNode *s = new BTNode(t, false);
s->C[0] = root;
s->split(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insNonFull(k);
// Change root
root = s;
}
else
root->insNonFull(k);
}
}
void BTNode::insNonFull(int k)
{
int i = n-1;
if (leaf == true
)
{
while (i >= 0 && keys[i] > k)
{
keys[i+1] = keys[i];
i--;
}
keys[i+1] = k;
n = n+1;
}
else
{
while (i >= 0 && keys[i] > k)
i--;
if (C[i+1]->n == 2*t-1)
{
// If the child is full, then split it
split(i+1, C[i+1]);
if (keys[i+1] < k)
i++;
}
C[i+1]->insNonFull(k);
}
}
void BTNode::split(int i, BTNode *y)
{
BTNode *z = new BTNode(y->t, y->leaf);
z->n = t - 1;
for (int j = 0; j < t-1; j++)
z->keys[j] = y->keys[j+t];
if (y->leaf == false)
{
for (int j = 0; j < t; j++)
z->C[j] = y->C[j+t];
}
y->n = t - 1;
for (int j = n; j >= i+1; j--)
C[j+1] = C[j];
C[i+1] = z;
for (int j = n-1; j >= i; j--)
keys[j+1] = keys[j];
keys[i] = y->keys[t-1];
n = n + 1;
}
int main()
{
BTree t(4); // A B-Tree with minium degree 4
t.ins(10);
t.ins(20);
t.ins(5);
t.ins(6);
t.ins(12);
t.ins(30);
t.ins(7);
t.ins(17);
cout << "Traversal of the constructed tree is ";
t.trav();
int k = 6;
(t.srch(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
k = 15;
(t.srch(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
return 0;
}
t.ins(7);
t.ins(17);
cout << "Traversal of the constucted tree is ";
t.trav();
int k = 6;
(t.srch(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
k = 15;
(t.srch(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
return 0;
}
write the c++ code. The records are insSerted Hl 6. A B tree: You are given...
[Index structure: B+ tree and B tree] (b). B+ tree index structure is said to be an improvement of B tree index structure. The most important distinction between them is that data record pointers exist in both internal and leaf nodes (i.e., blocks) for a B tree whereas only in the leaf nodes for a B+ tree. Explain why this distinction would make B+ tree a more efficient structure (in terms of index search speed) overall than a B tree...
Multi-Level Indexing. Construct a B+-tree for a data file that contains 10 records with the following key values, respectively: 23, 26,43,77,4,92 The index fan-out p 3, that is, each internal tree node of the B+-tree contains at most p-1 = 2 keys and p = 3 pointers; the underlying data file has a blocking factor pI-2, that is, each leaf of the B+-tree contains at most pI-2 data records. Assume that the tree is initially empty and records are added...
Question 3. (25 points) Consider the B+ tree index shown below. Each intermediate node can hold up to five pointers and four key values. Each leaf can hold up to four records, and leaf nodes are doubly linked as usual, although these links are not shown in the figure. If you can borrow from or merge with both siblings, choose the right sibling. Answer the following questions 3080 I1 35425065 9098 12 68 69 70 79 98 99 1001105 30...
2. A regular binary tree is a binary tree whose internal nodes all have two subtrees (left and right). In other words, all their nodes have either zero subtrees (in which case they are leaves) or two subtrees (in which case they are internal nodes). Suppose that you have a boolean function that tells you, for each node of the tree, whether it is a leaf or not (call it: leaf(n), for node n). a) Write a recursive function that...
Show the results of deleting the keys C, then P, then V, from the following tree I ALSO ATTACHED AN IMAGE [E L P T X] [A C] [J K] [N O] [Q R S] [U V] [Y Z] f) Е. L,P.Tх B deleted: case 3a J K A C N O QRS UV Y Z f) Е. L,P.Tх B deleted: case 3a J K A C N O QRS UV Y Z
(b) You are given the AVL Tree in the figure below. Assume that the nodes are sorted in alphabetical order. E J B D K A F L H Draw the resulting BST after node E is removed. To construct the new BST replace node E with an appropriate node from the left subtree of E. Do not rebalance the resulting tree. Label each node in the resulting tree with its balance factor. (e) Now rebalance the tree from the...
In this assignment, you will develop a C program to construct a red and black tree. For a given input sequence the tree is unique by using RB-INSERT on one number at a time. Below is an example: The input is a sequence of numbers separated by comma, e.g. 1,8,11,2, … You can enter the numbers using an input file and output the tree, or through a command line with one number at a time (with “X” to stop entering...
Summary You will write an application to build a tree structure called Trie for a dictionary of English words, and use the Trie to generate completion lists for string searches. Trie Structure A Trie is a general tree, in that each node can have any number of children. It is used to store a dictionary (list) of words that can be searched on, in a manner that allows for efficient generation of completion lists. The word list is originally stored...
C++ Programming help to write a code with the components: Randomly generate a 5x5 grid of random letters. Create an interface to allow for the entry of a candidate word. If the word if found, the user interface will indicate this, if not, it will also inform the user. Allow for multiple words on the same grid to be guessed. Also allow for the ability to generate a new grid. Letters are considered to be touching if they are horizontally,...
This is a c++ question note: not using namespace std; at the beginning of the program Writing Data to a File This program will write a series of letters, starting with 'A', to an external file (letters.txt). The user will decide how many letters in total get saved to the file. ** IMPORTANT: The test cases will evaluate your code against .txt files that I uploaded. You do NOT have to upload your own txt files. Input: Including 'A', how many...