I'm implementing a Modified preorder tree traversal class for a category tree on a Web site, but I'm having trouble with one scenario. Typically when inserting a new category a top-level parent is specified, whose left value in the tree is used in order to determine where the new category should go in the tree. However, there could be times when no parent is specified, which means the new category must go in at the top of the tree, to the right of any other categories at the top of the tree.
Looking at some other applications with similar structures, many of them seem to be inserting a "root" node in the tree at the time of installation. I'm wondering if this is so they don't ever have to detect if it is the first insert, and they always have a left reference. Any thoughts or pseudo-code would be much appreciated. I'm doing this in PHP if it matters. My tree might look something like this:
Electronics Apparel My New Category
/ \ / \
MP3 TVs Shirts Shoes
My thought is that in this scenario, Apparel's right value will always be the greatest in the table, but I'm not sure how I might use that to determine it is the last. Any help or hints would be appreciated.