Program to create Simple Binary Tree with recursive traversal | Wave the world

Program to create Simple Binary Tree with recursive traversal

/*
    Simple Binary Tree with recursive traversal
    Created By : Pirate
*/

/*Create the simple binary tree and recursive traversal*/
#include<stdio.h>
#include<conio.h>
#include<malloc.h>

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

void insert (node *, node*);
void inorder (node *);
void preorder (node *);
void postorder (node *);
node *get_node();

int main(){
    int choice;
    char ans = 'n';
    node *newNode,*root;
    root = NULL;
    do{
        printf("*** Simple Binary Tree ***\n");
        printf("\n1.Create\n");
        printf("2.Inorder\n");
        printf("3.Preorder\n");
        printf("4.Postorder\n");
        printf("5.Exit\n");
        printf("Enter Your choice: ");
        scanf("%d",&choice);
        switch(choice){
            case 1:
                root = NULL;
                do{
                    newNode=get_node();
                    printf("Enter the Element\n");
                    scanf("%d",& newNode->data);
                    if(root == NULL)
                        root= newNode;
                    else
                        insert(root, newNode);
                    printf("\nDo you want to enter more elements?(Y/N)\n");
                    ans = getch();
                }while(ans== 'Y'|| ans=='y');
                break;
            case 2:
                if(root == NULL)
                    printf("Tree is not created!");
                else
                    inorder(root);
                break;
            case 3:
                if(root ==NULL)
                    printf("Tree is not created!");
                preorder (root);
                break;
            case 4:
                if(root == NULL)
                    printf("Tree is not created!");
                postorder(root);
                break;
        }
    }while(choice!=5);
    return 0;
}
node *get_node(){
    node *temp;
    temp = (node *) malloc(sizeof(node));
    temp->left = NULL;
    temp->right = NULL;
    return temp;
}
void insert(node *root, node *newNode){
    char ch;
    printf("Where to insert left (L)/right (R) of %d:\n", root->data);
    ch=getche();
    if((ch=='R') || (ch=='r')){
        if(root->right == NULL){
            root->right=newNode;
        }
        else
            insert(root->right,newNode);
        }
    else{
        if(root->left== NULL){
            root->left=newNode;
        }
        else
            insert(root->left,newNode);
    }
}
void inorder(node *temp){
    if(temp!= NULL){
        inorder(temp->left);
        printf("%d\n", temp->data);
        inorder(temp ->right);
    }
}
void preorder(node *temp){
    if(temp != NULL){
        printf("%d\n",temp->data);
        preorder(temp->left);
        preorder(temp->right);
    }
}
void postorder(node *temp){
    if(temp!= NULL){
        postorder(temp-> left);
        postorder(temp->right);
        printf("%d\n",temp->data);
    }
}

Output :

Enjoy :)

1 comments:

  1. <Investing online has been a main source of income, that's why knowledge plays a very important role in humanity, you don't need to over work yourself for money.All you need is the right information, and you could build your own wealth from the comfort of your home!Binary trading is dependent on timely signals, assets or controlled strategies which when mastered increases chance of winning up to 90%-100% with trading. It’s possible to earn $10,000 to $20,000 trading weekly-monthly in cryptocurrency(bitcoin) investment,just get in contact with Mr Bernie Doran my broker. I had almost given up on everything and even getting my lost funds back, till i met with him, with his help and guidance now i have my lost funds back to my bank account, gained more profit and I can now trade successfully with his profitable strategies and software!! 
Reach out to him through Gmail : Bernie.doranfx01@gmail.com ,Telegram: bernie_fx or WhatsApp +1(424)285-0682 for inquires

    ReplyDelete

 

Pro

About