sschuldenzucker

sschuldenzucker

Hey all, I’m building my own little app (side project) and I’m looking for a tree data structure. I’m honestly wondering if I’m overlooking something but I couldn’t find a library that’s right for me.

The whole thing should be “generic” (think, like a DOM), i.e., like this:

  1. Arbitrary number of children per node.
  2. Children are ordered.
  3. Easy to traverse. E.g. there should be “get sibling right after this”, “get parent” etc.
  4. Effiiciently find child by node ID (no zipper; I wanna build a microservice that manages the tree)

I could use a digraph from Erlang but it’s actually too flexible for my use case and would require a wrapper around it to make sure nobody’s creating a loop.

In particular 5. seems hard to satisfy with the libraries I found.

Do you guys agree that there’s currently no library that does this? Any advice on how I would build this without going crazy?

Thanks a lot!

Showing Posts 1 to 5

al2o3cr

al2o3cr

Not sure I’m following the problem here - you can pass :acyclic to :digraph.new/1 and the library will handle enforcing that restriction.

I only see 4 numbered bullet points, did something get lost in editing?

“Generic” is hard. Different implementations are going to have seriously different performance (both time and space) for specific operations.

For instance, a tree implemented as a closure table can answer queries like “what are all the descendants of this node” very efficiently. But moving a subtree in that implementation is very slow, because it needs to touch many rows for each element of the subtree.

An adjacency-list representation (aka “put a parent_id column on the record”) has the opposite properties: moving an entire subtree only needs to update the parent_id on the subtree’s root, but checking for descendants requires recursive CTEs or other SQL trickery.

sschuldenzucker

sschuldenzucker OP

Hey, thx for the pointers.

Regarding :acyclic: This would definitely make it better but acyclic digraphs can still have undirected cycles (two nodes pointing to the same child), so it’s not enough. I guess you can get around that by restricting the access methods a bit more (e.g., don’t allow adding edges, just moving nodes).

Oh, and in a digraph, the children of a node (aka. the edges going out of a node) are not ordered, right?

Regarding 5., this should’ve been 4. (no zippers).

Regarding “generic”, of course you’re right. I think for my use case, local operations (what’s the parent / next sibling / children / etc.; move this subtree somewhere else) should be enough. So adjacency lists are probably the way to go.

I suppose all of these things are not fundamentally hard, was just wondering if someone already did it.

petrus-jvrensburg

petrus-jvrensburg

Would the Postgres Ltree datatype help?

sschuldenzucker

sschuldenzucker OP

NB I’ve built something based on IDs (essentially doing the usual pointer rotations in a set of maps). Will publish something not too far out.

user20230119

user20230119

Did you publish something?

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
water
I’m aware of 2 such projects, are there others? git: GitHub - team-alembic/realworld: A fullstack Phoenix LiveView application with back...
New
itzmidinesh
Hi all, I’m working on a Phoenix project where I need to make external HTTP requests and I’m trying to choose between Req and HTTPoison....
New
pragdave
How do I tell Hologram not to escape HTML when substituting into templates? {Textile.render(@product.long_description)} escapes the HTM...
New
rlipscombe
I’d like to create a file with 600 permissions (rw-------), but there’s a race condition if I create it and then change the permissions. ...
New
leoschweizer
Hi, we run different LLM models across a cluster, with each node loading a configurable subset. Model-specific queues let nodes consume ...
New
rlipscombe
When I use Mix.install with a git dependency, using git@github.com:... how do I change the SSH identity used? I’ve got two Github accoun...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
raa-ziq
Hi everyone :waving_hand: I wanted to share something I’ve been building — NowBlind, a random chat app designed to help people discover ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New
kip
Localize is the next generation localisation library for Elixir. Think of it as ex_cldr version 3.0. The first version will be released ...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews