Skip to main content

My alernative to nested sets for arbitrary-depth hierarchical data sets: Good or Bad?


While recreating my CMS, I wanted an alternative to the traditional parent/child approach for managing the sitemap / page hierarchy. I had remembered seeing the nested set model a while back, but couldn't remember what it was called. So, I stumbled upon a similar approach that I want to evaluate and compare the properties, making sure I won't run into dumb limitations later on because I didn't go with what is already time-tested. So, please advise if A) it's already been invented (what's it called?!), B) there are fundamental flaws in the properties, or C) it's a good approach (please give good justification!).



Consider this list:



  • Home

    • About Us

    • Contact Us

    • Products

      • Clothing

      • Books

      • Electronics



    • Knowledge Base

    • Other stuff





Under the nested set model, I believe you store the left/right descriptors for each node with a depth-first traversal:




Home 1-18
About Us 2-3
Contact Us 4-5
Products 6-13
Clothing 7-8
Books 9-10
Electronics 11-12
Knowledge Base 14-15
Other stuff 16-17



And here's my "wrong way" that I'm starting to like better:




Home 1-9
About Us 2-2
Contact Us 3-3
Products 4-7
Clothing 5-5
Books 6-6
Electronics 7-7
Knowledge Base 8-8
Other stuff 9-9



Rather than a left/right pair, I'm storing ID and LAST_CONTAINED_ID. I found that many of the properties are the same (or very similar):



  • The root node is ID = 1

  • For "leaves," both properties are equal, while with branches, they are not

  • The total number of "subnodes" for any given node is LAST_CONTAINED_ID - ID

  • All contained nodes have an ID > the container's ID, but <= the container's LAST_CONTAINED_ID

  • The ancestor nodes have an ID < the child ID, but also a LAST_CONTAINED_ID >= the child ID

  • The depth is the SUM of the ancestor nodes



In addition, the ID serves an order-specific, unique identifier (with no gaps!). I've found it easier also to store DEPTH and PARENT references for simplicity, but that's pretty much the same for nested sets too from what I understand.



So, does this count as a nested set? And is it already a common approach (but why hadn't I heard of it before...)? Is there a good reason why I should use a true nested set over this?



I welcome your thoughts.


Source: Tips4allCCNA FINAL EXAM

Comments

  1. The only advantage it gives is the 'no gaps' feature, but to achieve that you've had to change the logic applied to right-values. In the original model, you get the children of 'Products' by seeing all those values 6 < .. < 13, but in your model, you get those children by seeing values 4 < .. <= 7. Having to treat right-values different to left-values makes it slightly less elegant.
    Another minor gripe is that in the original, the jump from 12 to 14 highlights that you've changed level, whereas in your model you don't get such visual cues.
    So if you're happy using (<, <=) in place of (<, <) then it works. (Since it appears to be equivalent, I can't say 'good' or 'bad', but you've already highlighted the dangers of implementing the path less travelled.)

    ReplyDelete

Post a Comment

Popular posts from this blog

[韓日関係] 首相含む大幅な内閣改造の可能性…早ければ来月10日ごろ=韓国

div not scrolling properly with slimScroll plugin

I am using the slimScroll plugin for jQuery by Piotr Rochala Which is a great plugin for nice scrollbars on most browsers but I am stuck because I am using it for a chat box and whenever the user appends new text to the boxit does scroll using the .scrollTop() method however the plugin's scrollbar doesnt scroll with it and when the user wants to look though the chat history it will start scrolling from near the top. I have made a quick demo of my situation http://jsfiddle.net/DY9CT/2/ Does anyone know how to solve this problem?

Why does this javascript based printing cause Safari to refresh the page?

The page I am working on has a javascript function executed to print parts of the page. For some reason, printing in Safari, causes the window to somehow update. I say somehow, because it does not really refresh as in reload the page, but rather it starts the "rendering" of the page from start, i.e. scroll to top, flash animations start from 0, and so forth. The effect is reproduced by this fiddle: http://jsfiddle.net/fYmnB/ Clicking the print button and finishing or cancelling a print in Safari causes the screen to "go white" for a sec, which in my real website manifests itself as something "like" a reload. While running print button with, let's say, Firefox, just opens and closes the print dialogue without affecting the fiddle page in any way. Is there something with my way of calling the browsers print method that causes this, or how can it be explained - and preferably, avoided? P.S.: On my real site the same occurs with Chrome. In the ex