#include <iostream.h>
#include <cstring>
typedef int KeyType;
#define NUM 11
class BinStree;
class BinSTreeNode
{
public:
KeyType key;
BinSTreeNode *lchild;
BinSTreeNode *rchild;
BinSTreeNode()
{
lchild = NULL;
rchild = NULL;
}
};
class BinSTree
{
public:
BinSTreeNode *root;
BinSTree()
{
root = NULL;
}
~BinSTree()
{
//delete root;
}
BinSTreeNode *BSTreeSearch( BinSTreeNode *bt, KeyType k, BinSTreeNode *&p );
void BSTreeInsert( BinSTreeNode *&bt, KeyType k );
int BSTreeDelete( BinSTreeNode *&bt, KeyType k );
void BSTreePreOrder(BinSTreeNode *bt);
bool IsEmpty()
{
return root == NULL;
}
};
/**
* 二叉树排序查找算法
* 在根指针为bt的二叉排序树中查找元素k的节点,若查找成功,则返回指向该节点的指针
* 参数p指向查找到的结点,否则返回空指针,参数p指向k应插入的父结点
*/
BinSTreeNode* BinSTree::BSTreeSearch( BinSTreeNode *bt, KeyType k, BinSTreeNode *&p )
{
BinSTreeNode *q = NULL;
q = bt;
while(q)
{
p = q;
if( q->key == k )
{
return(p);
}
if( q->key > k )
q = q->lchild;
else
q = q->rchild;
}
return NULL;
}
/**
* 二叉排序树的插入节点算法
* bt指向二叉排序树的根结点,插入元素k的结点
*/
void BinSTree::BSTreeInsert( BinSTreeNode *&bt, KeyType k )
{
BinSTreeNode *p = NULL, *q;
q = bt;
if( BSTreeSearch( q, k, p ) == NULL )
{
BinSTreeNode *r = new BinSTreeNode;
r->key = k;
r->lchild = r->rchild = NULL;
if( q == NULL )//这里的q代表的bt,不是search函数里面的q
{
bt = r; //被插入节点做为树的根节点
}
if( p && k < p->key )
p->lchild = r;
else if( p )
p->rchild = r;
}
}
/**
* 中序遍历
*/
void BinSTree::BSTreePreOrder(BinSTreeNode *bt)
{
if(bt != NULL)
{
cout << bt->key << " ";
BSTreePreOrder(bt->lchild);
BSTreePreOrder(bt->rchild);
}
}
/**
* 二叉排序树的删除结点算法
* 在二叉排序树中删除元素为k的结点,*bt指向二叉排序树的根节点
* 删除成功返回1,不成功返回0.
*/
int BinSTree::BSTreeDelete( BinSTreeNode *&bt, KeyType k )
{
BinSTreeNode *f, *p, *q, *s;
p = bt;
f = NULL;
//查找关键字为k的结点,同时将此结点的双亲找出来
while( p && p->key != k )
{
f = p;
if( p->key > k )
p = p->lchild;
else
p = p->rchild;
}
if( p == NULL ) //找不到待删除的结点时返回
return 0;
if( p->lchild == NULL ) //待删除结点的左子树为空
{
if( f == NULL ) //待删除结点为根节点
bt = p->rchild;
else if( f->lchild == p ) //待删结点是其双亲结点的左节点
f->lchild = p->rchild;
else
f->rchild = p->rchild; //待删结点是其双亲结点的右节点
delete p;
}
else //待删除结点有左子树
{
q = p;
s = p->lchild;
while( s->rchild ) //在待删除结点的左子树中查找最右下结点
{
q = s;
s = s->rchild;
}
if( q == p )
q->lchild = s->lchild;
else
q->rchild = s->lchild;
p->key = s->key;
delete s;
}
return 1;
}
int main( void )
{
int a[NUM] = { 34, 18, 76, 13, 52, 82, 16, 67, 58, 73, 72 };
int i;
BinSTree bst;
BinSTreeNode *pBt = NULL, *p = NULL, *pT = NULL;
for( i = 0; i < NUM; i++ )
{
bst.BSTreeInsert( pBt, a[i] ); //创建二叉排序树
}
pT = bst.BSTreeSearch( pBt, 52, p ); //搜索排序二叉树
bst.BSTreePreOrder(pBt);
cout << endl;
bst.BSTreeDelete( pBt, 13 ); //删除无左孩子的情况
bst.BSTreePreOrder(pBt);
cout << endl;
bst.BSTreeDelete( pBt, 76 ); //删除有左孩子的情况
bst.BSTreePreOrder(pBt);
cout << endl;
return 0;
}
注意:? *& bt 和 *bt 和&bt的不同。
二叉排序树操作实现
本文介绍了一种二叉排序树的基本操作实现方法,包括插入、查找和删除节点等核心功能,并通过C++代码示例展示了这些算法的具体实现过程。
644

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



