bitwalker / libgraph

A graph data structure library for Elixir projects
MIT License
524 stars 75 forks source link

Add stronger support for trees #71

Open apreifsteck opened 1 year ago

apreifsteck commented 1 year ago

Proposal

Add a dedicated module to working with Graphs that are Trees.

Details

Rationale

The Graph module provides some very excellent utilities for working with graphs, but is somewhat bare on the tree side of things. I had some use cases come up where a dedicated tree data structure would come in handy. The code that's outlined here is a pretty faithful replica of what I rolled at work to remediate that need. The main use case that I've had for this is creating trees of atoms that can be expanded to serve as a key list for a data structure that implements the access behaviour.

For example, here's the implementation of a function we've used to make sure a particular set of values in a large, deep map are non-empty:

    def all_present?(%{} = data, %Tree{} = tree, empty_values) do
    Tree.reduce(tree, true, fn %Tree.Node{data: field} = node, path_stack, all_present ->
      if Tree.is_leaf?(tree, node) do
        data =
          [field | Enum.map(path_stack, & &1.data)]
          |> Enum.reverse()
          |> then(&get_in(data, &1))

        if data in empty_values do
          {:halt, false}
        else
          {:next, all_present}
        end
      else
        {:next, all_present}
      end
    end)
  end

I think it was actually this function that lead me to write in the leaf_reduce function. This one never got refactored though.

I'm happy to make any changes or additions to it, or release it as it's own package if that's your preference. Some of what in here is probably not very consistent with the API, I know. I just didn't want this code to sit locked inside our application any more 👍 .