Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
this was the questio give :
Write a menu-based C program to perform operations for a binary search tree (BST).

a. Search an element b. Find minimum c. Find maximum d. Insertion e. Deletion

In this code Im unable to perform search and delete element.I expect output to show that deleted element is this and output to ask user to enter an element they wanna search in the binary tree and give it as output.

please help need urgently!!!!!!

What I have tried:

#include<stdio.h>
#include<stdlib.h>

struct node
{
        struct node *lchild;
        int info;
        struct node *rchild;
};

struct node *insert(struct node *ptr, int ikey);
struct node *Min(struct node *ptr);
struct node *Max(struct node *ptr);
void display(struct node *ptr,int level);

int main( )
{
        struct node *root=NULL,*ptr;
        int choice,k;

        while(1)
        {
                printf("\n");
                printf("1.Insert\n");
                printf("2.Display\n");
                printf("3.Find minimum and maximum\n");
                printf("4.Quit\n");
                printf("\nEnter your choice : ");
                scanf("%d",&choice);

                switch(choice)
                {
                case 1:
                        printf("\nEnter the key to be inserted : ");
                        scanf("%d",&k);
                        root = insert(root, k);
                        break;

        case 2:
            printf("\n");
            display(root,0);
            printf("\n");
            break;

                 case 3:
                        ptr = Min(root);
                        if(ptr!=NULL)
                                printf("\nMinimum key is %d\n", ptr->info );
                        ptr = Max(root);
                        if(ptr!=NULL)
                                printf("\nMaximum key is %d\n", ptr->info );
                        break;

                 case 4:
                        exit(1);

                 default:
                        printf("\nWrong choice\n");
                }/*End of switch */
        }/*End of while */

        return 0;

}/*End of main( )*/


struct node *insert(struct node *ptr, int ikey )
{
        if(ptr==NULL)
        {
                ptr = (struct node *) malloc(sizeof(struct node));
                ptr->info = ikey;
                ptr->lchild = NULL;
                ptr->rchild = NULL;
        }
        else if(ikey < ptr->info) /*Insertion in left subtree*/
                ptr->lchild = insert(ptr->lchild, ikey);
        else if(ikey > ptr->info) /*Insertion in right subtree */
                ptr->rchild = insert(ptr->rchild, ikey);
        else
                printf("\nDuplicate key\n");
        return ptr;
}/*End of insert( )*/


struct node *Min(struct node *ptr)
{
        if(ptr==NULL)
                return NULL;
        else if(ptr->lchild==NULL)
        return ptr;
        else
                return Min(ptr->lchild);
}/*End of min()*/
DELETE(T, z)
  if z.left == NULL
      TRANSPLANT(T, z, z.right)
  elseif z.right == NULL
      TRANSPLANT(T, z, z.left)
  else
      y = MINIMUM(z.right) //minimum element in right subtree
      if y.parent != z //z is not direct child
          TRANSPLANT(T, y, y.right)
          y.right = z.right
          y.right.parent = y
      TRANSPLANT(T, z, y)
      y.left = z.left
      y.left.parent = y

SEARCH(x, T)
  if(T.root != null)
      if(T.root.data == x)
          return r
      else if(T.root.data > x)
          return SEARCH(x, T.root.left)
      else
          return SEARCH(x, T.root.right)
struct node *Max(struct node *ptr)
{
        if(ptr==NULL)
                return NULL;
        else if(ptr->rchild==NULL)
        return ptr;
        else
                return Max(ptr->rchild);
}/*End of max()*/


void display(struct node *ptr,int level)
{
        int i;
        if(ptr == NULL )/*Base Case*/
                return;
        else
    {
                display(ptr->rchild, level+1);
                printf("\n");
                for (i=0; i<level; i++)
                        printf("    ");
                printf("%d", ptr->info);
                display(ptr->lchild, level+1);
        }
}
Posted
Updated 18-Jan-22 7:01am
v4

1 solution

While we are more than willing to help those that are stuck, that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

If you are having problems getting started at all, then this may help: How to Write Code to Solve a Problem, A Beginner's Guide[^]
 
Share this answer
 
Comments
Rosaila 18-Jan-22 12:06pm    
Now i have posted where im stuck please help now.
OriginalGriff 18-Jan-22 12:36pm    
You may be stuck there, but there is no sign of what you have tried - and we are no a "code to order" service.

So if you have tried to implement search or delete and it doesn't work, then show us what code you tried, tell us what happens when you run that code, what you expected to happen, how you checked what happened. Tell us what you tored to work out why, and what happened when you did.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.

At the moment, you don't even have code to add an element, so there is no way we could even test what you have done.
Rosaila 18-Jan-22 12:39pm    
ok ill modify it again (sorry to disturb you again and again )
Rosaila 18-Jan-22 12:51pm    
now is it fine? (again sorry for disturbance)
Rosaila 18-Jan-22 13:14pm    
??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900