#include<stdio.h>
#include<conio.h>
#include<stdio.h>
void maketree(struct node**);
void inorder(struct node**);

struct node{
struct node *left;
struct node *right;
int data;
}

void main()
{
 struct node *root;
 root=NULL;
 printf("Enter root");
 maketree(&root);
 preorder(&root);
 getch();
 }

void maketree(struct node **root)
{
	int num;
	struct node *temp;
	if(*root==NULL){
		//printf("Enter number");
		scanf("%d",&num);
		temp=(struct node*)malloc(sizeof(struct node));
		temp->data=num;
		temp->left=temp->right=NULL;
	}
	if((*root)->data!=0){
		printf("enter left of %d ",(*root)->data);
		maketree(&(*root)->left);
		printf("Enter right of %d ",(*root)->data);
		maketree(&(*root)->right);
	}

}

void ineorder(struct node *root)
{
	if(root!=NULL)
	inorder(root->left);
	printf("%d->",root->data);
	inorder(root->right);
}

Recommended Answers

All 4 Replies

If you want help, please post a specific question. What about this code is troubling you?

in this program i have to enter numbers in the tree
maketree() function is a recursive function
but it does not work properly.

in this program i have to enter numbers in the tree
maketree() function is a recursive function
but it does not work properly.

in this program i have to enter numbers in the tree
maketree() function is a recursive function
but it does not work properly.

Again, more detail would be helpful. What does "not work properly" mean to you here? What is it not doing that you expect it to? What is it doing right, if anything?

You'll get better results in this forum (and with software engineering in general) if you start asking yourself these questions--and answering them.

Just looking at the code, a few things stand out:

void maketree(struct node **root)

Why the double indirection? I don't think it's necessary, and it only confuses your code.

if(*root==NULL){
  //printf("Enter number");
  scanf("%d",&num);
  temp=(struct node*)malloc(sizeof(struct node));
  temp->data=num;
  temp->left=temp->right=NULL;
}

You create a new node here, but never assign anything to root , so it will always be NULL .

temp=(struct node*)malloc(sizeof(struct node));

You're allocating storage for all these nodes, but the memory is never freed. You should do that before the program exits. Here's an introduction to common malloc -related errors.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.