【问题标题】:BST insertion,deletion,searchBST 插入、删除、搜索
【发布时间】:2019-07-30 10:03:11
【问题描述】:

我正在解决一个问题,它说:

编写一个程序,在二叉搜索树上处理以下查询:

  1. i x:在 BST 中插入 x
  2. d x:从 BST 中删除 x

输入格式

第1行包含一个整数Q,查询次数

接下来的 Q 行是 i x 或 d x 的形式

输出格式

对于每个查询,打印 x 在 BST 中的位置

如果一个节点的位置是p,那么它的左右孩子的位置分别是2*p和2*p+1

根节点的位置是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

一切正常,直到我删除节点 9。然后节点 14 的位置变为 5。见图:最初,

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

删除9后;

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

插入14后

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

正确的格式应该是

        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;
}

这种可能的插入是如何作为叶节点完成的呢?

【问题讨论】:

  • 嘿,Harshal,看起来一切都很好。您面临什么问题?
  • @FarukHossain 当我删除 9 时,它在其右侧节点上被 11 替换,但当我插入 14 时,它正在替换 13 并将其添加为叶子,而 14 应该已添加为叶子
  • 一些 C++ 建议: 1. 有帮助时不要使用#define。因此,如果必须,请执行using ll = long long;。 2.stackoverflow.com/questions/1452721/… 3.使用类:代替getNewNode,给BSTNode一个构造函数。 4. 使用nullptr 而不是NULL,因为nullptr 提供了更多的类型安全性。 5. 尽可能使用std::unique_ptr;原始指针自找麻烦。
  • 你永远不应该#include &lt;bits/stdc++.h&gt;。它不是正确的 C++。它破坏了便携性并养成了糟糕的习惯。见Why should I not #include &lt;bits/stdc++.h&gt;
  • 另外,避免using namespace std;。这被认为是不好的做法。见Why is “using namespace std;” considered bad practice?

标签: c++ tree insert binary-search-tree


【解决方案1】:

在行中 Correct format should be 你的图表是错误的。 14是13的右孩子。 假设您要插入的节点是二叉搜索树,如果未找到给定节点,则需要将最后一个父节点保存在搜索路径中。并将新节点插入到函数insert() 中获得的最后一个父节点。该程序不是递归的。

这是一个 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);

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多