gpt4 book ai didi

c - 二叉搜索树的删除函数出错

转载 作者:行者123 更新时间:2023-11-30 19:23:27 25 4
gpt4 key购买 nike

#include<stdlib.h>
#include<stdio.h>


typedef struct treenode
{
int data;
struct treenode *left;
struct treenode *right;
}tnode;

tnode * insert(tnode *node,int data)
{
if(node==NULL)
{
node=(tnode *)malloc(sizeof(tnode));
node->data=data;
node->left=node->right=NULL;
return node;
}

if(data>node->data)
{
node->right=insert(node->right,data);
}
else if(data<node->data)
{
node->left=insert(node->left,data);
}

return node;
}


tnode * find(tnode *node,int data)

{
if(node==NULL)
return NULL;


if(node->data<data)
return find(node->right,data);

else if(node->data>data)
return find(node->left,data);

else
return node;
}

tnode* maximal(tnode *node)

{

if(node==NULL)
{
return NULL;
}

if(node->right)
return maximal(node->right);

else
return node;

}


tnode* minimal(tnode *node)

{
if(node==NULL)
{
return NULL;
}

if(node->left)
return minimal(node->left);

else
return node;
}

tnode * delete(tnode *node,int data)

{
tnode *temp;

if(node==NULL)
{
printf("Element Not Found");
}

else if(data < node->data)

{
node->left = delete(node->left, data);
}

else if(data > node->data)

{
node->right = delete(node->right, data);
}

else
{
/* Now We can delete this node and replace with either minimum element
in the right sub tree or maximum element in the left subtree */
if(node->right && node->left)
{
/* Here we will replace with minimum element in the right sub tree */
temp = minimal(node->right);
node -> data = temp->data;
/* As we replaced it with some other node, we have to delete that node */
node -> right = delete(node->right,temp->data);
}
else
{
/* If there is only one or zero children then we can directly
remove it from the tree and connect its parent to its child */
temp = node;
if(node->left == NULL)
node = node->right;
else if(node->right == NULL)
node = node->left;
free(temp); /* temp is longer required */
}
}
return node;

}





int main()

{
tnode *root=NULL,*temp;
//int maximum,minimum;
int op,n;
do

{
printf("\n1.insertion");
printf("\n2.deletion");
printf("\n3.preorder");
printf("\n4.inorder");
printf("\n5.postorder");
printf("\n6.minimal node");
printf("\n7.maximum node");
printf("\n8.find node");
printf("\n9.quit");
printf("\nenter your option:");
scanf("%d",&op);

switch(op)
{
case 1:
printf("enter the element to be inserted:");
scanf("%d",&n);
root=insert(root,n);
break;

case 2:
printf("enter the element to be deleted:");
scanf("%d",&n);
root=delete(root,n);
break;

case 6:
temp=minimal(root);
if(temp==NULL)
printf("no elements in the tree");
else
printf("the minimum element found is:%d",temp->data);
break;

case 7:
temp=maximal(root);
if(temp==NULL)
printf("no elements in the tree");
else
printf("the maximal element found is:%d",temp->data);
break;

case 8:
printf("enter the element to be found:");
scanf("%d",&n);
temp=find(root,n);
if(temp == NULL)
printf("element not found");
else
printf("element found");
break;

default :
exit(0);
}
}while(op<9);

return 0;

}

*当我编译时,很多错误向我袭来,所有错误都与删除函数以及如何释放指针有关(两个错误与释放指针有关)。我在widows平台上使用gcc来执行程序。 *

错误如下:g++.exe -x c++ -c C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C -o C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY ~2.o -Wall -f 允许C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:97:“删除”之前解析错误

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:120: `->' 之前有语法错误

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:122:`->' 之前有语法错误

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:133:在此范围内未声明“temp”

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:133:警告:ANSI C++ 禁止声明无类型的“free”

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:133:“int free”重新声明为不同类型的符号

...\lib\gcc-lib\i386-mingw32\2.95.2........\include\stdlib.h:271: 先前声明 `void free(void *)'

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:134:“}”之前解析错误

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:在函数 `int main()' 中:

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:174:警告:逗号表达式的左侧操作数无效

C:\Users\HARSHA\DOCUME~1\GCCPRO~1\ps2\BINARY~2.C:174:类型int'参数给出删除',预期指针

失败

最佳答案

g++ 编译 c++ 而不是 c 代码。 delete 是 C++ 中的运算符,因此不能用于函数名称。例如,尝试将函数删除的名称更改为“删除”(大写“D”)。

关于c - 二叉搜索树的删除函数出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11970370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com