简体   繁体   English

你能在二叉搜索树的构造中发现一个错误吗?

[英]Can you spot a mistake in this construction of a Binary Search Tree?

I am going through AlgoExpert and learning about Binary Search trees and their construction for the first time.我正在通过 AlgoExpert 第一次学习二叉搜索树及其构造。 The below implementation seems to be working locally for me as expected but I am getting errors for most test cases on AlgoExpert.下面的实现似乎按预期对我来说在本地工作,但我在 AlgoExpert 上的大多数测试用例都遇到错误。 This is my implementation:这是我的实现:

class BST {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }

  insert(value) {

    const searchTree = (node) => {
      if (value < node.value) {
        if (!node.left) {
          node.left = new BST(value);
        } else {
          searchTree(node.left);
        }
      } else {
        if (!node.right) {
          node.right = new BST(value);
        } else {
          searchTree(node.right);
        }
      }
    };
    searchTree(this);
    // Write your code here.
    // Do not edit the return statement of this method.
    return this;
  }

  contains(value) {
    let currentNode = this;
    while (currentNode) {
      if (currentNode.value === value) {
        return true;
      }
      if (value < currentNode.value) {
        currentNode = currentNode.left;
      } else {
        currentNode = currentNode.right;
      }
    }
    return false;
    // Write your code here.
  }

  min() {
    let currentNode = this;
    while (currentNode.left) {
      currentNode = currentNode.left;
    }
    return {
      value: currentNode.value,
      node: currentNode
    };
  }

  remove(value) {
    let nodeToBeRemoved = this;
    while (nodeToBeRemoved.value !== value) {
      if (value < nodeToBeRemoved.value) {
        nodeToBeRemoved = nodeToBeRemoved.left;
      } else {
        nodeToBeRemoved = nodeToBeRemoved.right;
      }
    }
    if (!nodeToBeRemoved.right) {
      nodeToBeRemoved.value = null;
    } else {
      const {
        value: minValue,
        node
      } = nodeToBeRemoved.right.min();
      node.value = null;
      nodeToBeRemoved.value = minValue;
    }
    // Write your code here.
    // Do not edit the return statement of this method.
    return this;
  }
}

Can you see any mistake that could cause this error on Algo Expert?您能在 Algo Expert 上看到任何可能导致此错误的错误吗? I don't see where the mistake is.我不明白错误在哪里。 This is the error I am getting:这是我得到的错误:

 Cannot read property 'toString' of null TypeError: Cannot read property 'toString' of null at constructBinaryTreeWithUniqueIds at constructBinaryTreeWithUniqueIds...

The remove method has some issues: remove方法有一些问题:

  • After the first while loop, nodeToBeRemoved could be null .在第一个while循环之后, nodeToBeRemoved可能是null In that case the next if statement will make an invalid reference with nodeToBeRemoved.right .在这种情况下,下一个if语句将使用nodeToBeRemoved.right进行无效引用。
  • nodeToBeRemoved.value = null does not remove a node. nodeToBeRemoved.value = null不删除节点。 It just modifies the value of a node to null .它只是将节点的值修改为null But it is still part of the tree, and could even have a left child.但它仍然是树的一部分,甚至可能有一个左孩子。
  • The same is true for node.value = null . node.value = null也是如此。 This remains part of the tree and could have a right child.这仍然是树的一部分,并且可能有一个正确的孩子。
remove(value) {
    let nodeToBeRemoved = this;
    if (value < this.value) {
        if (this.left) this.left = this.left.remove(value);
    } else if (value > this.value) {
        if (this.right) this.right = this.right.remove(value);
    } else if (!this.right) {
        return this.left;
    } else if (!this.left) {
        return this.right;
    } else {
        const minValue = this.right.min().value;
        this.right.remove(minValue); // use recursion to delete that min-node
        this.value = minValue;
    }
    return this;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM