简体   繁体   English

如何将随机值数组输入到二叉搜索树算法中

[英]How to input an array of random values into a binary search tree algirithm

I have an array with randomly generated numbers and I have to input them into a binary search tree algorithm, then to output them and visually represent it. 我有一个带有随机生成数字的数组,我必须将它们输入到二进制搜索树算法中,然后输出它们并直观地表示它。 Here is the code : 这是代码:

//buttons and input
range:<input type="text"  id="input1">
lenght:<input type="text"  id="input2">
<input type="submit" value="Submit" onclick="javascript:myJsFunction()">
<script>
//random number generator
function myJsFunction(){
    var x=document.getElementById('input1').value;
    var n=document.getElementById('input2').value;
    var data=[];

    for (var i = 0; i < n ; i++){
        data[i]=Math.floor(Math.random()*x);
    }
}


//binary search tree algorithm
class Node {
    constructor(data, left = null, right = null) {
        this.data = data;
        this.left = left;
        this.right = right;
     }
}

class BinaryTree {
    constructor() {
        this.root = null;
    }

    add(data) {
        const node = this.root;
        if (node === null) {
            this.root = new Node(data);
            return;
        } else {
            const searchTree = function(node) {
                if (data < node.data) {
                    if (node.left === null) {
                        node.left = new Node(data);
                        return;
                    } else if (node.left !== null) {
                        return searchTree(node.left);
                    }
                } else if (data > node.data) {
                    if (node.right === null) {
                        node.right = new Node(data);
                        return;
                    } else if (node.right !== null) {
                        return searchTree(node.right);
                    }
                } else {
                     return null;
                }
            };

            return searchTree(node);
        }
    }
}
</script>

I need a simple visual representation, something like sticks from the root and nodes (left-right). 我需要一个简单的视觉表示,例如从根和节点(左右)伸出的棍子。 I couldn't find anything on array to binary tree or proper visualization so i would require some help. 我找不到二进制树或适当的可视化的数组上的任何内容,所以我需要一些帮助。

It seems that the concept of children is missing from your tree. 您的树似乎缺少了儿童的概念。 I believe you need to initialize an empty array for storing children and assign it the children property. 我相信您需要初始化一个空数组来存储孩子并为其分配children属性。

class Node {
    constructor(data){
        this.data = data;
        this.children = [];
    }

    add(data) {
        this.children.push(new Node(data));
    }

    remove(data) {
        this.children = this.children.filter(node => {
            return node.data !== data;
        });
    }

}

class Tree {
  this.root = null;
}

So I created a new node inside the add method with data. 因此,我在add方法中使用数据创建了一个新节点。 Then take a newly created node and push it to the current nodes children array. 然后获取一个新创建的节点,并将其推入当前节点的childs数组。

So we initialized our data variable , created an empty array of children so every single node has this children array so every child can have some number of children. 因此,我们初始化了数据变量,创建了一个空的子代数组,因此每个节点都具有该子代数组,因此每个子代可以具有一定数量的子代。

The add and remove method belong to every node so we could as easily have called add onto either one of these children nodes, this is how we can build out our tree structure. add和remove方法属于每个节点,因此我们可以轻松地将add调用到这些子节点之一上,这就是我们可以构建树结构的方式。

So when you first create a tree its going to start off with an empty root property. 因此,当您第一次创建树时,它将从一个空的root属性开始。

Finally, you have to implement your traverse methods, the breadth-first in your case since you mention going from left to right and all you have to do is implement this: 最后,您必须实现遍历方法,因为您提到的是从左到右,所以要实现宽度优先,而要做的就是实现此方法:

traverseBF(callback)

As far as a visual diagram just draw a pyramid of boxes with arrows pointing from the first single box at the top of the pyramid on down representing parent to child nodes and then another set of arrows for each row of boxes pointing towards the right, indicating left to right. 就可视化图而言,只需绘制一个金字塔框,其中箭头从金字塔顶部的第一个单个框指向下方,向下代表父节点到子节点,然后针对每一行框向右指向另一组箭头,指示左到右。 You can use a tool like draw.io. 您可以使用诸如draw.io之类的工具。

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

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