lalo2302

lalo2302

Represent a tree with specific constraints with processes. Too much memory?

Hi everyone.

I am working on a personal project where I need to represent a horizontal tree in a visual way for a user to interact with it, like shown at FIG. 1.

FIG. 1

I have thought about representing it as a list of lists as it usually goes, but I have this constraints:

  • I need to increase the depth of the tree at user request
  • The content of the new nodes (at depth expansion) depends solely on the leafs of the tree

To do this, I need to know the content of each leaf every time I want to increase its depth, and transversing the lists every time seems unnecessary and expensive. With other programming languages I could store the memory reference of each leaf at creation time, but with elixir this is not possible.

It came to my mind the possibility to make every node a process, having a “tree manager” that stores the PID of the root and the PIDs of each leaf. Leading to interact directly with the leafs at depth expansion without interacting with the rest of the nodes. But my lack of experience doesn’t let me know if this is unnecessary memory usage, seeing that a process uses 338 words when spawned, including a heap of 233 words compared with other data structures.

The tree is not very wide, but can increase its depth at user request. Maybe I can set a limit to change the root of the tree for one closer to the leafs every time the user passes it.

What do you guys think?

Marked As Solved

OvermindDL1

OvermindDL1

I would just represent it as a map of maps, or if you really need efficiency with querying and such then perhaps a :graph (the BEAM comes with a graph module for graph data structures).

Also Liked

kokolegorille

kokolegorille

That might be a good use case to test neo4j.

OvermindDL1

OvermindDL1

Easier to do positional lookup primarily, but there is also the idea that you could encode the path as keys and just flatten the whole tree into a single map (at which point if you need full speed then you could lift that into ETS itself later on). :slight_smile:

kokolegorille

kokolegorille

With FP You can use a [zipper](https://en.wikipedia.org/wiki/Zipper_(data_structure) to traverse the tree.

@OvermindDL1 could explain what a zipper is much better than I might.

OvermindDL1

OvermindDL1

Hehe, I love zippers. Even in a flat encoding you can keep a special data structure to hold a fresh zipper to start iterations from if you need. ^.^

lalo2302

lalo2302

Wow.

but there is also the idea that you could encode the path as keys and just flatten the whole tree into a single map

Didn’t thought about that, thank you a lot for the suggestion. At the beginning I was thinking on creating the tree first, and then transverse it to “draw” it on the browser with dot. But I could draw it on the go and at the same time save the tree as a flat map. So if the user wants to access a node, or expand the tree, I can access directly to its value on the map. :thumbsup:

Where Next?

Popular in Questions Top

openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
sergio
I couldn’t find any guides that worked well with Phoenix 1.6.0 and esbuild. I hope this helps people test the waters and eases you into t...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New

We're in Beta

About us Mission Statement