簡體   English   中英

JS中的樹狀數據結構允許最快的節點查找?

[英]Tree-like data structure in JS that allows for fastest node lookup?

我將如何在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)時間 希望有所幫助。

我在應用程序中有這樣的結構。 指定的API會使您難以創建所需的結構。 以下是我注意到的一些事情: DataStructure是單例, addChild不允許您添加addChild節點的節點,索引是數字。 以下怎么樣?

API

TreeNode(newable)

成員:

  • children(對象,在ID上建立索引)
  • root(TreeNode)
  • parent(TreeNode)
  • index(String)
  • 屬性(對象)
  • _referenceCount(int) - 如果您的樹由於某種原因而具有循環,則非常有用

方法:

  • addChild(TreeNode:treeNode)
    • 使用父級注冊節點
    • 按ID將節點添加到子節點
    • 增加TreeNode的引用計數
  • forEach(callback(TreeNode:treeNode))
  • removeChild(String:id)
    • 從此Object中刪除節點
    • 減少指定孩子的_referenceCount
    • 如果_referenceCount為0,則從根目錄中刪除它

樹(擴展TreeNode)(新增)

成員:

  • _nodeMap(Object)

方法:

  • getNodeByID(String:id)
    • 在_nodeMap中查找id,O(1)查找時間
  • removeNode(String:id)
  • addToNodeMap(TreeNode:treeNode)

treeFactory(可選)

方法:

  • createFromFormatX(對象:某種特定格式的東西樹)樹工作正常,你應該為你的特定情況創建一個工廠,幫助你將blob轉換成你喜歡的數據結構。
  • createFromFormatY,基本上是另一個加載器機制

潛在的不變性

這里沒有什么是不可改變的。 但是,您可以通過使用新的工廠方法並使上述方法中的更多內容始終強制樹的不變性。 這可能是也可能不是所希望的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM