c++ - Incompatible Pointer type while making binary search tree -
here code. trying insert in binary search tree , address pointers when getting bigger data node going left , vice versa
getting error: c:\users\huf\documents\tree.c|26|error: request member `right' in not structure or union|
#include <stdio.h> struct node { struct node*left; int data; struct node *right; }; void maketree(struct node **root1, int data1) { if((*root1) == null) { (*root1) = (struct node *)malloc(sizeof(struct node)); (*root1)->data = data1; (*root1)->left = null; (*root1)->right = null; //printf("%d %d",(*root1)->data,data1); } else if(data1 > ((*root1)->data)) { printf("%d ", (*root1)->data); maketree((*root1)->right,data1); } else if(data1 < (*root1)->data) { maketree((*root1)->left,data1); printf("%d ", (*root1)->data); } } int main() { struct node * root = null; //int data; //data = 5; maketree(&root,12); maketree(&root,5); maketree(&root,9); maketree(&root,8); maketree(&root,16); maketree(&root,10); return 0; }
why getting incompatible pointer type.
ths problem in line maketree((*root1)->right,data1);
. there must struct node **root1
(see parameters of function maketree()
), actual parameter struct node *
.
try put line: maketree(&(*root1)->right,data1);
(and same 'left' few lines lower).
Comments
Post a Comment