- // BSTree.cpp : Defines the entry point for the console application.
- //问题描述:二叉查找树的实现
- //创建者:QGX
- //创建时间:2008-2-1
- #include "stdafx.h"
- #include<iostream>
- using namespace std;
- //二叉查找树的定义
- #define NULL 0
- typedef int Type;
- class TreeNode
- {
- friend class BSTree;
- private:
- TreeNode *rchild,*lchild;
- Type data;
- };
- class BSTree
- {
- private:
- TreeNode *tree;
- TreeNode* Search(TreeNode *t,Type x);
- void traverse(TreeNode *t);
- public:
- BSTree(){tree=NULL;}
- ~BSTree(){delete tree;}
- TreeNode* RSearch(Type x);
- TreeNode* ISearch(Type x);
- void Insert(Type x);
- void Delete(Type x);
- void Traverse(){traverse(tree);}
- };
- TreeNode* BSTree::RSearch(Type x)
- {
- return Search(tree,x);
- }
- TreeNode* BSTree::Search(TreeNode* t,Type x)
- {
- //递归查找
- if(!t)return NULL;
- if(t->data==x)return t;
- else if(t->data<x)return Search(t->rchild,x);
- else return Search(t->lchild,x);
- }
- TreeNode* BSTree:: ISearch(Type x)
- {
- //迭代查找
- TreeNode* t=tree;
- bool found=false;
- while(!found && t)
- {
- if(t->data==x)
- {
- found=true;
- }
- else if(t->data<x)
- {
- t=t->rchild;
- }
- else t=t->lchild;
- }
- if(found) return t;
- else return NULL;
- }
- void BSTree:: Insert(Type x)
- {
- TreeNode *p,*q;
- bool found=false;
- p=tree;
- q=NULL;
- while(p)
- {
- if(p->data==x)
- {
- found=true;
- break;
- }
- else
- {
- q=p;
- if(p->data>x)p=p->lchild;
- else p=p->rchild;
- }
- }
- if(found)return;
- else
- {
- TreeNode *temp=new TreeNode;
- temp->lchild=NULL;
- temp->rchild=NULL;
- temp->data=x;
- if(!tree)tree=temp;//根结点
- else
- {
- if(q->data>x)q->lchild=temp;
- else q->rchild=temp;
- }
- }
- }
- void BSTree::Delete(Type x)
- {
- TreeNode *p=tree,*q=tree;
- bool found=false;
- while(p)
- {
- if(p->data==x)
- { //找到待删除的结点
- found=true;
- break;
- }
- else
- { //q为p的父结点
- q=p;
- if(p->data>x)p=p->lchild;
- else p=p->rchild;
- }
- }
- if(!found)
- {
- //不存在此结点
- cout<<"Does not exist this node"<<endl;
- return;
- }
- else
- {
- if(p==q)
- {
- //删除的是根结点
- if(!p->rchild && !p->lchild)
- { //无左右子树
- delete p;
- tree=NULL;
- }
- else if(!p->rchild)
- { //无右子树
- tree=p->lchild;
- delete p;
- }
- else if(!p->lchild)
- { //无左子树
- tree=p->rchild;
- delete p;
- }
- else
- {
- //有左右子树
- //找到x的直接后继结点,交换,删除
- TreeNode *p2=p->rchild;
- TreeNode *p3=p->rchild;
- while(p2->lchild)
- {
- p3=p2;
- p2=p2->lchild;
- }
- if(p3==p2)
- {
- p2->lchild=p->lchild;
- tree=p2;
- delete p;
- }
- else
- {
- p->data=p2->data;
- p3->lchild=p2->rchild;
- delete p2;
- }
- }
- }//end_if
- else
- {
- //删除非根结点
- if(!p->rchild && !p->lchild)
- {
- if(q->data>x)q->lchild=NULL;
- else q->rchild=NULL;
- delete p;
- }
- else if(!p->rchild)
- {
- if(q->data>x)q->lchild=p->lchild;
- else q->rchild=p->lchild;
- delete p;
- }
- else if(!p->lchild)
- {
- if(q->data>x)q->lchild=p->rchild;
- else q->rchild=p->rchild;
- delete p;
- }
- else
- {
- //有左右子树
- //找到x的直接后继结点,交换,删除
- TreeNode *p2,*p3;
- p2=p->lchild;
- p3=p->lchild;
- while(p2->rchild)
- {
- p3=p2;
- p2=p2->rchild;
- }
- if(p2==p3)
- {
- p->data=p2->data;
- p->lchild=p2->lchild;
- delete p2;
- }
- else
- {
- p->data=p2->data;
- p3->rchild=p2->lchild;
- delete p2;
- }
- }//end_else
- }//end_else
- }//end_else
- }
- void BSTree::traverse(TreeNode *t)
- {
- if(!t)return;
- else
- {
- traverse(t->lchild);
- cout<<t->data<<endl;
- traverse(t->rchild);
- }
- }
- int _tmain(int argc, _TCHAR* argv[])
- {
- BSTree *t=new BSTree();
- const int N=8;
- Type data[N]={8,6,5,7,12,9,20,14};
- for(int i=0;i<N;i++)
- t->Insert(data[i]);
- cout<<"The tree is:"<<endl;
- t->Traverse();
- //t->Delete(6);
- //t->Delete(8);
- //t->Delete(12);
- //t->Delete(5);
- //t->Delete(20);
- //t->Insert(100);
- //t->Insert(3);
- //t->Insert(12);
- //t->Insert(19);
- //t->Traverse();
- //if(t->ISearch(19))cout<<"OK"<<endl;
- //if(t->RSearch(19))cout<<"OK too"<<endl;
- getchar();
- return 0;
- }
二叉查找树
最新推荐文章于 2020-02-14 09:10:00 发布
1301

被折叠的 条评论
为什么被折叠?



