Implementation of Binary Tree in C programming:

Number of student who learning C language having fear of data structure, but data structure is very important in any language to solve complex problem and it not so difficult to learn it just you have to imagine every thing.

If you are still playing with IF.. ELSE now its time to upgrade to Data structure and pointers.  

* BEFOR START PROGRAMMING

>First clear that what you want to implement.

>Make some rough work , diagram and decide your programs flow(must follow).

>Decide about  input output

input : 50 12 60 69 75 36 25

 

>Here we can imagine what is our input and what output we expect.

 >Now start for coding confidently that you can implement binary tree.

* PROGRAMING: 

 > Make habit to write your own header file.
>"btree.h"

#ifndef __BTREE
#define __BTREE

#include <stdio.h>


typedef struct btree_t
{
    struct btree_t *left;
    int data;
    struct btree_t *right;
} btree_t;

#endif

 >give some name for diagrame and make sure you give that name to variable which you are going to used in programming.

>"main.c"

#include "btree.h"

btree_t *head;

int insert()
{
    int val;
    printf("Insert value\n");
    scanf_s("%d",&val);
    if (head == NULL)
    {
        head = (btree_t *)malloc(sizeof(btree_t));
        head->data = val;
        head->left = NULL;
        head->right = NULL;
    }
    else
    {
        btree_t *ptr = head;
        while (1)
        {
            if (val > ptr->data)
            {
              
                if (ptr->right == NULL)
                {
                    ptr->right = (btree_t *)malloc(sizeof(btree_t));
                    ptr = ptr->right;
                    ptr->data = val;
                    ptr-> right = NULL;
                    ptr->left = NULL;
                    return 0;
                }
                else
                    ptr = ptr->right;

            }
            else if (val < ptr->data)
            {
                if (ptr->left == NULL)
                {
                  
                    ptr->left = (btree_t *)malloc(sizeof(btree_t));
                    ptr = ptr->left;
                    ptr->data = val;
                    ptr->right = NULL;
                    ptr->left = NULL;
                    return 0;
                }
                else
                    ptr = ptr->left;
            }
            else if (val == ptr->data)
            {
                printf("Duplicate value\n");
                break;
            }
        }
    }
    return -1;
}

void LVR(btree_t *ptr)
{
    if (ptr != NULL)
    {
        LVR(ptr->left);
        printf("%d ", ptr->data);
        LVR(ptr->right);
    }
}


void VLR(btree_t *ptr)
{
    if (ptr != NULL)
    {
        printf("%d ", ptr->data);
        VLR(ptr->left);
        VLR(ptr->right);
    }
}


void LRV(btree_t *ptr)
{
    if (ptr != NULL)
    {
        LRV(ptr->left);
        LRV(ptr->right);
        printf("%d ", ptr->data);
    }
}

void printbt(btree_t *ptr)
{
    printf("LRV traversal : ");
    LVR(head);
    printf("\n");

    printf("VLR traversal : ");
    VLR(head);
    printf("\n");

    printf("LRV traversal : ");
    LRV(head);
    printf("\n");
}

int main()
{
    int ch,err;
  
    do{
        printf("\n\n1. Insert \n2. Print\n3. exit");
        printf("Enter Your Choice\n");
        scanf_s("%d", &ch);
        switch (ch)
        {
        case 1:
            err=insert();
            if (err == -1)
                printf("cant insert value\n");
            else
                printf("value inserted successfully\n");
            break;
        case 2:
            printbt(head);
            break;
        }
    } while (ch != 3);

    return 0;
}


>Both header file and main.c should be in same folder

>Here is code that I implemented.

 

Comments

Popular posts from this blog