Skip to content

Immutable N-ary tree? #159

Answered by vitoke
ornamentist asked this question in Q&A
May 3, 2023 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

I'm not sure whether a Graph is the best fit for an n-ary tree. The thing is that in a rimbu Graph, each node is unique. That means that each value could appear only once in the entire tree. Usually, for a tree, that is not the case. If you really want to use a Graph, you could consider creating unique invisible nodes, and store the tree values in the edge values, but that can become quite complex.

Depending on your use case, I would expect a recursive data structure using a map-like structure, for example:

import { HashMap, type RMap } from "@rimbu/core";

// Recursive tree type
type NTree<T> = RMap<T, NTree<T>>;

// example instances
const emptyTree: NTree<number> = HashMap.empty();
const 

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ornamentist
Comment options

Answer selected by ornamentist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants