ADT—二叉查找树

Posted on 2010-04-01 16:49  625747  阅读(345)  评论(0)    收藏  举报

 

代码
#pragma once
#include
<iostream>
using namespace std;

class TreeNode
{
public:
int data;
TreeNode
*left,*right;
TreeNode():left(NULL),right(NULL){}
TreeNode(
int item):data(item),left(NULL),right(NULL){}
};

class BSTree
{
public:
BSTree(
void);
~BSTree(void);
bool isEmpty();
bool Search(const int item);
bool Search(const int item,bool found,TreeNode * local,TreeNode * parent);
void Insert(int item);
void Delete( int item);

TreeNode
* searchTree( int key);
TreeNode
* searchParent(TreeNode * p);

void inorderTravel();
void preorderTravel();
void postorderTravel();

private:
TreeNode
* root;
void inorder(TreeNode *p);
void preorder(TreeNode *p);
void postorder(TreeNode *p);
};

 

代码
#include "BSTree.h"



BSTree::BSTree(
void)
{
root
= NULL;
}

BSTree::
~BSTree(void)
{
}

bool::BSTree ::isEmpty ()
{
return (root ==NULL);
}

void BSTree::Insert (int item)
{
TreeNode
* Node, *current,*trailCurrent, *newNode;

newNode
=new TreeNode;

newNode
->data=item;
newNode
->left =NULL;
newNode
->right =NULL;
if(root==NULL)
root
=newNode;
else
{
current
=root;
while(current!=NULL)
{
trailCurrent
=current;
if(current->data ==item)
{
cerr
<<"The insert item is already in the tree"<<endl;
return;
}
else
if(current->data >item)
current
=current->left;
else
current
=current->right;
}
//新增结点肯定为叶结点
if(trailCurrent->data>item)
trailCurrent
->left =newNode;
else
trailCurrent
->right=newNode;
}
}

void BSTree::Delete (int item)
{
//TreeNode * x = new TreeNode;// 指向包含项的节点
//bool found = Search(item);
//if(!found)
//{
// cout<<"Item not in the BST\n";
// return ;
//}
//
//if(x->left!=NULL&&x->right!=NULL)//处理有两个儿子的节点
//{
// TreeNode * xsucc=x->right;
// while(xsucc->left!=NULL)
// {
// root = xsucc;
// xsucc=xsucc->left;
// }
//
// x->data=xsucc->data; //把xsucc的内容移到x,并令x指向后继,该后继将被删除
// x=xsucc;
//}
//TreeNode * subtree=x->left; //处理有0个或者1个儿子的情况
//if(subtree==NULL) subtree=x->right;
//if(root==NULL) root=subtree;
//else if(root->left==x) root->left=subtree;
//else root->right=subtree;
//delete x;

TreeNode
* p = searchTree(item);
TreeNode
* parent=searchParent(p);



if(p->left==NULL&&p->right==NULL)//没有叶子结点
{
if ( p == root ) //没有树了
{
root
= NULL;
}
if(parent->left==p)parent->left=NULL;
else parent->right=NULL;
}

else
{
if(p->right==NULL)//没右子树,只有左子树
{
if ( p == root ) //是root
{
root
= p->left ;
}
else
{
if(parent->left==p)
parent
->left=p->left;
else parent->right=p->left;
}
}
else if(p->left !=NULL&&p->right != NULL) //有左子树和右子树
{
if ( p == root )
{
if ( p->left ->data < p->right ->data )
{
root
= p->left ;
TreeNode
* s = p->left;
while(s->right != NULL) // p左子的最右子树存在s 中
{
s
=s->right;
}
s
->right=p->right; //p的右子树直接挂到 p左子的最右子 的右子树上
delete p;
}
else
{
root
->right ;
TreeNode
* s = p->left;
while(s->right != NULL) // p左子的最右子树存在s 中
{
s
=s->right;
}
s
->right=p->right; //p的右子树直接挂到 p左子的最右子 的右子树上
delete p;
}
}
else
{
parent
->left=p->left;//p左子树直接挂到父结点左子树上
TreeNode * s = p->left;
while(s->right != NULL) // p左子的最右子树存在s 中
{
s
=s->right;
}
s
->right=p->right; //p的右子树直接挂到 p左子的最右子 的右子树上
delete p;
}
}
else //只有右孩子
{
if ( p == root )
{
root
= p->right;
}
else parent->right = p ->right ;
}
}
return ;
}

bool BSTree::Search(const int item)
{

TreeNode
* local=root;
bool found=false;
while(1)
{
if(found||local==NULL)
break;
if(item<local->data) local=local->left;
else
if(item>local->data) local=local->right;
else found=true;
}
return found;
}

TreeNode
* BSTree::searchTree (int key)
{
TreeNode
* p=root;
while(p!=NULL)
{
if(key==p->data)return p;
else if(key>p->data)p=p->right;
else p=p->left;
}
if(p==NULL)return NULL;
}

TreeNode
* BSTree::searchParent(TreeNode * p)//查找出p的父亲节点的指针
{
TreeNode
* q=root;
TreeNode
*parent=root;
while(q!=NULL)
{
if(p->data==q->data) return parent;
else if(p->data <q->data){ parent=q; q=q->left;}
else { parent=q; q=q->right;}
}
if(q==NULL)return NULL;
}
//中序遍历
void BSTree::inorderTravel()
{
inorder (root);
}
//前序遍历
void BSTree::preorderTravel ()
{
preorder(root);
}

//后序遍历
void BSTree::postorderTravel ()
{
postorder(root);
}


void BSTree::inorder (TreeNode *p)
{
if(p!=NULL)
{
inorder(p
->left );
cout
<<p->data <<" ";
inorder(p
->right);
}
}
void BSTree::preorder(TreeNode *p)
{
if(p!=NULL)
{
cout
<<p->data <<" ";
preorder(p
->left );
preorder(p
->right );
}
}

void BSTree::postorder (TreeNode *p)
{
if(p!=NULL)
{
postorder(p
->left);
postorder(p
->right);
cout
<<p->data <<" ";
}
}

int main()
{

BSTree bt;

bt.Insert (
34);
bt.Insert (
45);
bt.Insert (
5);
bt.Insert (
634);


bt.inorderTravel ();

cout
<<"删除后"<<endl;
//bt.Delete (45);

bt.Delete (
34);
bt.inorderTravel ();


}