c语言二叉排序树的创建与查找,C语言实现二叉查找树的插入和删除操作问题求教...

使用C语言实现二叉查找树的插入和删除操作,但在

return searchBST( T->rchild, val, f, p);出错。这里应该使用了双指针,求教应该怎么改才正确。

/*

+----------------------------------------------------------------------+

| 这里实现二叉排序树的插入和删除操作

+----------------------------------------------------------------------+

| Author: Sean |

+----------------------------------------------------------------------+

*/

#include

#include

#define OK 1

#define TRUE 1

#define ERROR -1

#define FALSE 0

typedef int ElemType;

typedef struct Node

{

ElemType data;

struct Node *lchild,*rchild;

}NODE,*PNODE;

//prompt error info and exit.

void errorInfo(char str[])

{

printf("%s\n",str);

exit(-1);

}

//prompt error if there is an error in locating memory

void mallocErr(PNODE p)

{

if(NULL==p)

errorInfo("Error in locating memory.");

}

//search binary sort tree

int searchBST(PNODE T,int val,PNODE *f,PNODE *p)

{

if(!T)

{

*p = *f;

return FALSE;

}

else if(val == T->data)

{

*p = T;

return TRUE;

}else if(val < T->data)

{

printf("run this code 52\n");

*f = T;

return searchBST(T->lchild,val,f,p);

}else

{

printf("run this code 56\n");

*f = T;

printf("run this code 58\n");

printf("T->data : %d \n",T->data);

//运行到此处出错

return searchBST( T->rchild, val, f, p);

}

}

int insertBST(PNODE *T,int val)

{

PNODE p = NULL, s = NULL,f = NULL;

int res = 0;

res = searchBST(*T,val,&f,&p);

if(!res) //val does not exist in the array.

{

s = (PNODE)malloc(sizeof(NODE));

mallocErr(s);

s->data = val;

if(!p) //p is null

{

printf("run this code 75\n");

printf("Create Tree with val:%d\n",val);

*T = s;

}else if(val < p->data)

{

printf("run this code 79\n");

printf("Insert Key To Tree(left):%d\n",val);

p->lchild = s;

}else

{

printf("run this code 84\n");

printf("Insert Key To Tree(right):%d\n",val);

p->rchild = s;

}

return TRUE;

}else //val already exists in the array.

{

return FALSE;

}

}

int main()

{

PNODE T = NULL, p = NULL;

insertBST(&T,100);

insertBST(&T,199);

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您解答关于C语言实现二叉排序树的相关问题。 首先,我们需要了解什么是二叉排序树二叉排序树是一种特殊的二叉树,它要求左子树的所有节点都小于根节点,右子树的所有节点都大于根节点。这样,我们就可以利用二叉排序树实现快速的查找插入删除操作。 接下来,我将分别介绍如何实现二叉排序树查找插入、建立、删除和打印二叉排序树形。 1. 查找 查找操作就是在二叉排序树查找某个关键字是否存在。查找操作可以使用递归实现。 ```c // 查找操作 bool searchBST(BiTree T, int key) { if (!T) { return false; } if (T->data == key) { return true; } else if (T->data > key) { return searchBST(T->lchild, key); } else { return searchBST(T->rchild, key); } } ``` 2. 插入 插入操作就是向二叉排序树插入一个新节点。插入操作也可以使用递归实现。 ```c // 插入操作 bool insertBST(BiTree &T, int key) { if (!T) { T = new BiTNode; T->data = key; T->lchild = T->rchild = nullptr; return true; } if (T->data == key) { return false; } else if (T->data > key) { return insertBST(T->lchild, key); } else { return insertBST(T->rchild, key); } } ``` 3. 建立 建立操作就是通过一组关键字建立一个二叉排序树。建立操作可以使用循环实现。 ```c // 建立操作 void createBST(BiTree &T, int *keys, int n) { T = nullptr; for (int i = 0; i < n; i++) { insertBST(T, keys[i]); } } ``` 4. 删除 删除操作就是从二叉排序树删除一个节点。删除操作可以分为三种情况: - 被删除节点没有左右子树,直接删除; - 被删除节点只有一个子树,将该子树替换到被删除节点的位置; - 被删除节点有左右子树,找到被删除节点的前驱或后继节点替换到被删除节点的位置。 ```c // 删除操作 bool deleteBST(BiTree &T, int key) { if (!T) { return false; } if (T->data == key) { if (!T->lchild && !T->rchild) { delete T; T = nullptr; } else if (!T->lchild || !T->rchild) { BiTree temp = T; T = T->lchild ? T->lchild : T->rchild; delete temp; } else { BiTree temp = T->lchild; while (temp->rchild) { temp = temp->rchild; } T->data = temp->data; deleteBST(T->lchild, temp->data); } return true; } else if (T->data > key) { return deleteBST(T->lchild, key); } else { return deleteBST(T->rchild, key); } } ``` 5. 打印 打印操作就是按照中序遍历的顺序打印二叉排序树中的所有节点。 ```c // 打印操作 void printBST(BiTree T) { if (T) { printBST(T->lchild); cout << T->data << " "; printBST(T->rchild); } } ``` 至此,我们已经完成了二叉排序树查找插入、建立、删除和打印操作实现。完整代码如下:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值