Results 1 to 2 of 2

Thread: Write a program in C language for Binary tree traversal

  1. #1

    Write a program in C language for Binary tree traversal

    Code:
    /* Binary tree traversal */
    /* BT_TRAV.C */
    
    # include<stdio.h>
    
    struct NODE
    {
    	char Info;
    	struct NODE *Left_Child;
    	struct NODE *Right_Child;
    };
    
    struct NODE *Binary_Tree (char *, int, int);
    void Output(struct NODE *, int );
    void Pre_order(struct NODE *);
    void In_order(struct NODE *);
    void Post_order(struct NODE *);
    
    /* Function to create an 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->Info = List [Mid];
    	if ( Lower>= Upper)
    	{
    		Node->Left_Child = NULL;
    		Node->Right_Child = NULL;
    		return (Node);
    	}
    
    	if (Lower <= Mid - 1)
    		Node->Left_Child = Binary_Tree (List, Lower, Mid - 1);
    	else
    		Node->Left_Child = NULL;
    	if (Mid + 1 <= Upper)
    		Node->Right_Child = Binary_Tree (List, Mid + 1, Upper);
    	else
    		Node->Right_Child = NULL;
    	return(Node);
    }
    
    /* Output function */
    
    void Output(struct NODE *T, int Level)
    {
    	int i;
    	if (T)
    	{
    		Output(T->Right_Child, Level+1);
    		printf("\n");
    		for (i = 0; i < Level; i++)
    			printf("   ");
    		printf(" %c", T->Info);
    		Output(T->Left_Child, Level+1);
    	}
    }
    
    /* Pre-order traversal */
    
    void Pre_order (struct NODE *Node)
    {
    	if (Node)
    	{
    		printf(" %c", Node->Info);
    		Pre_order(Node->Left_Child);
    		Pre_order(Node->Right_Child);
    	}
    }
    
    /* In-order traversal */
    
    void In_order (struct NODE *Node)
    {
    	if (Node)
    	{
    		In_order(Node->Left_Child);
    		printf(" %c", Node->Info);
    		In_order(Node->Right_Child);
    	}
    }
    
    /* Post-order traversal */
    
    void Post_order (struct NODE *Node)
    {
    	if (Node)
    	{
    		Post_order(Node->Left_Child);
    		Post_order(Node->Right_Child);
    		printf(" %c", Node->Info);
    	}
    }
    
    /*  Function main */
    
    void main()
    {
    	char List[100];
    	int Number = 0;
    	char 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("%c", &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);
    	Output(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);
    }
    
    
    techforum4u.com Member!!

  2. #2

    Re: Write a program in C language for Binary tree traversal

    Code:
    #include<stdio.h>
    #include<conio.h>
    struct node
    {
    int data;
    struct node *right, *left;
    }*root,*p,*q;
    
    struct node *make(int y)
    {
    struct node *newnode;
    newnode= (struct node *)malloc(sizeof(struct node));
    newnode->data=y;
    newnode->right=newnode->left=NULL;
    return(newnode);
    }
    
    void left(struct node *r,int x)
    {
    if(r->left!=NULL)
    printf("\n Invalid !");
    else
    r->left=make(x);
    }
    
    void right(struct node *r,int x)
    {
    if(r->right!=NULL)
    printf("\n Invalid !");
    else
    r->right=make(x);
    }
    
    void inorder(struct node *r)
    {
    if(r!=NULL)
    {
    inorder(r->left);
    printf("\t %d",r->data);
    inorder(r->right);
    }
    }
    
    void preorder(struct node *r)
    {
    if(r!=NULL)
    {
    printf("\t %d",r->data);
    preorder(r->left);
    preorder(r->right);
    }
    }
    
    void postorder(struct node *r)
    {
    if(r!=NULL)
    {
    postorder(r->left);
    postorder(r->right);
    printf("\t %d",r->data);
    }
    }
    
    void main()
    {
    int no;
    int choice;
    clrscr();
    printf("\n Enter the root:");
    scanf("%d",& no);
    root=make(no);
    p=root;
    while(1)
    {
    printf("\n Enter another number:");
    scanf("%d",& no);
    if(no==-1)
    break;
    p=root;
    q=root;
    while(no!=p->data && q!=NULL)
    {
    p=q;
    if(no<p->data)
    q=p->left;
    else
    q=p->right;
    }
    if(no<p->data)
    {
    printf("\n Left branch of %d is %d",p->data,no);
    left(p,no);
    }
    else
    {
    right(p,no);
    printf("\n Right Branch of %d is %d",p->data,no);
    }
    }
    while(1)
    {
    printf("\n 1.Inorder Traversal \n 2.Preorder Traversal \n 3.Postorder Traversal \n 4.Exit");
    printf("\n Enter choice:");
    scanf("%d",&choice);
    switch(choice)
    {
    case 1 :inorder(root);
    break;
    case 2 :preorder(root);
    break;
    case 3 :postorder(root);
    break;
    case 4 :exit(0);
    default:printf("Error ! Invalid Choice ");
    break;
    }
    getch();
    }
    
    }

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2013, 06:22 AM
  2. Replies: 0
    Last Post: 11-06-2011, 02:49 PM
  3. Replies: 0
    Last Post: 11-06-2011, 02:45 PM
  4. Replies: 0
    Last Post: 11-05-2011, 04:35 AM
  5. Binary Search Tree using C
    By smith in forum C
    Replies: 1
    Last Post: 07-12-2011, 06:12 AM

Members who have read this thread : 1

You do not have permission to view the list of names.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Disclaimer: Users of techforum4u.com are responsible for ensuring that any material they post (article, blog posts, images or other mulitimedia content) does not violate or infringe upon the copyright, patent, trademark, or any personal or proprietary rights of any third party, and is posted with the permission of the owner of such rights.Anyone who violates these rules may have their access privileges removed without warning.