1

When implementing a recursive data structure like TREE, I need a common attribute per TREE, and I wonder how to implement it:

  • Adding the attribute to a TREE node, replicates the attribute for every node, not once per TREE
  • Using a once attribute, I get only one shared attribute for all TREEs, not one per TREE.

Is there any elegant Eiffel-style solution for that?

U. Windl
  • 3,480
  • 26
  • 54
  • It depends on the TREE structure. E.g., is it possible to reach the root node from any other node of the tree? If yes, the root node can be a special one with the required attribute, and all other nodes can retrieve the attribute value when needed by accessing the root node. – Alexander Kogtenkov May 24 '21 at 20:07
  • In a general tree you couldn't reach the root node, but in one implementation you actually could. So could you sketch a partial answer when the root node is reachable from any node (maybe through an attribute named `parent`). – U. Windl May 26 '21 at 09:09

1 Answers1

0

A tree has a distinguished root node that can be used to store information related to the whole tree rather than to a specific node. In order to retrieve this information, there should be a possibility to reach the root node from any other node of the tree. One possible solution is to have a feature parent that would return the parent node of the specified node (or Current for the root). Then, the feature that obtains the root node can look like

root: TREE
        -- The root of the tree.
    local
        p: TREE
    do
        from
            Result := Current
            p := parent
        until
            Result = p -- `Result = Result.parent` when `Result` is root.
        loop
            Result := p
            p := p. parent
        end
    ensure
        Result.parent = Result -- `Result` has no other parent.
    end

Then the tree-specific attribute value can be retrieved from an arbitrary tree node n with n.root.my_attribute.

EDIT:

Another possibility is to have a dedicated CELL with the required data, and all nodes in the tree simply refer to this cell. The benefit is that no reference to the parent node is need and the access to the data is immediate.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • So `n` is a `TREE`, and `my-attribute` is part of the `TREE`? If so, isn't `my_attribute` present in any `TREE` node then? If so that wouldn't be an answer (efficiency issues left aside). – U. Windl May 27 '21 at 09:17
  • @U.Windl Different implementations are possible. In one, all nodes in the tree have the same type, the attribute is present in every node, however, `n.root.my_attribute` guarantees that only a single version is used. In another implementation, the root has a dedicated type. Every node implements `my_attribute` as a function returning `root.my_attribute` except for the root node where it is the actual attribute. – Alexander Kogtenkov May 28 '21 at 11:53
  • Even with a `once` attribute (that wouldn't solve the problem) I was afraid that every node of the `TREE` would contain a `POINTER` corresponding to the `once` function. Specifically what I had in mind was something like a single bit (`BOOLEAN`). In C (for contrast) one could use a bit field, most likely where there are some spare bits anyway (like tree balance, for example). – U. Windl May 29 '21 at 09:59
  • @U.Windl The question is language-neutral, feel free to provide a working answer in any language. – Alexander Kogtenkov May 29 '21 at 15:02
  • Actually I'm thinking about an array-based implementation, making things like that much easier. – U. Windl May 30 '21 at 11:03
  • @U.Windl The question is specifically about tree nodes, not about arrays. – Alexander Kogtenkov May 31 '21 at 06:11