SearchBST(T, key)
伪代码&&代码
void SearchBST(BiTree T, int key)//查找
{
if (T为空)return;
else if (T的值 == key)cout << "查找成功" << endl;
else if (T的值 > key)SearchBST(T->rchild, key);右子树查找
else if (T的值 > key)SearchBST(T->lchild, key);左子树查找
}
void SearchBST(BiTree T, int key)//查找
{
if (T==NULL)return;
else if (T->data == key)cout << "查找成功" << endl;
else if (T->data > key)SearchBST(T->rchild, key);
else if (T->data > key)SearchBST(T->lchild, key);
}
InsertBST(T, key)
伪代码&&代码
void InsertBST(BiTree T, int key)//插入
{
if (T为空) {
新建T
}
else{
if (T的值 == key)return;
else if (key<T的值)InsertBST(T->lchild, key);左子树插入
else if (key > T的值)InsertBST(T->rchild, key);右子树插入
}
}
void InsertBST(BiTree T, int key)//插入
{
if (T == NULL) {
T = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
else{
if (T->data == key)return;
else if (key<T->data)InsertBST(T->lchild, key);
else if (key > T->data)InsertBST(T->rchild, key);
}
}
CreateBST(T)
伪代码&&代码
void CreateBST(BiTree T)//创建
{
if(T为空){
新建T;
}
char str[12];
cin >> str;
cout<<"输入节点个数"<<endl;
cin>>n;
for (int i = 0; i<n ; i++) {//使用数组循环插入
InsertBST(T, str[i]);
}
}
void CreateBST(BiTree T)//创建
{
if(T==NULL){
T=new BiTNode;
T->lchild = new BiTNode;
T->rchild = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
char str[20];
cin >> str;
for (int i = 0; str[i] != '\0'; i++) {
InsertBST(T, str[i]);
}
}
DeleteBST(T, key)
伪代码&&代码
void DeleteBST(BiTree T, int key)//删除
{
if (T的值 == key) {
if (T->rchild 不为空) {
if (T->lchild 不为空) {
T->rchild->lchild = T->lchild;右孩子先指向左孩子,再释放T;
free(T);
}
else free(T);右孩子为空,无论左孩子是否存在,都释放T
}
else if (T->lchild 不为空)free(T);左孩子不存在,无论右孩子释放存在,都释放T
}
}
void DeleteBST(BiTree T, int key)//删除
{
if (T->data == key) {
if (T->rchild != NULL) {
if (T->lchild != NULL) {
T->rchild->lchild = T->lchild;
free(T);
}
else free(T);
}
else if (T->lchild != NULL)free(T);
}
}
完整代码
#include<iostream>
using namespace std;
typedef struct BiTNode {
int data;
struct BiTNode* lchild;
struct BiTNode* rchild;
}BiTNode, * BiTree;
void SearchBST(BiTree T, int key);
void InsertBST(BiTree T, int key);
void CreateBST(BiTree T);
void DeleteBST(BiTree T, int key);
void InorderTraverse(BiTree T);
void SearchBST(BiTree T, int key)//查找
{
if (!T)return;
else if (T->data == key)cout << "查找成功" << endl;
else if (T->data > key)SearchBST(T->rchild, key);
else if (T->data > key)SearchBST(T->lchild, key);
}
void InsertBST(BiTree T, int key)//插入
{
if (T == NULL) {
T = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
else{
if (T->data == key)return;
else if (key<T->data)InsertBST(T->lchild, key);
else if (key > T->data)InsertBST(T->rchild, key);
}
}
void CreateBST(BiTree T)//创建
{
if(T==NULL){
T=new BiTNode;
T->lchild = new BiTNode;
T->rchild = new BiTNode;
T->lchild = NULL;
T->rchild = NULL;
}
char str[20];
cin >> str;
cout<<"输入节点个数"<<endl;
cin>>n;
for (int i = 0; i<n ; i++) {//使用数组循环插入
InsertBST(T, str[i]);
}
}
void DeleteBST(BiTree T, int key)//删除
{
if (T->data == key) {
if (T->rchild != NULL) {
if (T->lchild != NULL) {
T->rchild->lchild = T->lchild;
free(T);
}
else free(T);
}
else if (T->lchild != NULL)free(T);
}
}
void InorderTraverse(BiTree T)//中序遍历
{
if (T)
{
InorderTraverse(T->lchild);
printf("%d ", T->data);
InorderTraverse(T->rchild);
}
}
int main()
{
int key;
BiTree T;
T = new BiTNode;
CreateBST(T);
cout << "中序输出" << endl;
InorderTraverse(T);
cout << "请输入查找数据" << endl;
cin >> key;
SearchBST(T, key);
InsertBST(T, key);
return 0;
}
运行结果
![](https://img2020.cnblogs.com/blog/1826313/202004/1826313-20200419222344630-1815164930.png)