简体   繁体   English

Javascript参考

[英]Javascript References

I'm building out a Binary Search Tree in javascript for some algorithm / data structures practice. 我正在用javascript构建二进制搜索树,以进行一些算法/数据结构练习。 I ran into an interesting scenario when implementing delete() , and I'm curious if there is a better way to go about solving this. 当实现delete() ,我遇到了一个有趣的场景,我很好奇是否有更好的方法来解决这个问题。 It really has nothing to do with binary trees, but here are the barebones that you need to know to understand my problem: 它确实与二叉树无关,但是这里是您了解我的问题所需的准系统:

function innerDelete(node, parent) {
            switch (node.countChildren(node)) {
                case 0:
                  ...
                case 1:
                    if (node.left)
                        node = node.left;
                    else
                        node = node.right;
                  ...

Props to anyone who can see the problem immediately. 向可以立即看到问题的任何人提供支持。 This tree is composed of Node objects each with the properties { value, left, right } . 该树由Node对象组成,每个对象具有属性{ value, left, right } This deletion method is updating objects directly so I don't have to manage updating parent references to child nodes. 这种删除方法是直接更新对象,因此我不必管理对子节点的父引用的更新。 The problem is that reassigning node during node = node.left or node = node.right is only reassigning the local reference to this variable, and not the actual node object. 问题在于,在node = node.leftnode = node.right期间重新分配节点仅是将本地引用重新分配给此变量,而不是实际的node对象。 Is there a way to directly update the object itself? 有没有办法直接更新对象本身? Alternatives I've come up with are a little ugly, so I hope there's a better way: 我提出的替代方案有些丑陋,所以我希望有更好的方法:

var branch = (parent.left == n ? 'left' : 'right');
if (node.left)
  p[branch] = node.left;
else
  p[branch] = node.right;

The problem is that reassigning node during node = node.left or node = node.right is only reassigning the local reference to this variable, and not the actual node object 问题在于,在node = node.left或node = node.right期间重新分配节点仅是将本地引用重新分配给此变量,而不是实际的节点对象

That's because the value node is a reference, so it's not that you're changing a local copy of node , you're changing the reference. 那是因为value node是一个引用,所以并不是要更改node的本地副本,而是要更改引用。 Therefore, you're not actually changing the object itself. 因此,您实际上并没有更改对象本身。

A cleaner approach for updating trees is to use recursion, with each recursive call updating the left and right nodes as they are returned. 更新树的更干净方法是使用递归,每个递归调用在返回左节点和右节点时都会对其进行更新。

For example: 例如:

root = delete(root, target);

function delete(node, target) {
    // Not found
    if (node === null) return null;
    // Found
    if (node.value === target) {
        // If there's children
          // If has both left and right child
            var newNode = minimum(node.right);
            deleteMin(node.right);
            newNode.left = node.left;
            newNode.right = node.right;
            return newNode;  
        // Else
            return null;
    }
    // Update
    node.left = delete(node.left, target);
    node.right = delete(node.right, target);
    return node;
}

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

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