做 九度笔记之 1467:二叉排序树时又重新翻阅了算法导论里相关的知识,之前都是看看也没有动手实践,这次就顺便把二叉排序数的其它函数都实现了下,还真发现了不少问题。
以下前提条件是所有的数都互不相同。
biNode* search(biNode *root,int value){ biNode*x =root; while(x!=NULL && x->value!=value){ if(x->value > value){ x = x->Left; }else x = x->Right; } return x; }如果找不到就返回NULL
while(x!=NULL && x->value!=value)
biNode* treeMax(biNode *root){ biNode* x = root; while(x->Right!=NULL){ x = x->Right; } return x; } biNode* treeMin(biNode *root){ biNode* x = root; while(x->Left!=NULL){ x = x->Left; } return x; }
while(x->Right!=NULL)
while(x->Left!=NULL)
void insert(biNode *&root,int value){ biNode *temNode = new biNode(value); if(root==NULL){ root = temNode; return; } biNode *y = NULL; biNode *x = root; while(x!=NULL){ if(value > x->value){ y = x; x = x->Right; }else{ y = x; x = x->Left; } } if(value < y->value){ y->Left = temNode; }else y->Right = temNode; temNode->Parent = y; }这里需要注意的是参数 root 应为
biNode *&root因为root初始为NULL, 出入后root的值就改变了,但是要想更改root的值就要用引用或者指针,也就是指针的指针,我们在这里用指针的引用。
然后需要注意的就是 要先判断root是否为NULL
这里的循环判断条件
while(x!=NULL)
biNode* treeSuccessor(biNode *x){ if(x->Right!=NULL){ return treeMin(x->Right); } biNode *y = x->Parent; while(y!=NULL && x==y->Right){ x = y; y = y->Parent; } return y; } biNode* treePredecessor(biNode *x){ if(x->Left!=NULL){ return treeMax(x->Left); } biNode *y = x->Parent; while(y!=NULL && x==y->Left){ x = y; y = y->Parent; } return y; }
void transplant(biNode *&root,biNode *u,biNode *v){// MUST *& ROOT if(u->Parent==NULL) root = v;//ATTENTION else if(u == u->Parent->Left) u->Parent->Left = v;// else u->Parent->Right = v;// if(v!=NULL) v->Parent = u->Parent; } void deleteNode(biNode *&root, biNode *z){// MUST *& ROOT if(z->Left==NULL){ transplant(root,z,z->Right); }else if(z->Right == NULL) transplant(root,z,z->Left); else{ //biNode *y = treeSuccessor(z);//ATTENION biNode *y = treeMin(z->Right);//ATTENION if(y->Parent!=z){ transplant(root,y,y->Right); y->Right = z->Right; z->Right->Parent = y; } transplant(root,z,y); y->Left = z->Left;//ATTENTION z->Left->Parent = y;// } }
void transplant(biNode *&root,biNode *u,biNode *v)
void deleteNode(biNode *&root, biNode *z)
首先要谨记root为指针,指针保存的是地址,一定要注意函数是否有可能需要修改root的值,是的话就要把root的参数 为指针的引用, 因为只有这样才能真正的修改root的值。
//============================================================================ // Name : BiTree.cpp // Author : wdy // Version : // Copyright : Your copyright notice // Description : Hello World in C++, Ansi-style //============================================================================ #include <iostream> using namespace std; struct biNode{ int value; biNode* Parent; biNode* Left; biNode* Right; public: biNode(int value_):value(value_),Parent(NULL),Left(NULL),Right(NULL){}; }; //biNode *root=NULL; void preWalkPrintTree(biNode *root){ if(root==NULL){ return; } std::cout<< root->value <<" "; preWalkPrintTree(root->Left); preWalkPrintTree(root->Right); } biNode* search(biNode *root,int value){ biNode*x =root; while(x!=NULL && x->value!=value){ if(x->value > value){ x = x->Left; }else x = x->Right; } return x; } biNode* treeMax(biNode *root){ biNode* x = root; while(x->Right!=NULL){ x = x->Right; } return x; } biNode* treeMin(biNode *root){ biNode* x = root; while(x->Left!=NULL){ x = x->Left; } return x; } void insert(biNode *&root,int value){ biNode *temNode = new biNode(value); if(root==NULL){ root = temNode; return; } biNode *y = NULL; biNode *x = root; while(x!=NULL){ if(value > x->value){ y = x; x = x->Right; }else{ y = x; x = x->Left; } } if(value < y->value){ y->Left = temNode; }else y->Right = temNode; temNode->Parent = y; } biNode* treeSuccessor(biNode *x){ if(x->Right!=NULL){ return treeMin(x->Right); } biNode *y = x->Parent; while(y!=NULL && x==y->Right){ x = y; y = y->Parent; } return y; } biNode* treePredecessor(biNode *x){ if(x->Left!=NULL){ return treeMax(x->Left); } biNode *y = x->Parent; while(y!=NULL && x==y->Left){ x = y; y = y->Parent; } return y; } void transplant(biNode *&root,biNode *u,biNode *v){// MUST *& ROOT if(u->Parent==NULL) root = v;//ATTENTION else if(u == u->Parent->Left) u->Parent->Left = v;// else u->Parent->Right = v;// if(v!=NULL) v->Parent = u->Parent; } void deleteNode(biNode *&root, biNode *z){// MUST *& ROOT if(z->Left==NULL){ transplant(root,z,z->Right); }else if(z->Right == NULL) transplant(root,z,z->Left); else{ //biNode *y = treeSuccessor(z);//ATTENION biNode *y = treeMin(z->Right);//ATTENION if(y->Parent!=z){ transplant(root,y,y->Right); y->Right = z->Right; z->Right->Parent = y; } transplant(root,z,y); y->Left = z->Left;//ATTENTION z->Left->Parent = y;// } } void test(){ int num[11] = {15,6,18,3,7,17,20,2,4,13,9}; biNode *root=NULL;//ATTENTION std::cout<<"insert"<<std::endl; for(int i = 0;i<11;i++){ insert(root,num[i]); /* preWalkPrintTree(root); std::cout<<std::endl; std::cout<<"tree max"<< treeMax(root)->value <<std::endl; std::cout<<"tree min"<< treeMin(root)->value <<std::endl; */ } //std::cout<<"2 succe"<<treeSuccessor(search(root,2))->value<<std::endl;; //std::cout<<"4 succe"<<treeSuccessor(search(root,4))->value<<std::endl;; //std::cout<<"9 succe"<<treeSuccessor(search(root,9))->value<<std::endl;; //std::cout<<"20 succe"<<treeSuccessor(search(root,20))->value<<std::endl;; //std::cout<<"15 succe"<<treeSuccessor(search(root,15))->value<<std::endl;; //std::cout<<"13 succe"<<treeSuccessor(search(root,13))->value<<std::endl;; //std::cout<<"****"<<std::endl; //std::cout<<"2 succe"<<treePredecessor(search(root,2))->value<<std::endl;; //std::cout<<"4 succe"<<treePredecessor(search(root,4))->value<<std::endl;; //std::cout<<"9 succe"<<treePredecessor(search(root,9))->value<<std::endl;; //std::cout<<"20 succe"<<treePredecessor(search(root,20))->value<<std::endl;; //std::cout<<"15 succe"<<treePredecessor(search(root,15))->value<<std::endl;; //std::cout<<"7 succe"<<treePredecessor(search(root,7))->value<<std::endl;; preWalkPrintTree(root); std::cout<<"****"<<std::endl; deleteNode(root,search(root,15));//Attention //biNode = preWalkPrintTree(root); } int main() { //cout << "!!!Hello World!!!" << endl; // prints !!!Hello World!!! test(); return 0; }