© 2013 All rights reserved.
0

Modified Preorder Tree Traversal Algorithm #3

Modified preorder tree traversal is one of the meanings, how to save tree structure in relational database. In this third part we will finish moving of bundles and more we will create very simple method to renumber of embranchments.

This article connects to last articles about Modified Preorder Tree Traversal Algorithm (#1, #2) again freely. In the last article we started with moving of embranchments in tree.

By using preorder tree traversal algorithm creates tree enough complicated for editing of embranchments, because we have to care about indexes for ever. By embranchments moving it is so hardly, that we have to solve out renumbering into few parts.

In last article we said, that it can create few cases by moving:


First of them appear, when two embranchments are just side-by-side and don’t contain other nested embranchments. This case is the most simple and we should change indexes of embranchments only.


Another case is, that two embranchments are side-by-side, but they have nested other embranchments in them. In this case we have to change indexes also in embranchments extra, which are inside of embranchments, which we are changing. It is logic, because right and left embranchments can’t contain same number of nested embranchments and so each index has to be changed.


The most complicated case appears in situation, when both embranchments, which we are changing, contain other embranchments and there are other embranchments between then extra. In this case we must change indexes in all subembranchments of right embranchment, in all subembranchments of left embranchment and moreover change indexes in all embranchments, which are between these two embranchments.

This are three basic cases and if we treat all cases, it won’t be problem with moving in tree.

Embranchments moving in TREE

It is end of theory, now have a look to practical use. We won’t care about each one case of moving, but we will create multi-purpose methods, which will treat all three cases automatically without aspect of that, if embranchments are side-by-side or not.

We will call method for moving like changeTree and input parameters will be left indexes between themselves of moved embranchments. This method won’t move nothing for this time, but it will only prepare variables helps to next methods, which we have created last time:

Method createTree creates array $parameters, which will contain left and right indexes of embranchments for moving. For finding of indexes we will use method _checkingTree, which control at the same time, if both embranchments have the same level of nesting in tree.

At the end method _edit_TREE is called. This method will move embranchments. Before we write this method, we will need another method, which will find ID of embranchments, which lies in first moved subtree and between both move tree, if there are same of them. It have to exist minimally one such as embranchment:

Have a look, that if some operation doesn’t manage to do, method returns “zero” and nothing happens.

We have got base completed, and we can create method for moving.

Method _edit_TREE first renumbers right subtree, which lies more to the right (it has got bigger right index) to the place of left subtree, then finds out, if other embranchments exist between these to subtrees and in case of true, it will renumber them too. At the end if will renumber left subtree to the place of right one.

We can’t renumber no more embranchments (by that I mean embranchments, which are ahead of left subtree or over right subtree), because these embranchments must agree with their status before moving.

Function _edit_TREE:

Have a look, that method calls another method, _edit_between. This method will renumber embranchments between two trees and we need to write it:

Method won’t be called, if two subtrees, which we move, have the same number of subembranchments. In such case indexes between trees will agree. Unfortunately this can’t be always true. Method _edit_between ensures this case.

Moving Up || Down

For case, that we want to move embranchments only one-by-one embranchment beside, we can create two simple method, which will use value of method _edit_TREE, indeed method _edit_between will be never called.

To make our work with user interface more simple, we will create one method extra, which will have first input parameter left index of moving embranchment and the second one will be direction, where moving will be directed.

In method are used statements UP and DOWN, this is though to size of left index:

We call two methods, which will be very similar. First time the left index of embranchment is found, it is index, which we want move and in case, that such embranchment exist and suit, method changeTree is called:

By this we have got finished whole operation of moving of embranchments. To the end we will complete our class with simple renaming of embranchment.

Embranchment’s renaming

Method for renaming is in our class only like such supplement, because we can need this function in future:

In the next part we will look to controlling of the whole class. We will create function for writing out tree and we will create simple code for easy controlling of moving etc.

At the end we can again look what we have done:

Comments are closed for this page

About
Hi, i am programmer from the Czech Republic. I love web development (Ruby, Ruby on Rails, PHP, Nette) and iOS development (Objective-C, Cocoa).
To cooperate, here is my phone:
+420 608 836