在我的二叉搜索树中删除某些内容时出现问题

时间:2018-05-16 17:28:42

标签: javascript

这是我制作的二叉树。

问题:当我删除数据时,从左到右时它不会停止运行,因此它只会向一个方向移动。当它只删除所有Left时,它会正确删除,而且只删除所有Right。

运行正常:

BST.insert(10);
BST.insert(9);
BST.insert(8);
BST.insert(7);
BST.insert(6);
BST.insert(5);
BST.insert(4);
BST.Delete(4);

BST.insert(10);
BST.insert(11);
BST.insert(12);
BST.insert(13);
BST.insert(14);
BST.Delete(14);

但是如果你运行它,它会像循环中的所有条件一样停留在循环中

BST.insert(10)
BST.insert(5)
BST.insert(8)
BST.insert(7)
BST.insert(6)
BST.insert(5)
BST.Delete(6)

您可以自己尝试一下:

var BST = (function(){
  // PRIVATE
  var root = null;

  // NODE OBJECT
  var Node = function (data, left, right) {
    this.data  = data;
    this.left  = left;
    this.right = right;
  }    

  // PUBLIC
  return {
    insert: function(data) {
      const node = new Node(data,null, null);

      if (root === null) {
        root = node; 
      } else {
        let current  = root;

        while (true) {
          if (current.data > data) {
            if (current.left === null) {
              current.left = node;
              console.log(root+ "  1");
              break;
            }

            current = current.left;
          } else {
            if (current.right === null) {
              current.right = node;
              console.log(root+ "  2");
              break;
            }
            current = current.right;
          }
        }
      }
    },

    Delete: function(data) {
      if (root === null) {
        return console.log("Nothings in Tree");
      } else if (root.data === data ) {
        root = null;
        return console.log("found values");
      }

      // condition    
      let current = root;

      while (true) {
        //CHECK If a value exist to the left or right
        if (current.data > data) {
          if (current.left === null) {
            return console.log("Could not find value");
          }
        } else if (current.data < data) {
          if (current.right === null) {
            return console.log("Could not find value");
          }
        }

        // if left exist
        if(current.left !== null){
          if (current.left.data === data) { // if found data
            console.log(`${data} has been remove from the node`);
            current.left = null;
            break;
          } else if (current.left.data > data) {
            //MOVE to the left==============
            console.log(current.left.data+ " .Great");          
            current = current.left;// moves to the left
          }
        }

        if (current.right !== null) {
          if (current.right.data === data) {
            console.log(`${data} has been remove from the node`);
            current.right = null; 
            break;          
          } else if (current.right.data < data) {
            //MOVE to the right==============
            console.log(current.right.data +".Less");
            current = current.right;
          }     
        }       
      }
    },

    show: function() {
      console.log(root);
    },

    test: function() {
      console.log("test is working");
    }
  }    
})();

1 个答案:

答案 0 :(得分:1)

看来我已经过度了。

 // if left exist
    if(current.left !== null){
      if (current.left.data === data) { // if found data
        console.log(`${data} has been remove from the node`);
        current.left = null;
        break;
      } else if (current.left.data > data) {
        //MOVE to the left==============
        console.log(current.left.data+ " .Great");          
        current = current.left;// moves to the left
      }
    }

“if if(current.left.data&gt; data)”应该替换为与右侧相同的“else”。答案如下。

var BST = (function(){
            // PRIVATE
            var root = null;

            // NODE OBJECT
            var Node = function (data,left,right){
            this.data   = data;
            this.left   = left;
            this.right  = right;
            }

            /*function currentToNull(current, data){
                if(current === null){ // repeated operation
                    current = data;
                }
            }*/


    // PUBLIC
    return {

        insert: function(data){
                const node = new Node(data,null, null);

                if(root === null){          
                    root = node; 
                }else{

                    let current  = root;
                    while(true){
                    if(current.data > data){
                                if(current.left === null){
                                    current.left = node;
                                console.log(root+ "  1");
                                break;
                                }
                                current = current.left;
                            }else{
                                if(current.right === null){
                                    current.right = node;
                                console.log(root+ "  2");
                                break;
                                }
                                current = current.right;
                            }
                    }
                }           
        },
        Delete: function(data){
                if(root === null){
                    return console.log("Nothings in Tree");
                }
                // condition        
                let current = root, right_leaf, left_leaf;
                    while(true){

                    right_leaf = current.right; // keep track of left
                    left_leaf =current.left;    // keep track of right


                        //CHECK If a value exist to the left or right
                        if(current.data > data){
                                if(left_leaf === null){
                                    return console.log("Could not find value");
                                }
                        }else if(current.data < data){
                                if(right_leaf === null){
                                    return console.log("Could not find value");
                                }
                        } else if(current.data === data){
                            current = null;
                            return console.log("data found");
                        }



                        if(left_leaf !== null){// L Leaf Data exist
                                    if(left_leaf.data === data){ // if found data
                                        console.log(`${data} has been remove from the node`);
                                        current.left = null;
                                        break;
                                    }else if(left_leaf.data > data){ //MOVE to the left======================
                                            console.log(" Move Left of left");                  
                                            current = current.left;// moves to the left
                                    } else if(left_leaf.data < data ){
                                            console.log(" Move Right of left"); 
                                            current = left_leaf;
                                    }                           
                        }

                        if (right_leaf !== null){
                                    if(right_leaf.data === data){ 
                                        console.log(`${data} has been remove from the node`);
                                        right_leaf = null;  
                                        break;                  
                                    }else if(right_leaf.data < data){   //MOVE to the right=======================          
                                                console.log(" Move Right of right"); 
                                                current = right_leaf;
                                        } else if(right_leaf.data > data ){
                                                console.log(" Move Left of right");
                                                current = right_leaf;

                                        }   
                        }



                    }
        }

        ,
        show: function(){
            console.log(root);
        },
        test: function (){
            console.log("test is working");
        }
    }

})();

//show();