随机产生一组关键字,利用二叉排序树的插入算法建立二叉排序树,然后删除某一指定关键字元素。
代码实现:
#include <iostream>
#include <cstdio>
#include <ctime>
#include <windows.h>
using namespace std;
typedef int ElemType;
typedef struct BSTNode
{
ElemType data;
struct BSTNode *lchild,*rchild;
} BSTNode,*BiTree;
bool Find(BiTree BST,ElemType &item) ///查找函数
{
if(BST==NULL)
{
return 0;
}
else
{
if(BST->data==item)
{
item=BST->data;
return 1;
}
else if(item<BST->data)
return Find(BST->lchild,item);
else
return Find(BST->rchild,item);
}
}
void Insert(BiTree &BST,const ElemType &item) ///插入
{
if(BST==NULL)
{
BSTNode *p=new BSTNode;
p->data=item;
p->lchild=p->rchild=NULL;
BST&