/*Implementation of Binary Tree*/
#include <stdio.h>
#include <ncurses.h>
#include <malloc.h>
#include <stdlib.h>
struct bin_tree
{
int INFO;
struct node *LEFT, *RIGHT;
};
typedef struct bin_tree node;
node *insert(node *,int); /*Function prototype for insering a new node*/
void display(node *); /*Function prototype fpr displaying the tree nodes*/
int count=1; /*counter for ascertaining left or right position for the new node*/
int main()
{
struct node *root = NULL;
int element, choice;
clear();
/*Displaying a menu of choices*/
while(1)
{
clear();
printf("\n Select an option\n");
printf("\n1 - Insert");
printf("\n2 - Display");
printf("\n3 - Exit");
printf("\n\nEnter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\n\n Enter the node value: ");
scanf("%d", &element);
root = insert(root, element); /*calling the insert function for inserting a new element into the tree*/
getch();
break;
}
case 2:
{
display(root); /*calling the display function for printing the node values*/
getch();
break;
}
case 3:
{
exit(1);
break;
}
default:
{
printf("\n incorrect choice.Please try again.");
getch();
break;
}
}
}
}
node *insert(node *r, int n)
{
if(r==NULL)
{
r=(node*)malloc(sizeof(node));
r->LEFT = r->RIGHT = NULL;
r->INFO = n;
count = count+1;
}
else
{
if(count%2==0)
r->LEFT = insert(r->LEFT,n);
else
r->RIGHT = insert(r->RIGHT,n);
}
return(r);
}
void display(node *r)
{
if(r->LEFT!=NULL)
display(r->LEFT);
printf("%d\n",r->INFO);
if(r->RIGHT!=NULL)
display(r->RIGHT);
}
gcc -Wall -c "BinaryTree.c" (in directory: /home/dere/IGNOUPROGRAMS/C)
BinaryTree.c: In function ‘main’:
BinaryTree.c:46:5: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:16:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:46:10: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c:53:5: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:17:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c: In function ‘insert’:
BinaryTree.c:86:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:74:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:86:11: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c:88:3: warning: passing argument 1 of ‘insert’ from incompatible pointer type [enabled by default]
BinaryTree.c:74:7: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:88:12: warning: assignment from incompatible pointer type [enabled by default]
BinaryTree.c: In function ‘display’:
BinaryTree.c:96:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:93:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
BinaryTree.c:99:2: warning: passing argument 1 of ‘display’ from incompatible pointer type [enabled by default]
BinaryTree.c:93:6: note: expected ‘struct node *’ but argument is of type ‘struct node *’
Compilation finished successfully.
感谢您对此错误提供任何帮助。
答案 0 :(得分:1)
在root
函数中将node *
声明为main()
本身的类型。无需声明为struct node *
。
node *root = NULL;
root
typedef
为struct bin_tree
。
警告是由于声明不匹配造成的。由于insert()
函数定义为node *insert(node *,int);
,但根变量定义为struct node *
,并将其作为参数传递给insert()
函数。
答案 1 :(得分:0)
似乎你的代码错了,可能会尝试这样。你需要传递root的地址来插入如下的函数指针:
root = insert(&root, element);
node *insert(node **r, int n)
{
struct node* temp = NULL;
struct node *q = NULL;
q = *r;
if(q==NULL)
{
temp=(node*)malloc(sizeof(node));
temp->LEFT = temp->RIGHT = NULL;
temp->INFO = n;
count = count+1;
}
else
{
if(count%2==0)
temp->LEFT = insert(temp->LEFT,n);
else
temp->RIGHT = insert(temp->RIGHT,n);
}
return(temp);
}