Unit 3 - Programs
Unit 3 - Programs
#include <iostream>
#include <queue>
using namespace std;
struct Node {
int key1, key2;
Node *left, *middle, *right;
Node(int key) {
key1 = key;
key2 = -1;
left = middle = right = nullptr;
}
Node(int key1, int key2, Node* left, Node* middle, Node* right) {
this->key1 = key1;
this->key2 = key2;
this->left = left;
this->middle = middle;
this->right = right;
}
};
class TwoThreeTree {
public:
Node* root;
if (node->key2 == -1) {
if (key < node->key1) {
if (!node->left) node->left = new Node(key);
else node->left = insert(node->left, key);
} else {
if (!node->right) node->right = new Node(key);
else node->right = insert(node->right, key);
}
} else {
if (key < node->key1) {
node->left = insert(node->left, key);
} else if (key > node->key2) {
node->right = insert(node->right, key);
} else {
node->middle = insert(node->middle, key);
}
}
split(node);
return node;
}
node->middle = nullptr;
node->right = nullptr;
node->key2 = -1;
}
return node;
}
queue<Node*> q;
q.push(node);
while (!q.empty()) {
int levelSize = q.size(); // Number of nodes in the current level
while (levelSize--) {
Node* current = q.front();
q.pop();
int main() {
TwoThreeTree tree;
int keys[] = {9, 5, 8, 3, 2, 4, 7}; // Updated keys
return 0;
}
// A BTree node
class BTreeNode
{
int *keys; // An array of keys
int t; // Minimum degree (defines the range for number of keys)
BTreeNode **C; // An array of child pointers
int n; // Current number of keys
bool leaf; // Is true when node is leaf. Otherwise false
public:
BTreeNode(int _t, bool _leaf); // Constructor
// Make BTree friend of this so that we can access private members of this
// class in BTree functions
friend class BTree;
};
// A BTree
class BTree
{
BTreeNode *root; // Pointer to root node
int t; // Minimum degree
public:
// Constructor (Initializes tree as empty)
BTree(int _t)
{ root = NULL; t = _t; }
// Split the old root and move 1 key to the new root
s->splitChild(0, root);
// Change root
root = s;
}
else // If root is not full, call insertNonFull for root
root->insertNonFull(k);
}
}
int k = 6;
(t.search(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
k = 15;
(t.search(k) != NULL)? cout << "\nPresent" : cout << "\nNot Present";
return 0;
}