2

I was solving a problem and it stated:

Write a program that processes the following queries on a Binary Search Tree:

  1. i x: Insert x in the BST
  2. d x: Delete x from the BST

Input format

Line 1 contains an integer Q, the number of queries

The next Q lines are of the form i x or d x

Output format

For each query, print the position of x in the BST

If the position of a node is p, the positions of its left and right children are 2*p and 2*p+1 respectively

Position of the root node is 1

Question's link

11  //Queries
i 15   //i=insert; d=delete  
i 9
i 25
i 8
i 13
i 18
i 19
i 7
i 11
d 9
i 14

Everything is working fine until I delete node 9. then the position of node 14 is coming out to be 5. see the diagram:Initially,

            15
          /    \
         9      25
        / \     /
       8  13  18  
      /  /      \
     7  11       19

After deleting 9;

        15
      /    \
     11      25
    / \     /
   8  13  18  
  /         \
 7          19 

After inserting 14

            15
          /    \
         11      25
        / \     /
       8  14  18  
      /  /      \
     7  13       19

Correct format should be

        15
      /    \
     11      25
    / \     /
   8  13  18  
  /    \    \
 7      14  19  

#include<bits/stdc++.h>
#define ll long long
using namespace std;

ll position=1;

struct BSTNode
{
int data;
BSTNode *left,*right;
};

BSTNode *getNewNode(int data)    
{
 BSTNode *newNode = new BSTNode(); 
 newNode->data = data;             
 newNode->left = newNode->right = NULL; 

  return newNode;   //returns address of new node
 }
 BSTNode* insert(BSTNode *root,int data)
 {
    if(root==NULL){
    root = getNewNode(data);

  }
  else if(data<root->data)
  {
     root->left = insert(root->left,data);
  }
  else if(data>root->data)
  {
     root->right = insert(root->right,data);
  }
 return root;
}

BSTNode *findMin(BSTNode *root)
{
  if(root->left ==NULL)
  {
      return root;
  }
  else
      findMin(root->left);

}

bool search(BSTNode *root,int data)
{
  if(root == NULL)
  {
     return 0;
  }
  if(data<root->data)
  {
     position=2*position;

     search(root->left,data);
  }
   else if(data>root->data)
  {
      position=2*position+1; 
      search(root->right,data);
  }
  else
  {
      cout<<"Found";
      return 1;
  }
}

BSTNode* delet(BSTNode* root,int data)
{

   if(root == NULL)
  {
      return 0;
   }
   else if(data<root->data)
   {

     root->left = delet(root->left,data);

   }
   else if(data>root->data)
  {
     root->right = delet(root->right,data);
  }
   else           //Found
  {
   //CASE 1:No child
      if(root->left == root->right ==NULL)
      {
       delete root;
       root = NULL;

       }
   //CASE2: One child
     else if(root->left == NULL)
     {
       BSTNode *temp= root;
       root = root->right;
       delete temp;

     }
      else if(root->right == NULL)
      {
       BSTNode *temp=root;
       root= root->left;
       delete temp;

      }
     //CASE 3: TWO CHILD
     else
     {
      BSTNode *temp = findMin(root->right);
      root->data = temp->data;
      root->right = delet(root->right,root->data);
     }

    return root;
  }

}

int main()
{
BSTNode* root = NULL;  //rootptr- pointer to node
  //tree is empty
   int n,input,data,del;
   char c;

   cin>>n;
   while(n--)
   {
      cin>>c;
      cin>>input;


      if(c=='i')
      {
      root = insert(root,input);
      search(root,input);

      }
      if(c=='d')
      {

        search(root,input);
        delet(root,input);

      }
      cout<<position<<endl;
      position=1;

   }

   return 0;
}

How is this possible insertion is being done as a leaf node Then?

  • Hey Harshal, it seems everything is fine. What's problem you are facing? – Faruk Hossain Jul 30 '19 at 11:45
  • @FarukHossain When I'm deleting 9, it is being replaced by 11 on its right node 13 should be there but when I'm inserting 14 it is replacing 13 and adding it as leaf whereas 14 should have been added as leaf – Harshal Sharma Jul 30 '19 at 12:13
  • 1
    A few C++ suggestions: 1. Don't use `#define` when you can help it. So if you must, do `using ll = long long;`. 2. https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice 3. Use classes: Instead of `getNewNode`, give `BSTNode` a constructor. 4. Use `nullptr` instead of `NULL`, as `nullptr` provides more type safety. 5. Use `std::unique_ptr` when you can; raw pointers are asking for trouble. – Ben Jul 30 '19 at 12:18
  • You should never `#include `. It is not proper C++. It ruins portability and fosters terrible habits. See [Why should I not `#include `](https://stackoverflow.com/q/31816095). – L. F. Jul 30 '19 at 12:46
  • Also, avoid `using namespace std;`. It is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Jul 30 '19 at 12:46

1 Answers1

-1

in the line Correct format should be your diagram is wrong. 14 is the right child of 13. Assume that you want to insert a node is a binary search tree, you need to save the last parent node in the your searching path if the given node is not found. and insert the new node to the last parent node got in the function insert(). The program is not recursive.

here is an example in C.

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

struct t_Data
{
  int m_Info;
};

struct t_Node
{
  struct t_Data m_Data;
  struct t_Node* m_LeftChild;
  struct t_Node* m_RightChild;
};

typedef struct t_Node* t_BinarySortTree;

/* return targetNode or the last node in the path */
int SearchBST(t_BinarySortTree T, int aGivenInfo, struct t_Node* lastParentNode, struct t_Node* *result)
{
  if(!T)
  {
    *result = lastParentNode;
    return 0;
  }
  else if (aGivenInfo == (*T).m_Data.m_Info)
  {
    *result = T;
    return 1;
  }
  else if(aGivenInfo < (*T).m_Data.m_Info)
  {
    lastParentNode = T;
    return SearchBST((*T).m_LeftChild,aGivenInfo,lastParentNode,result);
  }
  else
  {
    lastParentNode = T;
    return SearchBST((*T).m_RightChild,aGivenInfo,lastParentNode,result);
  }

}

void InsertBST(t_BinarySortTree *T, struct t_Data newData)
{
  int status;
  struct t_Node* result;

  status = SearchBST(*T,newData.m_Info,NULL,&result);

  /* condition: fail to search for 'newData' in the binary sort tree */
  if (!status)
  {
    struct t_Node* newNode;
    newNode = (struct t_Node*)malloc(sizeof(struct t_Node));
    (*newNode).m_Data = newData;
    (*newNode).m_LeftChild = NULL;
    (*newNode).m_RightChild = NULL;

    /* condition: result == NULL */
    if (!result)
    {
      *T = newNode;
    }
    else if (newData.m_Info < (*result).m_Data.m_Info)
    {
      (*result).m_LeftChild = newNode;
    }
    /* condition: newData.m_Info > (*result).m_Data.m_Info */
    else
    {
      (*result).m_RightChild = newNode;
    }
  }
}

int main(void)
{
  t_BinarySortTree aBST;
  aBST = NULL;

  struct t_Data d1,d2,d3,d4,d5,d6,d7,d8,d9,d10;
  d1.m_Info = 62;
  d2.m_Info = 88;
  d3.m_Info = 58;
  d4.m_Info = 47;
  d5.m_Info = 35;
  d6.m_Info = 73;
  d7.m_Info = 51;
  d8.m_Info = 99;
  d9.m_Info = 37;
  d10.m_Info = 93;

  InsertBST(&aBST,d1);
  InsertBST(&aBST,d2);
  InsertBST(&aBST,d3);
  InsertBST(&aBST,d4);
  InsertBST(&aBST,d5);
  InsertBST(&aBST,d6);
  InsertBST(&aBST,d7);
  InsertBST(&aBST,d8);
  InsertBST(&aBST,d9);
  InsertBST(&aBST,d10);

}