Code

Changes between Version 1 and Version 2 of ModifiedPreorderTreeTraversal


Ignore:
Timestamp:
02/01/06 04:09:55 (8 years ago)
Author:
inerte@…
Comment:

typos and clarification

Legend:

Unmodified
Added
Removed
Modified
  • ModifiedPreorderTreeTraversal

    v1 v2  
    1 There's a good article explaining [http://www.sitepoint.com/print/hierarchical-data-database what's Modified Preorder Tree Traversal on SitePoint]. Basically, it allows you to discover every children, or parent, or the whole tree, with a single query. Very efficient! And good for threaded discussions... 
     1There's a good article explaining [http://www.sitepoint.com/print/hierarchical-data-database what's Modified Preorder Tree Traversal on SitePoint]. I am not going to explain the whole theory here, just offer some code so you can implement it quickly. 
    22 
    3 I modified these snippets from an app I coded in portuguese, with more features than what's pasted here. If there's anything wrong, send me an email, because I do have it working here :) 
     3Basically, it allows you to discover every children, or parent, or the whole tree, with a single query. Very efficient! And good for threaded discussions... 
     4 
     5I modified these snippets from an app I coded in the portuguese language, with more features than what's pasted here. If there's anything wrong, send me an email, because I do have it working :) 
    46 
    57Here's a model, a view and a couple functions to help you implement it. 
     
    3638cursor.execute("UPDATE `app_comments` " \ 
    3739               "SET `lft` = `lft` + 2 " \ 
    38                "WHERE `lft` > %i" % int(target_rght) 
     40               "WHERE `lft` > %i" % int(target_rght)) 
    3941 
    4042db.commit() 
     
    6062    for j in stack_copy: 
    6163        if j < comment.rght: 
    62             stack.pop()                     
     64            stack.pop() 
    6365 
    6466    stack_size = len(stack) 
    6567 
    66     comment.stack = stack_size        
     68    comment.stack = stack_size 
    6769 
    6870    stack.append(comentario.rght) 
    6971}}} 
    7072 
    71 So, what have we done? There's a "stack" attribute on each comment now that tells you "how far" each comment is from another. For example, every "root" comment has a stack of 1. Every comment that's an answer to the root comments has a stack of 2. The stack respect the order that the comments were inserted at the database. Something like this: 
     73So, what have we done? There's a "stack" attribute on each comment now that tells you "how far" each comment is from another. For example, every "root" comment (the direct reply to a "post", for example) has a stack of 1. Every comment that's an answer to the root comments has a stack of 2. The stack respects the order (because of order_by=['lft']) that the comments were inserted at the database. So we end up with comments having stacks numbered like this: 
    7274 
    7375{{{ 
     
    7678  3 
    7779  3 
     80   4 
     81 2 
    7882 2 
    7983 
    80841 
    8185 2 
     86  3 
    8287  3 
    8388}}} 
     
    8994  {{ comment.body }} 
    9095</div> 
    91  
    9296}}} 
    9397