file indexer using b tree under windows c++ implement
- User
file indexer using b tree under windows c++ implement
- Assistant
file indexer using b tree under windows c++ implement
ation:
struct IndexEntry { std::wstring file_name; std::wstring file_location; unsigned long file_size; // other necessary info };
class BTree { public: // Constructor BTree(unsigned int order);
// Destructor ~BTree();
// Search the tree for a specific key IndexEntry* Search(std::wstring key);
// Insert a new index entry into the tree void Insert(IndexEntry entry);
// Delete an index entry from the tree void Delete(std::wstring key);
// Traverse and print all the nodes and entries in the tree void Print();
private: // Define node structure which contains keys, children and number of keys struct BTreeNode { std::vectorstd::wstring keys; std::vector<BTreeNode*> children; unsigned int num_keys; bool leaf; };
// Helper function to search a node for a specific key BTreeNode* SearchNode(std::wstring key);
// Helper function to split a node when it is full void SplitNode(BTreeNode* node, BTreeNode* child, unsigned int index);
// Helper function to insert a key into a node void InsertToNode(BTreeNode* node, std::wstring key, BTreeNode* right_child);
// Helper function to delete a key from a node void DeleteFromNode(BTreeNode* node, std::wstring key);
// Helper function to merge two nodes when one has too few keys void MergeNodes(BTreeNode* parent, unsigned int index);
// Helper function to print all nodes and entries in the tree void PrintNodes(BTreeNode* node, std::wstring prefix, bool is_last);
// Root node of the tree BTreeNode* root_;
// Minimum number of keys a node can have unsigned int min_keys_;
// Maximum number of keys a node can have unsigned int max_keys_; };
a. Constructor: BTree::BTree(unsigned int order) { root_ = nullptr; min_keys_ = order / 2; max_keys_ = order - 1; }
b. Destructor: BTree::~BTree() { // TODO: To be implemented }
c. Search: IndexEntry* BTree::Search(std::wstring key) { // Search the tree recursively starting from the root node BTreeNode* node = SearchNode(key); if (!node) { return nullptr; }
// Find the index of the key in the node's keys vector
unsigned int index = 0;
while (index < node->num_keys && node->keys[index] < key) {
++index;
}
// If the key is found in the node, return the associated index entry
if (index < node->num_keys && node->keys[index] == key) {
return &(index_entries_[node->children[index]]);
}
// If the key is not found in the node, search the appropriate child node
if (node->leaf) {
return nullptr;
}
return node->children[index]->Search(key);
}
d. Insert: void BTree::Insert(IndexEntry entry) { // If the tree is empty, create a new root node with the entry as the only key if (!root_) { root_ = new BTreeNode(); root_->keys.push_back(entry.file_name); root_->children.push_back(nullptr); root_->children.push_back(nullptr); root_->num_keys = 1; root_->leaf = true; index_entries_.push_back(entry); return; }
// Search for the appropriate leaf node to insert the entry into
BTreeNode* node = root_;
while (!node->leaf) {
unsigned int index = 0;
while (index < node->num_keys && node->keys[index] < entry.file_name) {
++index;
}
node = node->children[index];
}
// If the leaf node has enough space, insert the entry directly
if (node->num_keys < max_keys_) {
InsertToNode(node, entry.file_name, nullptr);
index_entries_.push_back(entry);
return;
}
// If the leaf node is full, split it and insert the new entry
BTreeNode* new_node = new BTreeNode();
std::wstring key_to_move;
unsigned int index_to_insert = 0;
if (entry.file_name < node->keys[min_keys_]) {
key_to_move = node->keys[min_keys_ - 1];
index_to_insert = 0;
node->keys.pop_back();
node->num_keys -= 1;
InsertToNode(new_node, entry.file_name, nullptr);
} else if (entry.file_name > node->keys[min_keys_]) {
key_to_move = node->keys[min_keys_];
index_to_insert = 1;
node->keys.erase(node->keys.begin() + min_keys_);
node->num_keys -= 1;
InsertToNode(new_node, entry.file_name, nullptr);
} else {
key_to_move = entry.file_name;
index_to_insert = min_keys_;
InsertToNode(node, entry.file_name, nullptr);
}
// Set up the new node as the right child of the original node
// and update the parent node accordingly
BTreeNode* parent = node;
while (parent != root_) {
if (parent->num_keys < max_keys_) {
InsertToNode(parent, key_to_move, new_node);
index_entries_.push_back(entry);
return;
}
unsigned int index = 0;
while (index < parent->num_keys && parent->keys[index] < key_to_move) {
++index;
}
SplitNode(parent, new_node, index);
new_node = parent->children[index + 1];
key_to_move = parent->keys[index];
parent->keys.erase(parent->keys.begin() + index);
parent->children.erase(parent->children.begin() + index + 1);
parent->num_keys -= 1;
parent = parent->children[0]->leaf ? root_ : parent->children[0];
}
// If the root node needs to be split, create a new root with the split nodes
if (parent->num_keys == max_keys_) {
BTreeNode* new_root = new BTreeNode();
new_root->keys.push_back(key_to_move);
new_root->children.push_back(parent);
new_root->children.push_back(new_node);
new_root->num_keys = 1;
new_root->leaf = false;
root_ = new_root;
parent->leaf = false;
new_node->leaf = false;
}
index_entries_.push_back(entry);
}
e. Delete: void BTree::Delete(std::wstring key) { // Search for the node and index of the key to be deleted BTreeNode* node = SearchNode(key); if (!node) { return; }
unsigned int index = 0;
while (index < node->num_keys && node->keys[index] < key) {
++index;
}
// If the key is not found in the leaf node, return without deleting anything
if (index >= node->num_keys || node->keys[index] != key) {
return;
}
// If the key is found in the leaf node, delete it and update the tree accordingly
DeleteFromNode(node, key);
index_entries_.erase(index_entries_.begin() + node->children[index]);
node->children.erase(node->children.begin() + index);
node->num_keys -= 1;
// If the root node is now empty, make the first child the new root
if (node == root_ && node->num_keys == 0) {
root_ = node->children[0];
node->children.clear();
delete node;
return;
}
// If the node still has enough keys, we are done
if (node->num_keys >= min_keys_) {
return;
}
// If the node has too few keys, try to borrow a key from a sibling
unsigned int index_parent = 0;
while (index_parent < node->parent->num_keys && node->parent->children[index_parent] != node) {
++index_parent;
}
// Try to borrow a key from the right sibling
if (index_parent < node->parent->num_keys && node->parent->children[index_parent + 1]->num_keys > min_keys_) {
BTreeNode* sibling = node->parent->children[index_parent + 1];
node->keys.push_back(node->parent->keys[index_parent]);
node->children.push_back(sibling->children[0]);
node->num_keys += 1;
node->parent->keys[index_parent] = sibling->keys[0];
sibling->keys.erase(sibling->keys.begin());
sibling->children.erase(sibling->children.begin());
sibling->num_keys -= 1;
return;
}
// Try to borrow a key from the left sibling
if (index_parent > 0 && node->parent->children[index_parent - 1]->num_keys > min_keys_) {
BTreeNode* sibling = node->parent->children[index_parent - 1];
node->keys.insert(node->keys.begin(), node->parent->keys[index_parent - 1]);
node->children.insert(node->children.begin(), sibling->children[sibling->num_keys]);
node->num_keys += 1;
node->parent->keys[index_parent - 1] = sibling->keys[sibling->num_keys - 1];
sibling->keys.erase(sibling->keys.begin() + sibling->num_keys - 1);
sibling->children.erase(sibling->children.begin() + sibling->num_keys);
sibling->num_keys -= 1;
return;
}
// If both siblings have too few keys, merge with the right sibling
if (index_parent < node->parent->num_keys) {
MergeNodes(node->parent, index_parent);
return;
}
// If both siblings have too few keys and the node is the last child of the parent, merge with the left sibling
MergeNodes(node->parent, index_parent - 1);
return;
}
f. Print: void BTree::Print() { if (root_) { PrintNodes(root_, L"", true); } }
void BTree::PrintNodes(BTreeNode* node, std::wstring prefix, bool is_last) { std::wcout << prefix; std::wcout << (is_last ? L"\u2514" : L"\u251C"); for (unsigned int i = 0; i < node->num_keys; ++i) { std::wcout << node->keys[i] << " "; } std::wcout << std::endl; for (unsigned int i = 0; i < node->num_keys + 1; ++i) { if (node->children[i]) { PrintNodes(node->children[i], prefix + (is_last ? L" " : L"\u2502 "), i == node->num_keys); } } }
a. SearchNode: BTreeNode* BTree::SearchNode(std::wstring key) { BTreeNode* node = root_; while (node) { unsigned int index = 0; while (index < node->num_keys && node->keys[index] < key) { ++index; } if (index < node->num_keys && node->keys[index] == key) { return node; } node = node->leaf ? nullptr : node->children[index]; } return nullptr; }
b. SplitNode: void BTree::SplitNode(BTreeNode* node, BTreeNode* child, unsigned int index) { BTreeNode* new_node = new BTreeNode(); new_node->num_keys = min_keys_; for (unsigned int i = 0; i < min_keys_; ++i) { new_node->keys.push_back(node->keys[min_keys_ + i]); new_node->children.push_back(node->children[min_keys_ + i + 1]); } new_node->children.push_back(node->children[max_keys_ + 1]); new_node->leaf = node->leaf; node->keys.erase(node->keys.begin() + min_keys_, node->keys.end()); node->children.erase(node->children.begin() + min_keys_ + 1, node->children.end()); node->num_keys = min_keys_; node->children[index] = child; node->children.insert(node->children.begin() + index + 1, new_node); node->keys.insert(node->keys.begin() + index, new_node->keys[0]); node->num_keys += 1; new_node->keys.erase(new_node->keys.begin()); }
c. InsertToNode: void BTree::InsertToNode(BTreeNode* node, std::wstring key, BTreeNode* right_child) { unsigned int index = 0; while (index < node->num_keys && node->keys[index] < key) { ++index; } if (right_child) { node->children.insert(node->children.begin() + index + 1, right_child); } node->keys.insert(node->keys.begin() + index, key); node->num_keys += 1; }
d. DeleteFromNode: void BTree::DeleteFromNode(BTreeNode* node, std::wstring key) { unsigned int index = 0; while (index < node->num_keys && node->keys[index] < key) { ++index; } if (index < node->num_keys && node->keys[index] == key) { if (node->leaf) { node->keys.erase(node->keys.begin() + index); node->num_keys -= 1; return; } BTreeNode* pred = node->children[index]; while (!pred->leaf || pred->num_keys > min_keys_) { pred = pred->children[pred->num_keys]; } std::wstring predecessor_key = pred->keys[pred->num_keys - 1]; DeleteFromNode(pred, predecessor_key); node->keys[index] = predecessor_key; return; } if (node->leaf) { return; } bool is_last = (index == node->num_keys); if (node->children[index]->num_keys <= min_keys_) { if (index > 0 && node->children[index - 1]->num_keys > min_keys_) { BTreeNode* sibling = node->children[index - 1]; node->children[index]->keys.insert(node->children[index]->keys.begin(), node->keys[index - 1]); node->children[index]->children.insert(node->children[index]->children.begin(), sibling->children[sibling->num_keys]); node->keys[index - 1] = sibling->keys[sibling->num_keys - 1]; sibling->keys.erase(sibling->keys.begin() + sibling->num_keys - 1); sibling->children.erase(sibling->children.begin() + sibling->num_keys); sibling->num_keys -= 1; node->children[index]->num_keys += 1; return; } if (index < node->num_keys && node->children[index + 1]->num_keys > min_keys_) { BTreeNode* sibling = node->children[index + 1]; node->children[index]->keys.push_back(node->keys[index]); node->children[index]->children.push_back(sibling->children[0]); node->keys[index] = sibling->keys[0]; sibling->keys.erase(sibling->keys.begin()); sibling->children.erase(sibling->children.begin()); sibling->num_keys -= 1; node->children[index]->num_keys += 1; return; } if (index > 0) { MergeNodes(node, index - 1); DeleteFromNode(node->children[index - 1], key); return; } MergeNodes(node, index); DeleteFromNode(node->children[index], key); return; } DeleteFromNode(node->children[index], key); }
e. MergeNodes: void BTree::MergeNodes(BTreeNode* parent, unsigned int index) { BTreeNode* left_child = parent->children[index]; BTreeNode* right_child = parent->children[index + 1]; left_child->keys.push_back(parent->keys[index]); left_child->num_keys += 1; left_child->children.push_back(right_child->children[0]); left_child->keys.insert(left_child->keys.end(), right_child->keys.begin(), right_child->keys.end()); left_child->children.insert(left_child->children.end(), right_child->children.begin() + 1, right_child->children.end()); left_child->num_keys += right_child->num_keys; parent->keys.erase(parent->keys.begin() + index); parent->children.erase(parent->children.begin() + index + 1); parent->num_keys -= 1; delete right_child; }
Not yet included in a published release.