answersLogoWhite

0


Best Answer

struct node{

int data;

struct node *left, *right;

};

typedef struct node node;

non recursive method :

main()

{

node * root = NULL , *new = NULL *temp1 =NULL , * temp2 = NULL;

int num =1;

printf(" Enter the elements of the tree( enter 0 to exit)\n");

while(1)

{

scanf("%d", &num);

if(num==0)

break;

new = malloc(sizeof(node));

new->left = new->right = NULL;

new->data = num;

if( root NULL)

root->left = new;

else

insert( new,root->left);

}

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

13y ago

#include <stdio.h>

void main()

{

int digi,num,i,sum=0;

printf("Enter the number of digits:");

scanf("%d",&num);

while(i>0);

{

for(i=0;i<10000;i++)

{

digi=num;

i=digi%10;

sum=sum+i;

num=digi/10;

}

printf("The sum of digits entered is %d",sum);

}

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

#include

#include

#include

struct NODE

{

int data;

struct NODE *leftchild;

struct NODE *rightchild;

};

struct NODE *Binary_Tree(char *,int,int);

void display(struct NODE *,int);

void Pre_order(struct NODE *);

void In_order(struct NODE *);

void Post_order(struct NODE *);

/* Function to create a binary tree */

struct NODE * Binary_Tree(char *List,int Lower,int Upper)

{

struct NODE *Node;

int Mid=(Lower + Upper)/2;

Node=(struct NODE*)malloc(sizeof(struct NODE));

Node->data=List[Mid];

if(Lower>=Upper)

{

Node->leftchild=NULL;

Node->rightchild=NULL;

return(Node);

}

if(Lower <=Mid-1)

Node->leftchild=Binary_Tree(List,Lower,Mid-1);

else

Node->leftchild=NULL;

if(Mid+1<=Upper)

Node->rightchild=Binary_Tree(List,Mid+1,Upper);

else

Node->rightchild=NULL;

return(Node);

}

/* Output function */

void display(struct NODE *T,int Level)

{

int i;

if(T)

{

display(T->rightchild,Level+1);

printf("\n");

for(i=0;i

printf(" ");

printf(" %d",T->data);

display(T->leftchild,Level+1);

}

}

/* Pre-order traversal */

void Pre_order(struct NODE *Node)

{

if(Node)

{

printf(" %d",Node->data);

Pre_order(Node->leftchild);

Pre_order(Node->rightchild);

}

}

/* In-order traversal */

void In_order(struct NODE *Node)

{

if(Node)

{

In_order(Node->leftchild);

printf(" %d",Node->data);

In_order(Node->rightchild);

}

}

/* Post-order traversal */

void Post_order(struct NODE *Node)

{

if(Node)

{

Post_order(Node->leftchild);

Post_order(Node->rightchild);

printf(" %d",Node->data);

}

}

/* Function Main */

void main()

{

char List[100];

int Number=0;

int info;

char choice;

struct NODE *T=(struct NODE*)malloc(sizeof(struct NODE));

T=NULL;

printf("\n Input Choice 'b' to break : ");

choice=getchar();

fflush(stdin);

while(choice != 'b')

{

printf("\n Input information of the node : ");

scanf("%d",&info);

List[Number++]=info;

fflush(stdin);

printf("\n Input choice 'b' to break : ");

choice=getchar();

fflush(stdin);

}

Number--;

printf("\n Number of elements in the list is %d ",Number);

T=Binary_Tree(List,0,Number);

display(T,1);

printf("\n Pre-order traversal \n");

Pre_order(T);

printf("\n In-order traversal \n");

In_order(T);

printf("\n Post-order traversal \n");

Post_order(T);

}

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

It is possible, you'll have to use a structure like this:

typedef struct TreeElement {

struct TreeElement *left, *right;

int data;

} TreeElement;

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

1010101

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How to write a program to implement a binary search tree in C?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Write a program of binary heap in c or c language?

to implement operations on binary heap in c


Write a program to implement prim's algorithm?

dfgbrgffee


How do you write a c program to convert binary to decimal by using while statement?

write a c++ program to convert binary number to decimal number by using while statement


Write A program to implement insertion using AVL trees?

yes


Write a program to implement domain and referential integrity?

give me the program which can related on domain and referential integrity.


Write a C program that takes a binary file as input and finds error check using different mechanisms?

write a c program that takes a binary file as input and finds error check using different mechanisms.


Write a program in c plus plus to implement macro processor?

Don't write, it is already written, google for 'cpp'.


Write a program in c language to implement framing methods like character stuffing?

A program in c language to implement framing methods like character stuffing can be grave sizeCRC-32 and the variable c50.


Write a program to implement a line using slope intercept formula?

lund lelo mooh mein


How can you write a program to convert binary code to gray code using 8085 microprocessor?

It can be implemented very easily .... Suppose the Binary word is X7X6X5.... X0 then the corresponding Gray code is G7G6G5....G0 where G7=X7 G6=X7 XOR X6 G5=X6 XOR X5 ..... G0=X1 XOR X0 Now implement the above algorithm


Write a program that takes a binary file as input and finds error check using different mechanism?

networking


Write a C program to implement excelp system call?

You are not able to do that, but you can write a program, that uses the system-call./* exectest.c */#include int main (void){execlp ("/bin/ls", "ls", "-ld", ".", NULL);return 0;}