javascript - JS中的树状数据结构允许最快的节点查找?

标签 javascript arrays json oop data-structures

我将如何在 JS 中创建树状数据结构,在那里,我可以访问诸如对父节点的引用、基于 id 的节点查找、访问子节点的长度(数量)、基于索引的内容查找等?

这基本上是我设想的 API:

var rootNode = DataStructure.getRoot();
var child1 = rootNode.addNode('child1'); //added a node with id 'child1'
child1.addNode('innerChild1');
child1.addNode('innerChild2');
rootNode.getChildById('child1') //should be same node as var child1
rootNode.getAtIndex(0) //should be same node as var child1
child1.parent() //should be rootNode
child1.getAtIndex(0) // should be node with id 'innerChild1'
child1.getAtIndex(1) // should be node with id 'innerChild2'
child1.length() //should be 2 

等..

我理解这是一个广泛的问题,但我想知道是否有人可以推荐一种方法来解决这个问题和/或任何可能已经在做的图书馆?我应该动态创建一个 XML 并使用它的本地方法吗?那会是最快的吗?

最佳答案

您描述的数据结构可以很容易地实现如下:

var Tree = defclass({
    constructor: function (parent) {
        this.parent   = parent || null; // null for root node
        this.children = {};             // for id based lookup
        this.ids      = [];             // for index based lookup
        this.length   = 0;              // for ease of access
    },
    addNode: function (id) {
        var children = this.children;
        if (children.hasOwnProperty(id)) throw new Error(id + " exists");
        return children[this.ids[this.length++] = id] = new Tree(this);
    },
    getChildById: function (id) {
        var children = this.children;
        if (children.hasOwnProperty(id)) return children[id];
        throw new Error(id + " does not exist");
    },
    getAtIndex: function (index) {
        return this.getChildById(this.ids[index]);
    }
});

function defclass(prototype) {
    var constructor = prototype.constructor;
    constructor.prototype = prototype;
    return constructor;
}
<script>
    setTimeout(function () {
        var rootNode    = new Tree;
        var child1      = rootNode.addNode("child1");
        var innerChild1 = child1.addNode("innerChild1");
        var innerChild2 = child1.addNode("innerChild2");

        console.assert(rootNode.getChildById("child1") === child1);
        console.assert(rootNode.getAtIndex(0)          === child1);
        console.assert(child1.parent                   === rootNode);
        console.assert(child1.getAtIndex(0)            === innerChild1);
        console.assert(child1.getAtIndex(1)            === innerChild2);
        console.assert(child1.length                   === 2);

        alert("success");
    }, 0);
</script>

基于 id 的查找和基于索引的查找都采用常量(即 O(1))time .希望对您有所帮助。

关于javascript - JS中的树状数据结构允许最快的节点查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29570744/

相关文章:

json - 如何使用 Codable 类型定义变量?

javascript - 如何将json传递到fabric ui下拉组件中?

javascript - 获取 `url` 的访问被 CORS 策略 : No 'Access-Control-Allow-Origin' header is present on the requested resource. ReactJS 阻止

c - 使用 malloc 和 free 理解指针

javascript - 将 JS FileReader 与 FormData 结合使用

java - 如何从文本文件中读取并存储到java中的数组中

javascript - 如果我的 props 在 React 中发生变化,如何更新状态以强制重新渲染?

javascript - 使用 Javascript 读取 URL 中显示的 JSON 字符串

Javascript -> 下载以 ISO-8859-1/Latin1/Windows-1252 编码的 CSV 文件

javascript - 上传一个文件时 Req.files.file.length 未定义