您的位置:首页 > 其它

二叉排序树的创建

2011-11-24 21:22 162 查看
下面介绍一下二叉排序树的创建

其实二叉排序树就是二叉树的一种,只是他把小的数放在左子树上,大的数放在右子树上。

所以他的创建与插入函数都像二叉树一样的,只是删除函数比较麻烦。

下面是在下看了书之后,改了一点的代码,如有错误,请见谅!

/////////////////////////////////////////// head.h /////////////////////////////////////////////////////////////

#include <iostream>

using namespace std;

typedef int KeyType;

struct DNode

{

KeyType key;

};

typedef DNode DataType;

//其实先要建立一颗普通的二叉树

struct BNode

{

DataType data;

BNode *lchild, *rchild;

};

typedef BNode BiTreeNode;

typedef BNode* BiTree;

//二叉树的中序遍历

void InOrderTraverse(BiTree T)

{

if(T)

{

InOrderTraverse(T->lchild);

cout<< T->data.key <<" ";

InOrderTraverse(T->rchild);

}

}

//二叉排序树的查找算法描述

BiTree BSearch(BiTree T, DataType x)

{

BiTreeNode *p;

if(T!=NULL)

{

p=T;

while(p!=NULL)

{

if( p->data.key == x.key)

{

return p;

}

else if(x.key < p->data.key ) //下面是来一个递归

{

p=p->lchild;

}

else

{

p=p->rchild;

}

}

}

return NULL; ///如果树里没有这个数的话就返回NULL

}

//二叉树的插入(大的插右边,小的插左边)

int BSTInsert(BiTree *T, DataType x)

{

BiTreeNode *p=NULL, *cur=NULL, *parent=NULL;

cur=*T;

while( cur!=NULL )

{

if( cur->data .key== x.key )

{

return 0; ////这个数已经存在,不用插入

}

parent = cur ;

if( x.key < cur->data.key )

{

cur=cur->lchild; ///小于的就放在左边

}

else

{

cur=cur->rchild;

}

}

p=(BiTreeNode *)malloc(sizeof(BiTreeNode)); ///生成一个新的结点

if(!p)

{

exit(-1);

}

p->data=x; ///开始插入

p->lchild=NULL;

p->rchild=NULL;

if(!parent) //////////如果二叉树是空的话,就生成根结点

{

*T=p;

}

else if( x.key < parent->data.key ) /// 小的就往左边插

{

parent ->lchild = p;

}

else /////////////////////////////////////// 大的就往右边插

{

parent->rchild = p;

}

return 1;

}

///二叉树的删除

void DeleteNode(BiTree *s)

{

BiTree q=NULL ,x=NULL, y=NULL;

///左右只有其中一颗子树

if( !(*s)->rchild )

{

q=*s;

*s=(*s)->lchild;

free(q);

}

else if( !(*s)->lchild )

{

q=*s;

*s=(*s)->rchild;

free(q);

}

else ///左右子树都存在

{

x=*s;

y=(*s)->lchild;

while(y->rchild) /// x是y的双亲结点 y是s的前驱结点

{

x=y;

y=y->rchild;

}

(*s)->data=y->data; /// 结点s被y取缔

if(x!=*s) /////////////////如果结点s的左孩子没有右子树,使y的左子树成为x的右子树

{

x->rchild=y->lchild;

}

else /////////////////如果s的左孩子没有存在右子树,使y的左子树成为x的左子树

{

x->lchild=y->lchild;

}

free(y);

}

}

int BSTDelete(BiTree *T, DataType x )

{

if( !*T )

{

return 0;

}

else

{

if( x.key == (*T)->data.key ) /// key值相等就直接删除

{

DeleteNode(T);

}

else if( (*T)->data.key > x.key ) /// 小于的就往左找

{

BSTDelete( &(*T)->lchild , x );

}

else

{

BSTDelete( &(*T)->rchild , x ); ///大的往右找

}

return 1;

}

}

///////////////////////////////////// main.cpp //////////////////////////////////////////////////

#include "head.h"

void InOrderTraverse(BiTree T);

int BSTInsert(BiTree *T, DataType x);

BiTree BSearch(BiTree T, DataType x);

void DeleteNode(BiTree *s);

int BSTDelete(BiTree *T, DataType x );

void main()

{

BiTree T=NULL , p;

DataType table[]={37,32,35,62,82,95,73,12,5};

int n=sizeof(table) / sizeof(table[0]) ;

DataType x={73}, s={32};

for(int i=0; i<n ; i++)

{

BSTInsert(&T,table[i]);

}

cout<<"中序遍历:"<<endl;

InOrderTraverse(T);

cout<<endl;

p=BSearch(T,x);

if(p!=NULL)

{

cout<<"关键字 "<<p->data.key <<" 是存在的!"<<endl;

}

else

{

cout<<"关键字 "<< x.key <<" 不存在的!"<<endl;

}

cout<<"删除 "<<s.key<<" 之后的二叉树所得的数列是 :"<<endl;

BSTDelete( &T, s);

InOrderTraverse(T);

cout<<endl;

}

后记:

其实关于删除的那一部分在下也说得不太清楚,最好就是看看别的资料把!!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: