#include<stdio.h> #include<stdlib.h> typedef struct BiTNode { int key; struct BiTNode *lchild, *rchild; } BiTNode, *BiTree; int SearchBST(BiTree T, int key, BiTree f, BiTree &p) { if (!T) { p = f; return 0; } else if (key == T->key) { p = T; return 1; } else if (key < T->key) SearchBST(T->lchild, key, T, p); else SearchBST(T->rchild, key, T, p); } int InsertBST(BiTree &T, int key) { if (!T) { T = (BiTree)malloc(sizeof(BiTNode)); T->key = key; T->lchild = (T)->rchild = NULL; } if (key == T->key) return 0; if (key > T->key) InsertBST(T->rchild, key); else InsertBST(T->lchild, key); } void InorderTraverse(BiTree T) { if (T){ InorderTraverse(T->lchild); printf("%d ", T->key); InorderTraverse(T->rchild); } } void Delete(BiTree &p) { BiTree q, s; if (!p->lchild && !p->rchild) p = NULL; else if (!p->lchild) { q = p; p = p->rchild; free(q); } else if (!p->rchild) { q = p; p = p->lchild; free(q); } else{ q = p; s = p->lchild; while (s->rchild) { q = s; s = s->rchild; } p->key = s->key; if (q != p) q->rchild = s->lchild; else q->lchild = s->lchild; free(s); } } int DeleteBST(BiTree &T, int key) { if (!T) return 0; else{ if (key == T->key) Delete(T); else if (key < T->key) DeleteBST(T->lchild, key); else DeleteBST(T->rchild, key); } } int main() { int e, n; BiTree T = NULL, f = 0, p; printf("输入长度:"); scanf("%d", &n); printf("输入元素:"); while (n--){ scanf("%d", &e); InsertBST(T, e); } printf("中序遍历:"); InorderTraverse(T); printf("\n"); while (1) { printf("输入要查找元素:"); scanf("%d", &e); if (SearchBST(T, e, f, p)) printf("找到了\n"); else printf("没找到\n"); printf("输入要插入元素:"); scanf("%d", &e); InsertBST(T, e); printf("中序遍历:"); InorderTraverse(T); printf("\n"); printf("输入要删除元素:"); scanf("%d", &e); DeleteBST(T, e); printf("中序遍历:"); InorderTraverse(T); printf("\n"); } }请帮我注视本段代码
时间: 2024-03-10 07:44:56 浏览: 105
好的,以下是本段代码的注释:
```c
#include<stdio.h>
#include<stdlib.h>
//定义二叉搜索树的节点结构体
typedef struct BiTNode
{
int key; //节点值
struct BiTNode *lchild, *rchild; //左右子节点
} BiTNode, *BiTree;
//在二叉搜索树T中查找值为key的节点,f为T的父节点,p为查找结果
int SearchBST(BiTree T, int key, BiTree f, BiTree &p)
{
if (!T)
{
p = f;
return 0; //未找到
}
else if (key == T->key)
{
p = T;
return 1; //找到
}
else if (key < T->key)
SearchBST(T->lchild, key, T, p);
else
SearchBST(T->rchild, key, T, p);
}
//在二叉搜索树T中插入值为key的节点
int InsertBST(BiTree &T, int key)
{
if (!T)
{
T = (BiTree)malloc(sizeof(BiTNode)); //申请新节点
T->key = key;
T->lchild = (T)->rchild = NULL; //左右子节点置为空
}
if (key == T->key)
return 0; //已有相同节点
if (key > T->key)
InsertBST(T->rchild, key); //插入右子树
else
InsertBST(T->lchild, key); //插入左子树
}
//中序遍历二叉搜索树
void InorderTraverse(BiTree T)
{
if (T){
InorderTraverse(T->lchild);
printf("%d ", T->key);
InorderTraverse(T->rchild);
}
}
//删除指定节点
void Delete(BiTree &p)
{
BiTree q, s;
if (!p->lchild && !p->rchild)
p = NULL;
else if (!p->lchild)
{
q = p;
p = p->rchild;
free(q);
}
else if (!p->rchild)
{
q = p;
p = p->lchild;
free(q);
}
else
{
q = p;
s = p->lchild;
while (s->rchild)
{
q = s;
s = s->rchild;
}
p->key = s->key;
if (q != p)
q->rchild = s->lchild;
else
q->lchild = s->lchild;
free(s);
}
}
//在二叉搜索树T中删除值为key的节点
int DeleteBST(BiTree &T, int key)
{
if (!T)
return 0; //未找到
else
{
if (key == T->key)
Delete(T); //找到并删除
else if (key < T->key)
DeleteBST(T->lchild, key); //在左子树中递归查找和删除
else
DeleteBST(T->rchild, key); //在右子树中递归查找和删除
}
}
//主函数
int main()
{
int e, n;
BiTree T = NULL, f = 0, p; //T为二叉搜索树,f为T的父节点,p为查找结果
printf("输入长度:");
scanf("%d", &n);
printf("输入元素:");
while (n--)
{
scanf("%d", &e);
InsertBST(T, e); //插入元素建树
}
printf("中序遍历:");
InorderTraverse(T); //中序遍历
printf("\n");
while (1)
{
printf("输入要查找元素:");
scanf("%d", &e);
if (SearchBST(T, e, f, p))
printf("找到了\n");
else
printf("没找到\n");
printf("输入要插入元素:");
scanf("%d", &e);
InsertBST(T, e); //插入元素
printf("中序遍历:");
InorderTraverse(T); //中序遍历
printf("\n");
printf("输入要删除元素:");
scanf("%d", &e);
DeleteBST(T, e); //删除元素
printf("中序遍历:");
InorderTraverse(T); //中序遍历
printf("\n");
}
}
```
阅读全文