简体   繁体   English

在js中获取二叉搜索树的最大值

[英]getting max value of binary search tree in js

I'm following along this JS data structures video from udemy on Binary search trees. 我将跟踪来自udemy的JS数据结构有关二进制搜索树的视频。 We've got a method to find the max value through recursion. 我们有一种方法可以通过递归找到最大值。

I was thinking more of comparing all the numbers such as 我想更多地比较所有数字,例如

BST.prototype.getMaxVal = function() {
  let num = null;
  if (num === null) num = this.value;
  else if (this.value > num) num = this.value;
  if (this.left) return this.left.getMaxVal();
  return num;
}

but the answer is 但是答案是

BST.prototype.getMaxVal = function() {
  if (this.right) return this.right.getMaxVal();
  else return this.value;
}

105 is the last number without its own leaf, but this method finds 107 before it? 105是没有自己的叶子的最后一个数字,但是此方法在它之前找到107? how does it find that without any comparison logic? 在没有任何比较逻辑的情况下如何找到它?

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

BST.prototype.insert = function(value) {
  if (value <= this.value) {
    if (!this.left) this.left = new BST(value);
    else this.left.insert(value);
  } else {
    if (!this.right) this.right = new BST(value);
    else this.right.insert(value);
  }

  return this;
}


const bst = new BST(50); 

bst.insert(30);
bst.insert(70);
bst.insert(107);
bst.insert(60);
bst.insert(59);
bst.insert(20);
bst.insert(45);
bst.insert(35);
bst.insert(85);
bst.insert(105);
bst.insert(10);

bst.getMaxVal();

https://repl.it/repls/NumbYellowgreenPlans https://repl.it/repls/NumbYellowgreenPlans

So this is the visual representation of the BST. 这就是BST的视觉表示。 If some value is less than you, you pass it on the left, and let the left sub-BST to decide where to put it. 如果某个值小于您,则将其传递到左侧,然后让左侧的子BST决定将其放置在何处。 If some value is greater than you, pass it on the right sub-BST and let it to decide where to put the value. 如果某个值大于您,则将其传递到正确的sub-BST上,然后让其决定将值放在何处。

在此处输入图片说明

In this setup, it is guarantee that, on the left most leaf, it must be the smallest value, and on the right most leaf, it contains the greatest value. 在此设置中,可以确保在最左边的叶子上它必须是最小值,而在最右边的叶子上它必须包含最大值。 Therefore, the idea is about, from every BST point of view, either his left tree has nothing, or its value must be smaller than me. 因此,从每一个BST的角度来看,这个想法都是关于他的左棵树什么都没有,或者它的值必须小于我。 So the algorithms writes: 因此,算法写道:

BST.prototype.getMinVal = function() {
  // if left tree is not null, it must be smaller tha me. Return its value
  if (this.left) return this.left.getMinVal();
  // if left tree is null, indicate i'm the smallest available, return me instead.
  else return this.value;
}

Update 1 更新1

There is one thing to note. 有一件事情要注意。 BST is designed to serve the purpose like this. BST旨在满足这样的目的。 Its data, when doing insertion, is structured to avoid the need of whole tree traversal. 在进行插入时,其数据的结构避免了遍历整个树的需要。 Its value is well ordered so you don't have to traverse every node when finding a min/max value. 它的值是有序排列的,因此查找最小值/最大值时不必遍历每个节点。 If your algorithms needs to, you are not using it correctly, even the function produce the correct logical output. 如果您的算法需要,则说明您没有正确使用它,即使该函数也会产生正确的逻辑输出。

By definition an in-order-traversal of a BST will return the sorted values. 根据定义,BST的按顺序遍历将返回排序后的值。 The insert() did the comparison and enforced this logic. insert()进行比较并执行此逻辑。

An in-order-traversal is equivalent to scanning the nodes from left to right (you can try this manually). 按顺序遍历等效于从左到右扫描节点(您可以手动尝试)。 We are not focusing on the leaf nodes. 我们不关注叶节点。 Anything in the left subtree of the 107 node (where 105 resides) are smaller than 107. 107节点(105所在的位置)的左子树中的任何内容都小于107。

Here's your BST: 这是您的BST:

{
    "value": 50,
    "left": {
        "value": 30,
        "left": {
            "value": 20,
            "left": { "value": 10, "left": null, "right": null },
            "right": null
        },
        "right": {
            "value": 45,
            "left": { "value": 35, "left": null, "right": null },
            "right": null
        }
    },
    "right": {
        "value": 70,
        "left": {
            "value": 60,
            "left": { "value": 59, "left": null, "right": null },
            "right": null
        },
        "right": {
            "value": 107,
            "left": {
                "value": 85,
                "left": null,
                "right": { "value": 105, "left": null, "right": null }
            },
            "right": null
        }
    }
}

See here for more on BST: 有关BST的更多信息,请参见此处:
VisuAlgo - Binary Search Tree, AVL Tree VisuAlgo-二进制搜索树,AVL树

So, if I understand your question correctly, the answer is that, because of the way the binary tree is structured, the getMaxVal and getMinVal methods simply need to go as far to the right or left (respectively) as possible in order to find the correct value. 因此,如果我正确理解了您的问题,答案是,由于二叉树的结构方式,getMaxVal和getMinVal方法只需要尽可能地分别向右或向左移动即可找到正确的值。 If you look at the insert method, the comparison is already "baked in" to the structure of the "tree" that is created by the series of calls to that method. 如果您看一下insert方法,则该比较已经“融入”了由对该方法的一系列调用所创建的“树”的结构。 When we call insert on 105, it will ultimately be placed on in the "right" property of 85, which is itself the "left" property of 107. The getMaxVal function simply takes advantage of the fact that the insert method ensures that no value lower than the greatest value can be inserted to the right of that value. 当我们在105上调用insert时,它将最终放置在85的“ right”属性中,该属性本身就是107的“ left”属性。getMaxVal函数只是利用了insert方法确保没有值的事实。低于最大值的值可以插入到该值的右边。 In fact, the greatest inserted value at any point in time will never have anything to it's right, so we can just traverse down the tree to the right until we reach a value that doesn't have a "right" property, and we know that that is the max value in the tree. 实际上,在任何时间点上最大的插入值永远都不会是正确的,因此我们可以向右遍历树,直到达到没有“正确”属性的值为止,并且我们知道那是树中的最大值。

Hope that helps! 希望有帮助!

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

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