为什么删除根节点会保留左侧的子树而不保留右侧的子树?

时间:2018-11-09 21:26:36

标签: java binary-search-tree

因此,我为BST实现创建了一个删除功能。它适用于删除叶节点和带有右子级但没有左子级的子树的情况,反之亦然。

但是,删除根节点给我带来了问题。我试图用其继任者替换根,然后将新根的正确子级设置为等于旧根的正确子级。

示例:

        50
       /  \
      /    \
     42     63
    / \    /
   /   \  55 
  21   43      
    \    \    
    23    \    
   / \    47
  /   \
 44   48   

如果我要在程序中删除节点50,则55将是新的根节点,但是63将会消失,但是其他一切都很好。

我重新链接节点错了吗? (从代码中的“问题在这里” 开始)

//How I display my tree
//inprder traversal
private void inOrder(Node curr){

      if(curr != null){
         //traverse left
         inOrder(curr.leftChild);

         System.out.print("\n" + curr + "->");

         //traverse right
         inOrder(curr.rightChild);
      }

   } 

public boolean deleteContact(String key){
      //Start at root node
      Node currentNode = root;
      Node parent = root;

      boolean isLeftChld = true;
      //loop over left and right subtrees
      //finds node to be deleted
      while(!key.equals(currentNode.key)){

         parent = currentNode;

         if(key.compareToIgnoreCase(currentNode.key) < 0){

            isLeftChld = true;
            currentNode = currentNode.leftChild;

         }else{

            isLeftChld = false;
            currentNode = currentNode.rightChild;

         }
         //Node never found
         if(currentNode == null){
            return false;
         }
      }

      //if the node doesnt have children
      //go ahead and delete
      if(currentNode.leftChild == null && currentNode.rightChild == null){
         if(currentNode == root){

            root = null;
         //Handle case of juss deleting leafs
         }else if(isLeftChld){//check if it was a left or right child
            //delete it
            parent.leftChild = null;

         }else{

            parent.rightChild = null;

         }

      //situation of no right child
      //handling node dissapearing
      } else if(currentNode.rightChild == null){
         if(currentNode == root){

            root = currentNode.leftChild;
         }else if (isLeftChld){

            parent.leftChild = currentNode.leftChild;

         }else{

            parent.rightChild = currentNode.leftChild;
         }

      //situation of no left child
      }else if(currentNode.leftChild == null){
         if(currentNode == root){

            root = currentNode.rightChild;

         }else if (isLeftChld){

            parent.leftChild = currentNode.rightChild;

         }else{

            parent.rightChild = currentNode.leftChild;
         }
       **Problem is Here**
      //situation of two children being involved
      //figure out which node will replace which
      } else {
         //Node temp = currentNode.rightChild;
         Node successor = minValue(currentNode.rightChild);

         if(currentNode == root){
            root = successor;

         }else if(isLeftChld){
            parent.leftChild = successor;

         }else{
            parent.rightChild = successor;
         }

         root.leftChild = currentNode.leftChild;

      }

      return true;

   }

   //Function to return min value for a root node
   //Used for delete function - finds succesor
   private Node minValue(Node root)
    {
        Node minv = root;
        while (root.leftChild != null)
        {
            minv = root.leftChild;
            root = root.leftChild;
        }
        return minv;
    }

1 个答案:

答案 0 :(得分:1)

您还需要分配合适的孩子。

// const startPosition = window.pageYOffset;
const startPosition = container.scrollTop;

// window.scrollTo(0,run);
container.scrollTo(0,run);

缺少以下行:

     //Root is 50 [42, 63]

     if(currentNode == root){
        root = successor;

     //Root is 55 [null, null]
     }else if(isLeftChld){
        parent.leftChild = successor;

     }else{
        parent.rightChild = successor;
     }

     //Root is 55 [42, null]
     root.leftChild = currentNode.leftChild;