#ifndef H_bTree #define H_bTree #include #include using namespace std; //************************************************************* // Author: D.S. Malik // // class bTree // This class specifies the basic operations to implement a // B-tree. //************************************************************* template struct bTreeNode { int recCount; recType list[bTreeOrder - 1]; bTreeNode *children[bTreeOrder]; }; template class bTree { public: bool search(const recType& searchItem); //Function to determine if searchItem is in the B-tree. //Postcondition: Returns true if searchItem is found in the // B-tree; otherwise, returns false. void insert(const recType& insertItem); //Function to insert insertItem in the B-tree. //Postcondition: If insertItem is not in the the B-tree, it // is inserted in the B-tree. void inOrder(); //Function to do an inorder traversal of the B-tree. bTree(); //constructor //Add additional members as needed protected: bTreeNode *root; private: void searchNode(bTreeNode *current, const recType& item, bool& found, int& location); void insertBTree(/*parameters*/); void insertNode(bTreeNode *current, const recType& insertItem, bTreeNode* &rightChild, int insertPosition); void splitNode (bTreeNode *current, const recType& insertItem, bTreeNode* rightChild, int insertPosition, bTreeNode* &rightNode, recType &median); void recInorder(bTreeNode *current); }; template bTree::bTree() { root = NULL; } //end constructor template bool bTree::search(const recType& searchItem) { bool found = false; int location; bTreeNode *current; current = root; while (current != NULL && !found) { searchNode(current, item, found, location); if (!found) current = current->children[location]; } return found; } //end search template void bTree::searchNode (bTreeNode *current, const recType& item, bool& found, int& location) { location = 0; while (location < current->recCount && item > current->list[location]) location++; if (location < current->recCount && item == current->list[location]) found = true; else found = false; } //end searchNode template void bTree::insert(const recType& insertItem) { bool isTaller = false; recType median; bTreeNode *rightChild; insertBTree(root, insertItem, median, rightChild, isTaller); if (isTaller) //the tree is initially empty or the root //was split by the function insertBTree { bTreeNode *tempRoot; tempRoot = new bTreeNode; tempRoot->recCount = 1; tempRoot->list[0] = median; tempRoot->children[0] = root; tempRoot->children[1] = rightChild; root = tempRoot; } } //insert template void bTree::insertBTree (/*parameters*/) { cout << "See Programming Exercise 15." << endl; } //insertBTree template void bTree::insertNode (bTreeNode *current, const recType& insertItem, bTreeNode* &rightChild, int insertPosition) { int index; for (index = current->recCount; index > insertPosition; index--) { current->list[index] = current->list[index - 1]; current->children[index + 1] = current->children[index]; } current->list[index] = insertItem; current->children[index + 1] = rightChild; current->recCount++; } //end insertNode template void bTree::splitNode (bTreeNode *current, const recType& insertItem, bTreeNode* rightChild, int insertPosition, bTreeNode* &rightNode, recType &median) { rightNode = new bTreeNode; int mid = (bTreeOrder - 1) / 2; if (insertPosition <= mid) //new item goes in the first //half of the node { int index = 0; int i = mid; while (i < bTreeOrder - 1) { rightNode->list[index] = current->list[i]; rightNode->children[index + 1] = current->children[i + 1]; index++; i++; } current->recCount = mid; insertNode(current, insertItem, rightChild, insertPosition); (current->recCount)--; median = current->list[current->recCount]; rightNode->recCount = index; rightNode->children[0] = current->children[current->recCount + 1]; } else //new item goes in the second half of the node { int i = mid + 1; int index = 0; while (i < bTreeOrder - 1) { rightNode->list[index] = current->list[i]; rightNode->children[index + 1] = current->children[i + 1]; index++; i++; } current->recCount = mid; rightNode->recCount = index; median = current->list[mid]; insertNode(rightNode, insertItem, rightChild, insertPosition - mid - 1); rightNode->children[0] = current->children[current->recCount + 1]; } } //splitNode template void bTree::inOrder() { recInorder(root); } // end inOrder template void bTree::recInorder (bTreeNode *current) { if (current != NULL) { recInorder(current->children[0]); for (int i = 0; i < current->recCount; i++) { cout << current->list[i] << " "; recInorder(current->children[i + 1]); } } } //end recInorder #endif