二叉查找树的模板类实现(二)
下面贴出所有的代码:
BinaryTreeNode.h 文件
template<class key,class value> class BinaryTreeNode { private: key theKey; value theValue; BinaryTreeNode<key,value> *parent; BinaryTreeNode<key,value> *left; BinaryTreeNode<key,value> *right; public: BinaryTreeNode(); BinaryTreeNode(key theKey,value theValue); key getKey(); value getValue(); BinaryTreeNode<key,value> *getLeft(); BinaryTreeNode<key,value> *getRight(); BinaryTreeNode<key,value> *getParent(); void setKey(key theKey); void setValue(value theValue); void setLeft(BinaryTreeNode<key,value> *left); void setRight(BinaryTreeNode<key,value> *Right); void setParent(BinaryTreeNode<key,value> *parent); }; template<class key,class value> BinaryTreeNode<key,value>::BinaryTreeNode() { parent=NULL; left = NULL; right = NULL; } template<class key,class value> BinaryTreeNode<key,value>::BinaryTreeNode(key theKey,value theValue) { this->theKey = theKey; this->theValue = theValue; parent=NULL; left = NULL; right = NULL; } template<class key,class value> key BinaryTreeNode<key,value>::getKey() { return theKey; } template<class key,class value> value BinaryTreeNode<key,value>::getValue() { return theValue; } template<class key,class value> BinaryTreeNode<key,value> *BinaryTreeNode<key,value>::getLeft() { return left; } template<class key,class value> BinaryTreeNode<key,value> *BinaryTreeNode<key,value>::getRight() { return right; } template<class key,class value> BinaryTreeNode<key,value> *BinaryTreeNode<key,value>::getParent() { return parent; } template<class key,class value> void BinaryTreeNode<key,value>::setValue(value theValue) { this->theValue = theValue; } template<class key,class value> void BinaryTreeNode<key,value>::setKey(key theKey) { this->theKey = theKey; } template<class key,class value> void BinaryTreeNode<key,value>::setLeft(BinaryTreeNode<key,value> *left) { this->left = left; } template<class key,class value> void BinaryTreeNode<key,value>::setRight(BinaryTreeNode<key,value> *right) { this->right = right; } template<class key,class value> void BinaryTreeNode<key,value>::setParent(BinaryTreeNode<key,value> *parent) { this->parent=parent; }
BSTree.h 文件
#include <string.h> #include "BinaryTreeNode.h" template <class key, class value> class BSTree { private: BinaryTreeNode<key,value> *root; public: BSTree(); public: BinaryTreeNode<key,value> *get(key theKey); BinaryTreeNode<key,value> *getRoot(); BinaryTreeNode<key,value> *remove(key theKey); void insert(key theKey, value theValue); void ascend(BinaryTreeNode<key,value> *); void outPut(); BinaryTreeNode<key,value> *tree_minimum(BinaryTreeNode<key,value> *p); BinaryTreeNode<key,value> *tree_maximum(BinaryTreeNode<key,value> *p); BinaryTreeNode<key,value> *tree_successor(BinaryTreeNode<key,value> *p); };
BSTree.cpp 文件
#include "BSTree.h" #include <iostream> using namespace std; template<class key,class value> BSTree<key,value>::BSTree() { root = NULL; } template<class key,class value> BinaryTreeNode<key,value> *BSTree<key,value>::get(key theKey) { BinaryTreeNode<key,value> *p=root; while(p!=NULL) { if(theKey < p->getKey()) p=p->getLeft(); else if(theKey > p->getKey()) p=p->getRight(); else return p; } return NULL; } template<class key,class value> void BSTree<key,value>::insert(key theKey,value theValue) { BinaryTreeNode<key,value> *p=root; //search pointer BinaryTreeNode<key,value> *parent=NULL;//parent of p; while(p!=NULL) { parent=p; if(theKey < p->getKey()) p=p->getLeft(); else if(theKey > p->getKey()) p=p->getRight(); else { p->setValue(theValue); return; } } BinaryTreeNode<key,value> *newNode = new BinaryTreeNode<key,value>(theKey,theValue); if(root == NULL) root = newNode; else { if(theKey < parent->getKey()) { parent->setLeft(newNode); newNode->setParent(parent); } else { parent->setRight(newNode); newNode->setParent(parent); } } return; } template<class key,class value> BinaryTreeNode<key,value> *BSTree<key,value>::remove(key theKey) { BinaryTreeNode<key,value> *deletedNode=get(theKey); if(deletedNode==NULL) return NULL; BinaryTreeNode<key,value> *todelete; BinaryTreeNode<key,value> *nextNode; if(deletedNode->getLeft()==NULL || deletedNode->getRight()==NULL) todelete = deletedNode; else todelete = tree_successor(deletedNode); if(todelete->getLeft()!=NULL) nextNode=todelete->getLeft(); else nextNode=todelete->getRight(); if(nextNode!=NULL) nextNode->setParent(todelete->getParent()); if(todelete->getParent()==NULL) nextNode=root; else if(todelete->getParent()->getLeft()==todelete) todelete->getParent()->setLeft(nextNode); else todelete->getParent()->setRight(nextNode); if(todelete==deletedNode) { deletedNode->setKey(todelete->getKey()); deletedNode->setValue(todelete->getValue()); } return todelete; } template<class key,class value> void BSTree<key,value>::ascend(BinaryTreeNode<key,value> *p) { if(p!=NULL) { ascend(p->getLeft()); cout<<p->getKey()<<" "<<p->getValue()<<endl; ascend(p->getRight()); } } template<class key,class value> void BSTree<key,value>::outPut() { BinaryTreeNode<key,value> *temp=tree_minimum(root); ascend(temp); } template<class key,class value> BinaryTreeNode<key,value> *BSTree<key,value>::tree_minimum(BinaryTreeNode<key,value> *p) { if(p==NULL) return NULL; BinaryTreeNode<key,value> *pp=p; while(pp->getLeft()!=NULL) pp=pp->getLeft(); return pp; } template<class key,class value> BinaryTreeNode<key,value> *BSTree<key,value>::tree_maximum(BinaryTreeNode<key,value> *p) { if(p==NULL) return NULL; BinaryTreeNode<key,value> *pp=p; while(pp->getRight()!=NULL) pp=pp->getRight(); return pp; } template<class key,class value> BinaryTreeNode<key,value> *BSTree<key,value>::tree_successor(BinaryTreeNode<key,value> *p) { if(p==NULL) return NULL; BinaryTreeNode<key,value> *pp=p; if(pp->getRight()!=NULL) return tree_minimum(pp->getRight()); BinaryTreeNode<key,value> *y=p->getParent(); while(y!=NULL && pp==y->getRight()) { pp=y; y=y->getParent(); } return y; } template<class key,class value> BinaryTreeNode<key,value> *BSTree<key,value>::getRoot() { return root; }
templateInstanceBSTree.cpp 文件
#include "BSTree.cpp" #include <string> using namespace std; template class BSTree<int,string>;
main.cpp 文件
#include <iostream> #include <string.h> #include "BSTree.h" using namespace std; int main() { BSTree<int,string> bstree; bstree.insert(0,"Jim"); bstree.insert(7,"Green"); bstree.insert(2,"Lucy"); bstree.insert(1,"Brown"); bstree.insert(3,"Lily"); bstree.insert(8,"Tom"); bstree.insert(4,"John"); bstree.insert(6,"Peter"); bstree.insert(5,"Joson"); bstree.outPut(); BinaryTreeNode<int,string> *p=bstree.getRoot(); bstree.remove(3); while(p!=NULL) { cout<<p->getKey()<<endl; p=bstree.tree_successor(p); } return 0; }