二叉排序树的创建
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;
}
后记:
其实关于删除的那一部分在下也说得不太清楚,最好就是看看别的资料把!!
其实二叉排序树就是二叉树的一种,只是他把小的数放在左子树上,大的数放在右子树上。
所以他的创建与插入函数都像二叉树一样的,只是删除函数比较麻烦。
下面是在下看了书之后,改了一点的代码,如有错误,请见谅!
/////////////////////////////////////////// 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;
}
后记:
其实关于删除的那一部分在下也说得不太清楚,最好就是看看别的资料把!!
相关文章推荐
- 二叉排序树的创建,插入和 删除操作
- 创建二叉排序树
- 二叉排序树的创建,插入,遍历
- 创建二叉排序树,先序遍历,中序遍历,判断是否存在关键点
- 二叉排序树的创建、查找
- 算法--查找--二叉排序树创建、查找
- 二叉排序树的创建删除中序输出&&平衡树
- 二叉排序树(BST)创建,删除,查找操作
- 数据结构 二叉排序树的创建,查找,插入,删除
- 二叉排序树的创建,查找,插入,删除
- 二叉排序树的创建、插入和遍历(代码)
- 数据结构:由有序数列创建一棵高度最小的二叉排序树与判断一 个序列是否为该二叉排序树中的一个合法查找序列
- 二叉树与二叉排序树的创建于遍历
- 二叉排序树(创建,查找,删除)
- 二叉排序树创建、查找、删除
- C++二叉排序树的创建和插入
- 数据结构上机实验-二叉排序树的创建
- 二叉排序树 创建和 遍历
- 二叉排序树的创建与查找
- 数据结构 二叉排序树的创建、查找、删除