#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 -fpermissive C:\ Users \ HARSHA \ DOCUME~1 \ GCCPRO~1 \ ps2 \ BINARY~2.C:97:在'删除'之前解析错误
C:\ Users \ HARSHA \ DOCUME~1 \ GCCPRO~1 \ ps2 \ BINARY~2.C:120:` - &gt;'之前的语法错误
C:\ Users \ HARSHA \ DOCUME~1 \ GCCPRO~1 \ ps2 \ BINARY~2.C:122:` - &gt;'之前的语法错误
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 ++禁止声明“免费”,没有类型
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:type int' argument given to
delete',预期指针
失败
答案 0 :(得分:1)
g ++编译c ++而不是c代码。 delete是c ++中的运算符,因此不能用于函数名。例如,尝试将函数delete的名称更改为Delete(使用大写“D”)。