English 中文(简体)
二、二元搜索树中的吸收
原标题:Deletion in binary search tree

So when I delete in binary search tree, do I need to have like 7 different cases i.e.

  1. Left Leaf;
  2. Right Leaf;
  3. Left child with only left child. //i.e the node to be deleted is the left child of it s parent and it has only left child.
  4. Left Child with only right child.
  5. Right child with only left child.
  6. Right child with only right child.
  7. Node to be deleted has both the children i.e. right and left.

Now when this code is using if-else it gets pretty nasty.. is there any other way of doing this.

这里,我的准则

if(current->left==NULL && current->right==NULL && current->key<prev->key)   //left leaf
prev->left=NULL;
else if(current->left==NULL && current->right==NULL && current->key>prev->key) // right     leaf
prev->right=NULL;
else if(current->left!=NULL && current->right==NULL && current->key<prev->key) // left     child with one child
prev->left=current->left;
else if(current->left==NULL && current->right!=NULL && current->key<prev->key)
prev->left=current->right;
else if(current->left!=NULL && current->right==NULL && current->key>prev->key)
prev->right=current->left;
else if(current->left==NULL && current->right!=NULL && current->key>prev->key)
prev->right=current->left;
else if(current->left!=NULL && current->right!=NULL)
{
    check=current->right;
    check1=check;
    while(check->left!=NULL)
    {
    check1=check;
    check=check->left;
    }
    *current=*check;
    check1->left=NULL;
}
最佳回答

你们可以保持比这更加简单得多的距离,并且简单地将 yourself局限于从《生物物理法》中删除一条子(二元搜索树)时的三个案例:

  1. a node without children (a leaf) : just remove it - nothing special needs to be done
  2. a node with one child : remove it, and move the child in its place
  3. a node with two children : swap it with either its in-order predecessor or successor, and then remove it

wiki page就是一个例子,说明如何从代码上看。

或作为C的一个非常基本的例子:

if (current->left==NULL && current->right==NULL) {
    /* leaf node */
    bst_replace(current, NULL);
}
else if (current->left==NULL || current->right==NULL) {
    /* node with one child */
    bst_replace(current, ((current->left) ? current->left : current->right));
}
else {
    /* node with two children */
    Node* successor = bst_next(current);
    current->data = successor->data;
    bst_replace(successor, successor->right);
}
问题回答

I don t really understand the protocol used for deleting here. You seem to not have a binary search tree (no ordering in the tree).

但是,仅仅使守则简单明了。 你可以这样做:

bool b1 = (current->left == NULL);
bool b2 = (current->right == NULL);
bool b3 = (current->key > prev->key);

int decision_case = b1 * 4 + b2 * 2 + b3;

switch(decision_case) {
  case 0: // fill in code here
          break;
  ...
  ...
  case 7: // fill in code here
          break;
}

Also, you should use delete to avoid memory leaks here. Hope that helps.

删除一个民族解放军协调人没有任何影响。 因此,你应该能够这样做,不出现特殊情况。 基本内容是:

delete current->left;
delete current->right;




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签