53

Are there good libraries for manipulating trees in javascript? Just to be clear, I am looking for tree as in data structure not display model.

tallowen
  • 4,198
  • 7
  • 27
  • 35

6 Answers6

38

Here are some libraries that you may find helpful:

arboreal.js, a "micro-library for traversing and manipulating tree-like data structures" in node.js and the browser.

buckets, a "complete, fully tested and documented data structure library" that includes BSTs, a heap, and a bunch of other goodies.

ty.
  • 10,924
  • 9
  • 52
  • 71
  • 1
    Too bad arboreal doesn't seem to maintained anymore, 3 pull requests waiting for 2 years now. buckets looks real good but appart from bst it does not really provide tree data structures – Overdrivr Dec 02 '15 at 21:51
  • 2
    TreeModel as indicated by @JNS seems to be the best option. – Faisal Mq Nov 23 '16 at 07:04
  • 1
    On a project I worked on for the Rwandan NGO Solid Africa, tree structure was a important part of keeping track of expenses and donations (your expense or donation belonged to a category, food, special care etc.). Based on this experience I developed the [tree-util node package](https://www.npmjs.com/package/tree-util). I hope it can be helpfull when you want to work with tree structures. Especially if you relate data to your tree structures. – Kristian Abrahamsen Nov 28 '16 at 21:12
  • have a look at https://stackoverflow.com/a/58136016/965921 for a custom solution, – Kivylius Dec 05 '19 at 12:36
32

Two libraries to add to the list are:

  • t.js - Seems to be good for traversal;
  • TreeModel - Manipulation and traversal.

disclaimer: I built TreeModel

jnuno
  • 711
  • 5
  • 8
6

Try the DOM

var o = document.createElement( 'WHAT_YOU_WANT' )
// o.appendChild( ... )
  • 13
    Please add some explanation of why this code helps the OP. This will help provide a good answer. See [answer] for more information. – Heretic Monkey Jan 15 '15 at 22:29
  • 7
    I do not disagree with this answer. The DOM is a tree strucure, compatible with XML and JSON. Removing nodes, adding children, children containing children. Add a little CSS and you can easily a good looking treeview structure. Furthermore the question does not specify the widget appearance they want. – John Smith Jan 26 '15 at 19:21
  • This is a good answer, why is this downvoted? – John Smith Aug 07 '15 at 21:57
  • 1
    not all trees live in the browser, though it would be nice to be able to use a tried and tested implementation. – Bet Lamed Jan 28 '16 at 13:59
  • 1
    Not a good answer, it is not a generic tree structure and relies on a specific implementation with specific side effects. – Cranio Mar 10 '17 at 14:48
  • I don't think DOM can be considered as a JavaScript data structure. But I upvoted this answer since I like those who think outside the box. – pouya Jan 02 '22 at 20:25
5

Wish I'd seen that solution before, because those libraries look helpful!

Here is one I put together which is an alternative. Meant for traversal, manipulation and loading of hierarchical tree structures from self referencing flat tables, not for balanced binary trees.

DataStructures.Tree and related blog post

Stephen James
  • 1,277
  • 9
  • 17
5

I am not sure what your needs are so this is a shot in the dark. I normally use (for lightweight Javascript) an array of arrays, like this:

node[i] = [parent, firstChild, secondChild, ... nthChild];
AturSams
  • 7,568
  • 18
  • 64
  • 98
2

I wrote one simple data oriented tree library called data-tree. You can use this to create, traverse and search tree in BFS/DFS fashion. You can also import/export data from tree. Checkout the detailed documentation at: http://cchandurkar.github.io/Data-Tree/

To use it in a node npm install data-tree

Chaitanya Chandurkar
  • 2,142
  • 3
  • 24
  • 43
  • Recommendation requests for off-site resources or tools are off-topic on Stack Overflow. If you answer them, you specifically reinforce the belief that Stack Overflow is a good place to answer those questions. It is not. Please don't answer these questions even if you know a good answer as most answers will be highly opinionated ("I personally like..."). You can open the flag dialogue on the question and see the close reason in full under the **off-topic** category, or in the [help/on-topic]. You should also not even answer in a comment, as the effect is similar to an actual answer. – Kyll Jan 27 '16 at 18:58
  • @Kyll, thanks for bringing it to my attention. I don't want to reinforce any such belief. Would it be appropriate if I delete my answer? – Chaitanya Chandurkar Jan 27 '16 at 19:57