<turbo-stream action="append" target="posts_list"><template>    <div class="postbit" id="181968" data-post-id="181968">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="bottlenecked" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/bottlenecked/120/11338_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  bottlenecked
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You could also draw inspiration from erlang’s <code>:gb_trees</code> implementation where each node is a <code>{key, value, left_subtree, right_subtree}</code> tuple which looks quite cheap/easy to update and probably cost less memory to keep around than a struct or map</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="181968" data-batch-url="/posts/batch_likers">
                        1
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/are-nested-structs-an-anti-pattern/32845/12">Post #11</a>
	                </div>
	            </div>
              <div id="likers-container-181968" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="181968"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #11"></div>
  </section>
</div>
    <div class="postbit" id="182323" data-post-id="182323">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="Dusty" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/Dusty/120/23609_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  Dusty
                    <span class="op-star" title="Thread Starter">
                      <img alt="OP" class="op-star-icon" src="/assets/thread-icons/thread-icon-thread-starter-df91e872.png" />
                    </span>
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>So I have been working a bit on the struct for each node in the tree. Each node needs to hold a pointer to its parent, as well as a list of children. What is the best way for the two nodes to point to each other in memory? I am coding in circles a bit. For example, if I start with this abbreviated module:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">defmodule SuffixTree.Node do
  alias __MODULE__
  use Puid

  @enforce_keys [:id, :children]

  defstruct id: nil,
            parent: nil,
            label: nil,
            leaves: [],
            children: [],
            link: nil

  @type t :: %Node{
          id: String.t(),
          parent: Node.t(),
          label: {String.t(), Range.t()},
          leaves: [{String.t(), integer()}],
          children: [Node.t()],
          link: Node.t()
        }

  def new_node() do
    %Node{
      id: generate(),
      parent: nil,
      label: nil,
      leaves: [],
      children: [],
      link: nil
    }
  end

  def add_child(%{children: children} = parent, child) do
    {:ok, child} = add_parent(parent, child)
    children = [child | children] |&gt; Enum.sort(Node)
    {:ok, %{parent | children: children}}
  end

  def add_parent(parent, child) do
    {:ok, %{child | parent: parent}}
  end

end
</code></pre>
<p>And then in IEx I load the module, and do something like:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">node1 = new_node()
node2 = new_node()
{:ok, node1} = add_child(node1, node2)
node1
</code></pre>
<p>I get back something like this:</p>
<pre data-code-wrap="elixir"><code class="lang-elixir">%SuffixTree.Node{
  children: [
    %SuffixTree.Node{
      children: [],
      id: "NJ_fgObmA_gBgU9GB7iy-Q",
      label: nil,
      leaves: [],
      link: nil,
      parent: %SuffixTree.Node{
        children: [],
        id: "rd2Y7laSMSDmaeT9JbLFeA",
        label: nil,
        leaves: [],
        link: nil,
        parent: nil
      }
    }
  ],
  id: "rd2Y7laSMSDmaeT9JbLFeA",
  label: nil,
  leaves: [],
  link: nil,
  parent: nil
}
</code></pre>
<p>I see that <code>node1</code>’s <code>id</code> matches in both places (in itself, and in the <code>parent</code> field of its child). But the <code>Node</code> listed in the <code>parent</code> field has no children, and we know that <code>node1</code> now has a child. It doesn’t take much thought for me to realize that there’s no way to display this kind of referential circle, because you would infinitely print the child inside the parent inside the child inside the parent…</p>
<p>So my question is: Am I pointing at 2 different memory locations? Are these actually 2 different <code>node1</code>’s?</p>
<p>The obvious answer seems to be to point only to the <code>id</code> of the node, rather than the node itself, but that means I need a lookup table/map containing every node by <code>id</code>.</p>
<p>Am I thinking about this wrong? How would you approach it?</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="182323" data-batch-url="/posts/batch_likers">
                        0
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/are-nested-structs-an-anti-pattern/32845/13">Post #12</a>
	                </div>
	            </div>
              <div id="likers-container-182323" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="182323"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-standard-post cat-standard-post" title="Post #12"></div>
  </section>
</div>
    <div class="postbit" id="182328" data-post-id="182328">
  <section>
    <div class="post-wrap">


					<div class="post-header">
		        <div class="user-avatar">
		          <img alt="NobbZ" src="https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/NobbZ/120/27235_2.png" width="120" height="120" />
		        </div>
					
						<div class="user-details">
		          <div class="user-name">
		            <h3>
                  NobbZ
                  </h3>
		          </div>
						
						</div>
					
					</div>

	        <div class="thread-main">
	            <div class="post-body" data-turbo="false">
								<p>You can’t have circular references in immutable data structures.</p>
<p>You can of course simulate them using a flat datastructure and mentioning IDs.</p> 
	            </div>

	            <div class="base-line">
	                <div class="thread-counters">
	                    <span class="thread-count count-likes js-likers-trigger" title="Likes" data-post-id="182328" data-batch-url="/posts/batch_likers">
                        3
                      </span>
                      <!-- <span class="thread-count js-solved-indicator" title="Marked as solution"></span> -->
	                </div>
	                <div class="go-to-post">
	                  <a title="Go to post" alt="Go to post" href="https://forum.elixirforum.com/t/are-nested-structs-an-anti-pattern/32845/14">Post #13</a>
	                </div>
	            </div>
              <div id="likers-container-182328" 
                   class="likers-container"
                   data-first-post="false"
                   data-batch-url="/posts/batch_likers">
                   <div class="likers-placeholder" 
                     data-likers-post-id="182328"
                     data-batch-url="/posts/batch_likers">
                  <div class="post-likers"></div>
                </div>
              </div>
	        </div>
			

    </div>

    <div class="triangle-top-right type-last-post cat-last-post" title="Last post!"></div>
  </section>
</div>
</template></turbo-stream><turbo-stream action="replace" target="load-more-container"><template><div id="load-more-container" class="load-more-container">
    <span class="all-loaded">— All posts loaded —</span>
</div></template></turbo-stream>