• C

binary tree insert C code

where is the error?
#include <stdio.h>
#include<stdlib.h>
 
typedef struct bnode{
        int key;
        struct bnode* left;
        struct bnode* right;
        }BNODE;
 
void printKeysReverse(BNODE* current);
void inorder(BNODE* current);
void insert(BNODE **root,int key);
int main(void){
    BNODE* root=NULL;
    
    insert(&root,27);
    insert(&root,59);
    insert(&root,21);
    insert(&root,38);
    insert(&root,54);
    insert(&root,63);
    insert(&root,8);
    insert(&root,70);
    insert(&root,15);
    
}
void insert(BNODE **root, int val){
  BNODE *newnode;
  
  newnode=(BNODE*)malloc(sizeof(BNODE));        
  newnode->right=NULL;
  newnode->left=NULL;
  
  if ((*root)==NULL){
       *root=newnode;
       (*root)->key=val;              
       return;
       }
  if (val<(*root)->key) insert((&root)->left,val);
  else insert((&root)->right,val);     
}//end 
 
void inorder(BNODE *root){
     
     if (root==NULL) return ;
     inorder(root->left);
     printf("%d ",root->key);
     inorder(root->right);
}//end inorder

Open in new window

dimi67Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
What's the symtom?
0
evilrixSenior Software Engineer (Avast)Commented:
It now builds. You had incorrect dereferenceing.
#include <stdio.h>
#include<stdlib.h>
 
typedef struct bnode{
	int key;
	struct bnode* left;
	struct bnode* right;
}BNODE;
 
void printKeysReverse(BNODE* current);
void inorder(BNODE* current);
void insert(BNODE **root,int key);
int main(void){
	BNODE* root=NULL;
 
	insert(&root,27);
	insert(&root,59);
	insert(&root,21);
	insert(&root,38);
	insert(&root,54);
	insert(&root,63);
	insert(&root,8);
	insert(&root,70);
	insert(&root,15);
 
}
void insert(BNODE **root, int val){
	BNODE *newnode;
 
	newnode=(BNODE*)malloc(sizeof(BNODE));        
	newnode->right=NULL;
	newnode->left=NULL;
 
	if ((*root)==NULL){
		*root=newnode;
		(*root)->key=val;              
		return;
	}
	if (val<(*root)->key) insert(&(*root)->left,val);
	else insert(&(*root)->right,val);     
}//end 
 
void inorder(BNODE *root){
 
	if (root==NULL) return ;
	inorder(root->left);
	printf("%d ",root->key);
	inorder(root->right);
}//end inorder

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
evilrixSenior Software Engineer (Avast)Commented:
The problems specifically were...

I hope this helps.

-Rx.
Before:  if (val<(*root)->key) insert((&root)->left,val);
After: if (val<(*root)->key) insert(&(*root)->left,val);
 
Before:  else insert((&root)->right,val);     
After: else insert(&(*root)->right,val);     

Open in new window

0
dimi67Author Commented:
thanks! :-)
0
kollidineshCommented:
if condition which you have return is wrong

it should be

if (val<(*root)->key))
    insert((&root)->left,val);
else
  insert(&(*root)->left,val);
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.