Skip to main content
deleted 7 characters in body; edited tags; edited title
Source Link
Jamal
  • 34.9k
  • 13
  • 133
  • 237

optimize Optimizing binary search tree

i madeHow could I optimize this binary search tree.How could i optimize it.?

optimize binary search tree

i made binary search tree.How could i optimize it.

Optimizing binary search tree

How could I optimize this binary search tree?

Source Link

optimize binary search tree

i made binary search tree.How could i optimize it.

#include<stdio.h>
#include<stdlib.h>
struct NODE
{
    int info;
    struct NODE *right;
    struct NODE *left;
    struct NODE *parent;
};
typedef struct NODE node;

node *createnode(node *temp,int number)
{
    temp=(node*)malloc(sizeof(node));
    temp->info=number;
    temp->right=NULL;
    temp->left=NULL;
    temp->parent=NULL;
return temp;
}

node *insert(node *head,int number)
{
    node *temp;
    temp=createnode(temp,number);
    node *traverse=head;
    if(head==NULL)
    {
        head=temp;
        temp->parent=NULL;
        return head;
    }
    else
    {
        while(traverse!=NULL)
        {
            if(number < traverse->info)
            {
                if(traverse->left==NULL)
                {
                    traverse->left=temp;
                    temp->parent=traverse;
                    break;
                }
                traverse=traverse->left;
            }
            else if(number>traverse->info)
            {
                if(traverse->right==NULL)
                {
                    traverse->right=temp;
                    temp->parent=traverse;
                    break;
                }
                traverse=traverse->right;
            }
            else
                break;
        }
    }
return head;
}

node *find(node *head,int number)               // finding the position of the element which we want to delete
{
    node *traverse=head;
    if(traverse==NULL)
        return NULL;
    else
    {
        while(traverse!=NULL)
        {
            if(number<traverse->info)
            {
                if(traverse->left==NULL)
                {
                    printf("Number not found\n");
                    return NULL;
                }
                traverse=traverse->left;
            }
            else if(number>traverse->info)
            {
                if(traverse->right==NULL)
                {
                    printf("Number not found\n");
                    return NULL;
                }
                traverse=traverse->right;
            }
            else
                break;
        }
    }
return traverse;
}
int find_children(node *temp)                   // finding the number of children of the the node that we want to delete
{
    int count=0;
    if(temp->left!=NULL)
        count+=1;
    if(temp->right!=NULL)
        count+=1;
return count;
}
node *delete(node *head,int number)               //deleting the node
{
    int children;
    node *temp=head;
    temp=find(temp,number);
    if(temp==NULL)
        return head;
    else if(head->left==NULL && head->right==NULL)
        return NULL;
    else
    {
        children=find_children(temp);
        if(children==0)
        {
            if(temp->parent->left==temp)
            {
                temp->parent->left=NULL;
            }
            else if(temp->parent->right==temp)
            {
                temp->parent->right=NULL;
            }
        }
        if(children==1)
        {
            if(temp->parent==NULL)
            {
                if(temp->right!=NULL)
                {
                    temp->right->parent=NULL;
                    head=temp->right;
                }
                else if(temp->left!=NULL)
                {
                    temp->left->parent=NULL;
                    head=temp->left;
                }
            }
            else
            {
                if(temp->parent->left==temp)
                {
                    if(temp->right!=NULL)
                    {
                        temp->right->parent=temp->parent;
                        temp->parent->left=temp->right;
                    }
                    else if(temp->left!=NULL)
                    {
                        temp->left->parent=temp->parent;
                        temp->parent->left=temp->left;
                    }
                }
                if(temp->parent->right==temp)
                {
                    if(temp->right!=NULL)
                    {
                        temp->right->parent=temp->parent;
                        temp->parent->right=temp->right;
                    }
                    else if(temp->left!=NULL)
                    {
                        temp->left->parent=temp->parent;
                        temp->parent->right=temp->left;
                    }
                }
            }
        }
        if(children==2)
        {
            node *temp1;
            node *temp2;
            temp1=temp;
            temp2=temp->right;
            while(temp2->left!=NULL)
            {
                temp1=temp2;
                temp2=temp2->left;
            }
            temp->info=temp2->info;
            if(temp1->left==temp2)
            {
                    temp1->left=temp2->left;
            }
            else
            {
                temp1->right=temp2->right;
            }
        }

    }
return head;
}
void printing(node *head)
{
    if(head==NULL)
        return ;
    printing(head->left);
       printf("%d ",head->info);
    printing(head->right);
}

int main()
{
    node *head;
    int number,choice;
    printf("Choose one of the option\n");
    while(1)
    {
        printf("\n1.Enter a element\n2.Delete an element\n3.print tree\n4.Exit\n");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:
            {
                scanf("%d",&number);
                head=insert(head,number);
                break;
            }
            case 2:
            {
                scanf("%d",&number);
                head=delete(head,number);
                break;
            }
            case 3:
            {
                printing(head);
                break;
            }

            case 4:
            {
                exit(0);
                break;
            }
        }
    }
    return 0;
}