Django Treebeard(annotated list)

Hi everyone, I’m using Django Treebeard and looking at the output of its annotated list. I found this example:

(a, {‘open’: True, ‘close’: , ‘level’: 0})
(ab, {‘open’: True, ‘close’: , ‘level’: 1})
(aba, {‘open’: True, ‘close’: , ‘level’: 2})
(abb, {‘open’: False, ‘close’: , ‘level’: 2})
(abc, {‘open’: False, ‘close’: [0,1], ‘level’: 2})
(ac, {‘open’: False, ‘close’: [0], ‘level’: 1})

I don’t understand how the close list works here and how the open value is determined to be true or false.
especially:

  • How does abc close [0,1] but then ac (which is on the same level as ab) still appears after?
  • If a (level 0) is closed at abc, how does ac still come afterward?
  • What exactly do the numbers in the close list refer .
    This is the image of the Tree

Welcome @aditya25042005 !

Ultimately, I would suggest reading the source code for this method to really understand what it’s doing.

Briefly, “open” indicates whether the node is the “leftmost” node of a particular level within the (sub)tree. So none of “a”, “ab” or “aba” have any siblings to their left, and so have an “open” value of True. The other three nodes, “abb”, “abc” and “ac” do have nodes to their left, and so are False.

The “close” value indicates whether the node is the “rightmost” of a set of siblings, and an indication of how deep it is within the subtree. The list simply contains the level numbers above it, it does not provide any information regarding its position within the tree. (In other words, my understanding is that if “ac” were to have child nodes “aca” and “acb”, then open would be True for “aca” and close would be [0,1] for “acb” just like it is for “abc”.)

2 Likes

Thank you so much for your detailed explanation. I truly appreciate the time and effort you took to help me. Your insights and reading source code have helped me understand the meaning . Thanks again for your assistance!