GfG QA is closing soon... Please refer PRACTICE or GfG now on wards !!

To convert a Binary tree to a binary search tree.What is the problem with the code it's not copying the contents of the array back to the tree.

#include<stdio.h>
#include<stdlib.h>
int x=0;
int l=0;
typedef struct node
{
int data;
struct node *rlink;
struct node *llink;
}Node;
Node *getnode()
{
    Node *temp=(Node *)malloc(sizeof(Node));
    return temp;
}
int a[10];
int i=0;
Node *create(Node *root,int key)
{
int c;
if(root==NULL)
{
    root=getnode();
    root->rlink=root->llink=NULL;
    root->data=key;
}
else
{
printf("Enter where you want to enter 1.RIGHT 2.LEFT");
scanf("%d",&c);
switch(c)
{
case 1:root->rlink=create(root->rlink,key);
    break;
case 2:root->llink=create(root->llink,key);
        break;
default:printf("Wrong choice");
        break;
}
}
return root;
}

void inorder(Node *root)
{
if(root!=NULL)
{
inorder(root->rlink);
if(root->data>0)
    a[i++]=root->data;
inorder(root->llink);
}
}
void utu(Node *h)
{
if(h!=NULL)
{utu(h->rlink);
printf("%d",h->data);
utu(h->llink);
}
}
void count(Node *h)
{
   if(h!=NULL)
    {
    count(h->llink);
        x++;
    count(h->rlink);
}
}
int max(int a,int b)
{
if(a>b)
    return a;
return b;
}
int height(Node *root)
{
    if(root==NULL)
        return 0;
    return max(height(root->llink),height(root->rlink))+1;
}
int widt(Node *root)
{int n=0;
int h=height(root);
int max=0;
int wid=0;
for(n=1;n<h;n++)
{
    wid=width(root,n);
    if(wid>max)
        max=wid;
}
return max;
}
int width(Node *root,int level)
{
    if(root==NULL)
        return 0;
    else if(level==1)
        return 1;
    else if(level>1)
        return width(root->llink,level-1)+width(root->rlink,level-1);
}
int maxd(Node *root)
{
if(root==NULL)
    return 0;
int leftd=0,rightd=0;
int lheight=height(root->llink);
int rheight=height(root->rlink);
leftd=maxd(root->llink);
rightd=maxd(root->rlink);
return max(lheight+rheight+1,max(leftd,rightd));
}
void inr(Node *root)
{
if(root==NULL)
    return;
else if(root!=NULL)
{
inr(root->llink);
root->data=a[l++];
inr(root->rlink);
}
}
void main()
{
int j=0;
int temp=0;
Node *h=NULL;
h=create(h,5);
h=create(h,6);
h=create(h,7);
h=create(h,8);
inorder(h);
count(h);
printf("%d \n",x);
printf("\n \n \n");
for(j=0;j<x;j++)
{
    printf("%d \n",a[j]);
}
for(int i=0;i<x;i++)
{
    for(j=i+1;j<x;j++)
    {
        if(a[i]>a[j])
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
}
utu(h);
//for(j=0;j<x;j++)
//{
//    printf("\n %d \n",a[j]);
//}
printf("\n \n");
//j=maxd(h);
//printf("%d",j);
inr(h);
utu(h);
}

#include<stdio.h>
#include<stdlib.h>
int x=0;
int l=0;
typedef struct node
{
int data;
struct node *rlink;
struct node *llink;
}Node;
Node *getnode()
{
    Node *temp=(Node *)malloc(sizeof(Node));
    return temp;
}
int a[10];
int i=0;
Node *create(Node *root,int key)
{
int c;
if(root==NULL)
{
    root=getnode();
    root->rlink=root->llink=NULL;
    root->data=key;
}
else
{
printf("Enter where you want to enter 1.RIGHT 2.LEFT");
scanf("%d",&c);
switch(c)
{
case 1:root->rlink=create(root->rlink,key);
    break;
case 2:root->llink=create(root->llink,key);
        break;
default:printf("Wrong choice");
        break;
}
}
return root;
}

void inorder(Node *root) //to copy the contents  of the tree in the array
{
if(root!=NULL)
{
inorder(root->rlink);
if(root->data>0)
    a[i++]=root->data;
inorder(root->llink);
}
}
void utu(Node *h) //For inorder traversal
{
if(h!=NULL)
{utu(h->rlink);
printf("%d",h->data);
utu(h->llink);
}
}
void inr(Node *root) //to copy the elements of sorted array in the tree to make it a BST
{
if(root==NULL)
    return;
else if(root!=NULL)
{
inr(root->llink);
root->data=a[l++];
inr(root->rlink);
}
}
void main()
{
int j=0;
int temp=0;
Node *h=NULL;
h=create(h,5);
h=create(h,6);
h=create(h,7);
h=create(h,8);
inorder(h);
count(h);
printf("%d \n",x);
printf("\n \n \n");
for(j=0;j<x;j++)
{
    printf("%d \n",a[j]);
}
for(int i=0;i<x;i++) //bubble sorting of the array obtained by copying the nodes data in the array
{
    for(j=i+1;j<x;j++)
    {
        if(a[i]>a[j])
        {
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
    }
}
utu(h);
//for(j=0;j<x;j++)
//{
//    printf("\n %d \n",a[j]);
//}
printf("\n \n");
//j=maxd(h);
//printf("%d",j);
inr(h);
utu(h);
}

 

asked Nov 12, 2016 by SAGAR9999

1 Answer

/*
 * C Program to Convert Binary Tree to Binary Search Tree 
 */
#include <stdio.h>
#include <stdlib.h>
 
/*
 * Structure of the binary tree
 */
struct btnode 
{
    int value;
    struct btnode *l;
    struct btnode *r;
};
 
void createbinary();
void inorder(node *);
int count(node*);
node* add(int );
void sort();
void binary_to_bst(node *);
int compare(const void *,const void *);
void inorder_to_array(node*,int[],int *);
void array_to_bst(int *arr,node *,int *);
void display_bst(node *);
void print();
void print_level(node*,int,int);
int height(node*);
 
typedef struct btnode node;
node *root = NULL,*ptr;
 
int data[10];
int i = 0;
 
int  main()
{
    createbinary();
    binary_to_bst(root);
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printf("\nThe inorder of binary search tree\n");
    inorder(root);
    printf("\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n");
    printf("\n================================================");
    printf("\nThe nodes of a binary search tree (LEVEL WISE)");
    printf("\n=================================================");
    print();
}
/*
 * constructing the following binary tree
 *     50
 *     / \
 *    20 30
 *   / \ 
 *  70 80
 * / \     \
 *10 40      60
 */    
void createbinary()
{
    root = add(50);
    root->l = add(20);
    root->r = add(30);
    root->l->l = add(70);
    root->l->r = add(80);
    root->l->l->l = add(10);
    root->l->l->r = add(40);
    root->l->r->r = add(60);
}
 
/*
 * Adds a node to the tree
 */
node* add(int val)
{
    ptr = (node*)malloc(sizeof(node));
    if (ptr == NULL)
    {
        printf("Memory was not allocated");
        return 0;
    }
    ptr->value = val;
    ptr->l = NULL;
    ptr->r = NULL;
    return ptr;
}
 
/*
 * Store the inorder of binary tree
 */
void inorder_to_array(node *n,int data[],int *ptr)
{
    if (n != NULL)
    {
        inorder_to_array(n->l,data,ptr);
        data[*ptr] = n->value;
        (*ptr)++;
        inorder_to_array(n->r,data,ptr);
    }
} 
 
/*
 * counting the number of nodes in a tree
 */
int count(node *n)
{
    int c = 1;
 
    if (n == NULL)
        return 0;
    else
    {
        c += count(n->l);
        c += count(n->r);
        return c;
    }
}
/*
 *Display inorder of the BST
 */
void inorder(node *root)
{
    if (root != NULL)
    {
        inorder(root->l);
        printf("%d->", root->value);
        inorder(root->r);
    }
 
}
/*
 * print the nodes of the BST for all levels until height of the tree is reached
 */
 
void print()
{
    int h, i;
 
    h = height(root);
    for(i = 0;i < h;i++)
    {
        printf("\nLEVEL %d  :", i);
        print_level(root, i, 0);
        printf("\n");
    }
}
 
/*
 * print the nodes of the BST for a particular level
 */
void print_level(node *n, int desired, int current)
{
    if (n)
    {
        if (desired == current)
        {    
            printf("  %5d", n->value);
        } 
        else
        {
            print_level(n->l, desired, current + 1);
            print_level(n->r, desired, current + 1);
        }
   }
}
 
/*
 * Height of binary tree
 */
int height(node *n)
{
    int lheight, rheight;
 
    if (n != NULL)
    {
        lheight = height(n->l);
        rheight = height(n->r);
        if (lheight > rheight)
            return(lheight + 1);
        else 
            return(rheight + 1);
    }
    else
    {
        return 0;
    }
}    
int compare(const void *a, const void *b)
{
    return *(int*)a-*(int*)b;
}
 
/*
 * copies the elements of array to binary tree
 */
void array_to_bst(int *arr, node *root, int *indexptr)
{
    if (root != NULL)
    {
        array_to_bst(arr,root->l, indexptr);
        root->value = arr[i++];
        array_to_bst(arr,root->r, indexptr);
    }
}
 
/*
 * Converting binary tree to binary search tree
 * storeinorder() function stores the inorder traversal of binary tree
 * qsort() sorts the inorder of binary tree
 * arraytobst() copies the elements of array to binary tree
 * Then binary tree converted to binary search tree
 */ 
 
void binary_to_bst(node *root)
{
    int n, i;
 
    if (root == NULL)
        return;
    n = count(root);
    i = 0;
    inorder_to_array(root, data, &i);
    qsort(&data, n, sizeof(data[0]), compare);
    i = 0;
    array_to_bst(data, root, &i);
}

 

answered Nov 15, 2016 by Kishlay Verma
...