//Header File Binary Search Tree #ifndef H_binaryTree #define H_binaryTree #include //Definition of the node template struct nodeType { elemType info; nodeType *llink; nodeType *rlink; }; //Definition of the class template class bSearchTreeType { public: const bSearchTreeType& operator= (const bSearchTreeType&); //Overload the assignment operator. bool isEmpty(); //Function to determine if the binary tree is empty. //Postcondition: Returns true if the binary tree is empty; // otherwise, returns false. void inorderTraversal(); //Function to do an inorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the inorder sequence. void preorderTraversal(); //Function to do a preorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the preorder sequence. void postorderTraversal(); //Function to do a postorder traversal of the binary tree. //Postcondition: The nodes of the binary tree are output // in the postorder sequence. int treeHeight(); //Function to deteremine the height of the binary tree. //Postcondition: The height of the binary tree is returned. int treeNodeCount(); //Function to determine the number of nodes in the //binary tree. //Postcondition: The number of nodes in the binary tree // is returned. int treeLeavesCount(); //Function to determine the number of leaves in the //binary tree. //Postcondition: The number of leaves in the binary tree // is returned. void destroyTree(); //Deallocates the memory space occupied by the binary tree. //Postcondition: root = NULL; bSearchTreeType(const bSearchTreeType& otherTree); //copy constructor bSearchTreeType(); //default constructor bool search(const elemType& searchItem); //Function to determine if searchItem is in the binary //search tree. //Postcondition: Returns true if searchItem is found in the // binary search tree; otherwise, returns false. void insert(const elemType& insertItem); //Function to insert insertItem in the binary search tree. //Postcondition: If no node in the binary search tree has // the same info as insertItem, a node with the // info insertItem is created and inserted in the //binary search tree. void deleteNode(const elemType& deleteItem); //Function to delete deleteItem from the binary search tree //Postcondition: If a node with the same info as deleteItem // is found, it is deleted from the binary // search tree. ~bSearchTreeType(); //destructor protected: nodeType *root; private: void copyTree(nodeType* &copiedTreeRoot, nodeType* otherTreeRoot); //Function to make a copy of the binary tree to //which otherTreeRoot points. //Postcondition: The pointer copiedTreeRoot points to // the root of the copied binary tree. void destroy(nodeType* &p); //Function to destroy the binary tree to which p points. //Postcondition: The nodes of the binary tree to which // p points are deallocated. // p = NULL. void inorder(nodeType *p); //Function to do an inorder traversal of the binary //tree to which p points. //Postcondition: The nodes of the binary tree to which p // points are output in the inorder sequence. void preorder(nodeType *p); //Function to do a preorder traversal of the binary //tree to which p points. //Postcondition: The nodes of the binary tree to which p // points are output in the preorder sequence. void postorder(nodeType *p); //Function to do a postorder traversal of the binary //tree to which p points. //Postcondition: The nodes of the binary tree to which p // points are output in the postorder sequence. int height(nodeType *p); //Function to determine the height of the binary tree //to which p points. //Postcondition: The height of the binary tree to which p // points is returned. int max(int x, int y); //Function to determine the larger of x and y. //Postcondition: The larger of x and y is returned. int nodeCount(nodeType *p); //Function to determine the number of nodes in the binary //tree to which p points. //Postcondition: The number of nodes in the binary tree // to which p points is returned. int leavesCount(nodeType *p); //Function to determine the number of leaves in the binary //tree to which p points. //Postcondition: The number of nodes in the binary tree // to which p points is returned. void deleteFromTree(nodeType* &p); //Function to delete the node, to which p points, from the //binary search tree. //Postcondition: The node to which p points is deleted // from the binary search tree. }; //Definition of member functions template bSearchTreeType::bSearchTreeType() { root = NULL; } template bool bSearchTreeType::isEmpty() { return (root == NULL); } template void bSearchTreeType::inorderTraversal() { inorder(root); } template void bSearchTreeType::preorderTraversal() { preorder(root); } template void bSearchTreeType::postorderTraversal() { postorder(root); } template int bSearchTreeType::treeHeight() { return height(root); } template int bSearchTreeType::treeNodeCount() { return nodeCount(root); } template int bSearchTreeType::treeLeavesCount() { return leavesCount(root); } template void bSearchTreeType::copyTree (nodeType* &copiedTreeRoot, nodeType* otherTreeRoot) { if(otherTreeRoot == NULL) copiedTreeRoot = NULL; else { copiedTreeRoot = new nodeType; copiedTreeRoot->info = otherTreeRoot->info; copyTree(copiedTreeRoot->llink, otherTreeRoot->llink); copyTree(copiedTreeRoot->rlink, otherTreeRoot->rlink); } } //end copyTree template void bSearchTreeType::inorder(nodeType *p) { if(p != NULL) { inorder(p->llink); cout<info<<" "; inorder(p->rlink); } } template void bSearchTreeType::preorder(nodeType *p) { if(p != NULL) { cout<info<<" "; preorder(p->llink); preorder(p->rlink); } } template void bSearchTreeType::postorder(nodeType *p) { if(p != NULL) { postorder(p->llink); postorder(p->rlink); cout<info<<" "; } } //Overload the assignment operator template const bSearchTreeType& bSearchTreeType:: operator=(const bSearchTreeType& otherTree) { if(this != &otherTree) //avoid self-copy { if(root != NULL) //if the binary tree is not empty, //destroy the binary tree destroy(root); if(otherTree.root == NULL) //otherTree is empty root = NULL; else copyTree(root, otherTree.root); }//end else return *this; } template void bSearchTreeType::destroy(nodeType* &p) { if(p != NULL) { destroy(p->llink); destroy(p->rlink); delete p; p = NULL; } } template void bSearchTreeType::destroyTree() { destroy(root); } //copy constructor template bSearchTreeType::bSearchTreeType (const bSearchTreeType& otherTree) { if(otherTree.root == NULL) //otherTree is empty root = NULL; else copyTree(root, otherTree.root); } template bSearchTreeType::~bSearchTreeType() { destroy(root); } template int bSearchTreeType::height(nodeType *p) { if(p == NULL) return 0; else return 1 + max(height(p->llink), height(p->rlink)); } template int bSearchTreeType::max(int x, int y) { if(x >= y) return x; else return y; } template int bSearchTreeType::nodeCount(nodeType *p) { // TODO: Write the definition of the function nodeCount return 0; } template int bSearchTreeType::leavesCount(nodeType *p) { // TODO: Write the definition of the function nodeCount return 0; } template bool bSearchTreeType::search(const elemType& searchItem) { nodeType *current; bool found = false; if(root == NULL) cerr<<"Cannot search the empty tree."<info == searchItem) found = true; else if(current->info > searchItem) current = current->llink; else current = current->rlink; }//end while }//end else return found; }//end search template void bSearchTreeType::insert(const elemType& insertItem) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current nodeType *newNode; //pointer to create the node newNode = new nodeType; assert(newNode != NULL); newNode->info = insertItem; newNode->llink = NULL; newNode->rlink = NULL; if(root == NULL) root = newNode; else { current = root; while(current != NULL) { trailCurrent = current; if(current->info == insertItem) { cerr<<"The insert item is already in the list -- "; cerr<<"duplicates are not allowed."<info > insertItem) current = current->llink; else current = current->rlink; }//end while if(trailCurrent->info > insertItem) trailCurrent->llink = newNode; else trailCurrent->rlink = newNode; } }//end insert template void bSearchTreeType::deleteNode(const elemType& deleteItem) { nodeType *current; //pointer to traverse the tree nodeType *trailCurrent; //pointer behind current bool found = false; if(root == NULL) cerr<<"Cannot delete from the empty tree."<info == deleteItem) found = true; else { trailCurrent = current; if(current->info > deleteItem) current = current->llink; else current = current->rlink; } }//end while if(current == NULL) cout<<"The delete item is not in the tree."<info > deleteItem) deleteFromTree(trailCurrent->llink); else deleteFromTree(trailCurrent->rlink); }//end if } }//end deleteNode template void bSearchTreeType::deleteFromTree (nodeType* &p) { nodeType *current; //pointer to traverse //the tree nodeType *trailCurrent; //pointer behind current nodeType *temp; //pointer to delete the node if(p == NULL) cerr<<"Error: The node to be deleted is NULL." <llink == NULL && p->rlink == NULL) { temp = p; p = NULL; delete temp; } else if(p->llink == NULL) { temp = p; p = temp->rlink; delete temp; } else if(p->rlink == NULL) { temp = p; p = temp->llink; delete temp; } else { current = p->llink; trailCurrent = NULL; while(current->rlink != NULL) { trailCurrent = current; current = current->rlink; }//end while p->info = current->info; if(trailCurrent == NULL) //current did not move; //current == p->llink; adjust p p->llink = current->llink; else trailCurrent->rlink = current->llink; delete current; }//end else }//end deleteFromTree #endif