二叉查找树节点结构声明
class BSNode{ public: BSNode(); public: int key; BSNode* pParent; BSNode* pLeft; BSNode* pRight; };
主要函数及主函数
// BinarySearchTree.cpp : 定义控制台应用程序的入口点。 // #include “stdafx.h” #include “Node.h” #include<iostream> #include<stdlib.h> #include<time.h> using namespace std; #define random(x) (rand()%x) //插入一个节点 void TreeInsert(BSNode** proot, BSNode* pnode); //中序遍历 void InorderTreeWalk(BSNode* proot); //采用中序遍历递归的方法在树中寻找一个元素 void FindNodeInBSTree(BSNode* proot, int key, BSNode** pnode); //返回以节点为根的最小元素 BSNode* TreeMinimum(BSNode* pnode); //返回以节点为根的最大元素 BSNode* TreeMaxmum(BSNode* pnode); //返回节点的后继 BSNode* TreeSuccessor(BSNode* pnode); //删除一个节点 BSNode* TreeDelete(BSNode* proot,BSNode* pnode); //The Main Method int _tmain(int argc, _TCHAR* argv[]) { int num = 10; cout << “请输入需要排序数组的长度,数组将随机生成:” <<endl; cin >> num; int* arry = new int[num]; //srand( (int)time(0) ); for ( int x=0; x<num; x++ ) { arry[x] = random(10000); cout << arry[x] << endl; } //构造二叉查找树 cout << “construct binary search tree”<<endl; BSNode* proot = NULL; for( int x=0; x<num; x++) { BSNode* pnode = new BSNode(); pnode->key = arry[x]; TreeInsert( &proot, pnode); } //Inorder – Tree – Walk cout << “Inorder Tree Walk” <<endl; InorderTreeWalk( proot ); int findNum; cout << “请输入需要删除的元素:” <<endl; cin >> findNum; BSNode* pfindNode = NULL ; FindNodeInBSTree( proot, findNum, &pfindNode); cout << pfindNode->key << endl; //删除元素 cout << “Delete Node” <<endl; BSNode* pdel = TreeDelete(proot, pfindNode); delete pdel; cout << “After Delete Inorder Tree Walk” <<endl; InorderTreeWalk( proot ); system(“pause”); delete[] arry; return 0; } // void InorderTreeWalk(BSNode* x) { if ( x != NULL ) { //cout << “In Left”<< endl; InorderTreeWalk( x->pLeft); //cout << “Out Left”<< endl; cout << x->key << endl; //cout << “In Right”<< endl; InorderTreeWalk( x->pRight); //cout << “Out Right”<< endl; } } // void TreeInsert(BSNode** proot, BSNode* pnode) { BSNode* y = NULL; BSNode* x = *proot; while( x!= NULL ) { y = x; if ( pnode->key < x->key ) { x = x->pLeft; } else { x = x->pRight; } } pnode->pParent = y; if ( y == NULL) { *proot = pnode; } else if ( pnode->key < y->key ) { y->pLeft = pnode; } else { y->pRight = pnode; } } // void FindNodeInBSTree(BSNode* x, int key, BSNode** pnode) { if ( x != NULL && *pnode == NULL ) { cout << “In Left”<< endl; FindNodeInBSTree( x->pLeft, key, pnode); cout << “Out Left”<< endl; cout << x->key << endl; if ( x->key == key ) { *pnode = x; cout << “===========” << x->key << “============” << endl; return; } cout << “In Right”<< endl; FindNodeInBSTree( x->pRight, key, pnode); cout << “Out Right”<< endl; } } // BSNode* TreeDelete(BSNode* proot,BSNode* pnode) { //分三种情况实现 //1 左右子树都为空 if ( pnode->pLeft == NULL && pnode->pRight == NULL ) { BSNode* pParent = pnode->pParent; if ( pnode == pParent->pLeft ) { pParent->pLeft = NULL; } else { pParent->pRight = NULL; } return pnode; } //2 左子树或右子树不为空 if ( pnode->pLeft == NULL || pnode->pRight == NULL ) { BSNode* pParent = pnode->pParent; //判断是左子树 还是右子树 BSNode* pChild = NULL; if ( pnode->pLeft != NULL ) { pChild = pnode->pLeft; } else { pChild = pnode->pRight; } //修改父节点的指针 if ( pnode == pParent->pLeft ) { pParent->pLeft = pChild; } else { pParent->pRight = pChild; } return pnode; } //3 左右子树都不为空,删除的是后继节点,并用后继节点的内容替换到pnode if ( pnode->pLeft != NULL && pnode->pRight != NULL ) { //这里选出的后继节点一定是pnode右子树的最小元素 //因为如果是选择pnode右子树为空的这种情况,则pnode在上面的判断中就会被删除掉 BSNode* pSuccessor = TreeSuccessor( pnode ); BSNode* pSubSucor = NULL; if ( pSuccessor->pLeft != NULL ) { pSubSucor = pSuccessor->pLeft; } else { pSubSucor = pSuccessor->pRight; } if ( pSubSucor != NULL ) { pSubSucor->pParent = pSuccessor->pParent; } BSNode* pParentSucor = pSuccessor->pParent; if ( pSuccessor == pParentSucor->pLeft ) { pParentSucor->pLeft = pSubSucor; } else { pParentSucor->pRight = pSubSucor; } pnode->key = pSuccessor->key; return pSuccessor; } return NULL; } // BSNode* TreeSuccessor(BSNode* pnode) { if ( pnode->pRight != NULL ) { return TreeMinimum( pnode->pRight ); } // BSNode* y = pnode->pParent; while ( y != NULL && pnode == y->pRight ) { pnode = y; y = pnode->pParent; } return y; } // BSNode* TreeMinimum(BSNode* pnode) { while ( pnode->pLeft != NULL ) { pnode = pnode->pLeft; } return pnode; } // BSNode* TreeMaxmum(BSNode* pnode) { while( pnode->pRight != NULL ) { pnode = pnode->pRight; } return pnode; }