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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function changeTree($left1, $left2){ if( ($svalues = self::_checkingTree($left1, $left2)) == 0 ) return 0; $this->parametrs[ 'parent' ] = $svalues[0]; $this->parametrs[ 'left1' ] = ( $left1 < $left2 ? $left1 : $left2 ); $this->parametrs[ 'right1' ] = ( $svalues[2] < $svalues[3] ? $svalues[2] : $svalues[3] ); $this->parametrs[ 'left2' ] = ( $left1 < $left2 ? $left2 : $left1 ); $this->parametrs[ 'right2' ] = ( $svalues[2] < $svalues[3] ? $svalues[3] : $svalues[2] ); switch( self::_edit_TREE()){ case 0 : return 0; break; case -1 : return -1; break; default: return 1; } } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
function _select_ids($left, $left2){ $select = "SELECT id_traverz, traverz_left FROM " . $this->table_name . " WHERE traverz_left > '" . ( $left - 1 ) . "' AND traverz_right < '" . $left2 . "' ORDER BY traverz_left"; $data = mysql_query($select, $this->link); if( mysql_num_rows($data) == 0 ) return 0; while( $row = mysql_fetch_object($data) ){ $this->ids[$row->traverz_left] = $row->id_traverz; } return ( sizeof($this->ids) > 0 ? 1 : 0 ); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
function _edit_TREE(){ if( self::_select_ids($this->parametrs['left1'], $this->parametrs['left2']) == 0 ) return 0; $right_tree = $this->parametrs['left2'] - $this->parametrs['left1']; $update = "UPDATE " . $this->table_name . " SET traverz_left = traverz_left - '" . $right_tree . "', traverz_right = traverz_right - '" . $right_tree . "' WHERE traverz_left > '" . ( $this->parametrs['left2'] - 1 ) . "' AND traverz_right < '" . ( $this->parametrs['right2'] + 1 ) . "'"; mysql_query($update, $this->link); if( ( $mysql_affected_rows = mysql_affected_rows($this->link)) < 1 ) return 0; $left_array = ( $this->parametrs['right1'] - $this->parametrs['left1'] - 1 ) / 2 ; $right_array = ( $this->parametrs['right2'] - $this->parametrs['left2'] - 1 ) / 2 ; if( $left_array != $right_array ){ if( ($num_rows = self::_edit_bettwen( $mysql_affected_rows)) < 0 ) return -1; } else $num_rows = 0; //$num_rows = $num_rows - 1; $p_a = (abs( $mysql_affected_rows ) + abs( $num_rows )) * 2; reset($this->ids); $ids = array(); foreach( $this->ids as $id => $value ) if( ($id <= $this->parametrs['right1']) && ($id >= $this->parametrs['left1']) ) $ids[] = $value; $update = "UPDATE " . $this->table_name . " SET traverz_left = (traverz_left + " . $p_a . "), traverz_right = traverz_right + " . $p_a . " WHERE id_traverz IN( "; foreach( $ids as $value ) $update .= $value . ','; $update = substr($update, 0, -1) . ') LIMIT ' . sizeof($ids) . ';'; mysql_query($update, $this->link); if( mysql_error() != NULL ){ return -1; } else return 1; } |
Have a look, that method calls another method, _edit_between. This method will renumber embranchments between two trees and we need to write it:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function _edit_bettwen($affected_rows){ if( ($this->parametrs['right1'] + 1 ) == $this->parametrs['left2'] ) return 0; $p_a = ( ($this->parametrs['left1']) + (2 * $affected_rows)) - ($this->parametrs['right1'] + 1 ); if( $p_a == 0 ) return 0; reset($this->ids); foreach($this->ids as $id => $value) if( ($id > $this->parametrs['right1']) && ($id < $this->parametrs['left2']) ) $ids[] = $value; $update_value = ( $p_a > 0 ? ' + ' : ' - ') . "'" . abs($p_a) . "'"; $update = "UPDATE " . $this->table_name . " SET traverz_left = (traverz_left " . $update_value . "), traverz_right = traverz_right " . $update_value . " WHERE id_traverz IN( "; foreach( $ids as $value ) $update .= $value . ','; $update = substr($update, 0, -1) . ') LIMIT ' . sizeof($ids) . ';'; mysql_query($update, $this->link); return mysql_affected_rows($this->link); } |
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:
1 2 3 4 5 6 7 8 9 |
function moveTree( $left, $shift ){ switch( $shift ){ case 'up' : return self::_moveUpTree( $left ); break; case 'down' : return self::_moveDownTree( $left ); break; default: return 0; } return 1; } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
function _moveUpTree($left){ $parent = self::select_parent( $left ); $select = "SELECT MAX(traverz_left) FROM " . $this->table_name . " WHERE( traverz_left < '" . $left . "') AND ( traverz_parent = '" . ( $parent - 1 ) . "')"; $data = mysql_query( $select, $this->link ); if( mysql_num_rows( $data ) == 0 ) return 0; $left2 = mysql_result( $data, 0, 0 ); return self::changeTree( $left2, $left); } function _moveDownTree( $left ){ $parent = self::select_parent( $left ); $select = "SELECT MIN( traverz_left) FROM " . $this->table_name . " WHERE( traverz_left > '" . $left . "') AND( traverz_parent = '" . ( $parent - 1 ) . "')"; $data = mysql_query( $select, $this->link ); if( mysql_num_rows( $data ) == 0 ) return 0; $left2 = mysql_result( $data, 0, 0 ); return self::changeTree( $left2, $left); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 |
function renameCell($new_name, $left){ $update = "UPDATE " . $this->table_name . " SET traverz_name = '" . trim(htmlspecialchars( $new_name )) . "', url = '" . $_REQUEST['url'] . "' WHERE traverz_left = '" . intval( $left ) . "' AND lang = '" . $_SESSION['lang_prefix'] . "' LIMIT 1;"; mysql_query($update, $this->link); return ( mysql_affected_rows( $this->link ) == 1 ? 1 : 0 ); } |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 |
class traverz extends connect{ public $traverz_right = NULL; public $id_traverz = NULL; public $parametrs = array(); public $ids = array(); public $table = NULL; function __construct( $table_name ){ $this->table_name = $table_name; parent::__construct(); self::_check_num_of_rows(); } function _check_num_of_rows(){ $select = "SELECT COUNT(*) FROM " . $this->table_name . ""; if( mysql_result( mysql_query( $select, $this->link ), 0, 0) == 0 ){ self::_insert_first(); return 0; } else{ return 0; } } function _insert_first(){ $insert = "INSERT INTO " . $this->table_name . "( traverz_name, traverz_parent, traverz_left, traverz_right ) VALUES( 'HOME', '0', '1', '2' )"; mysql_query($insert, $this->link); return ( mysql_affected_rows($this->link) == 1 ? 1 : 0); } function _update_tree($id, $left){ $sql[] = "UPDATE " . $this->table_name . " SET traverz_left = (traverz_left + 2) WHERE ( traverz_left > '" . $left . "' ) AND id_traverz != '" . $id . "'"; $sql[] = "UPDATE " . $this->table_name . " SET traverz_right = (traverz_right + 2) WHERE ( traverz_right > '" . ($left - 1) . "' ) AND id_traverz != '" . $id . "'"; foreach($sql as $value){ mysql_query($value, $this->link); } if(mysql_affected_rows($this->link) == 0 ) return 0; } function select_parent($left, $info = 0){ $select = "SELECT traverz_parent, traverz_right, id_traverz FROM " . $this->table_name . " WHERE traverz_left = '" . $left . "' LIMIT 1;"; $data = mysql_query($select, $this->link); if( $info == 1 ){ $this->traverz_right = @mysql_result($data, 0, 1); $this->id_traverz = @mysql_result( $data, 0, 2 ); } return ( @mysql_result($data, 0, 0) + 1 ); } function _setup_position( $position ){ switch( $position ){ case 'h' : return 0; break; case 'e' : return 1; break; default: return 0; } } function addTree ( $name, $left, $position = 0 ){ if( gettype( $position ) == 'string' ) $position = self::_setup_position( $position ); $position = abs( intval( $position) ); if( $position > 2 ) return 0; $parent = self::select_parent($left, 1); switch( $position ){ case 0 : { if( self::_addTree_h( $name, $left, $parent ) == 1 ) return 1; else return 0; break; } case 1 :{ if( self::_addTree_e( $name, $left, $parent, ( $this->traverz_right - 1 ) ) == 1 ) return 1; else return 0; break; } } return 1; } function _addTree_h( $name, $left, $parent ){ $insert = "INSERT INTO " . $this->table_name . "( traverz_name, traverz_parent, traverz_left, traverz_right ) VALUES( '" . trim( htmlspecialchars($name) ) . "', '" . intval( $parent ) . "', '" . ( intval( $left ) + 1 ) . "', '" . ( intval( $left ) + 2 ) . "' );"; mysql_query($insert, $this->link); if( mysql_affected_rows($this->link) == 1 ) return (self::_update_tree(mysql_insert_id(), $left) == 1 ? 1 : 0 ); else return 0; } function _addTree_e( $name, $left, $parent ){ $insert = "INSERT INTO " . $this->table_name . "( traverz_name, traverz_parent, traverz_left, traverz_right ) VALUES( '" . trim( htmlspecialchars( $name ) ) . "', '" . intval( $parent ) . "', '" . intval( $this->traverz_right ) . "', '" . ( intval( $this->traverz_right ) + 1 ) . "' );"; mysql_query($insert, $this->link); if( mysql_affected_rows($this->link) == 1 ) return (self::_update_tree(mysql_insert_id(), $this->traverz_right) == 1 ? 1 : 0 ); else return 0; } function _renumberTree($left){ $update[] = "UPDATE " . $this->table_name . " SET traverz_left = (traverz_left - 2) WHERE ( traverz_left > '" . intval( $left ) . "')"; $update[] = "UPDATE " . $this->table_name . " SET traverz_right = (traverz_right - 2) WHERE ( traverz_right > '" . intval( $left + 1 ) . "')"; foreach($update as $value){ mysql_query( $value, $this->link ); if( mysql_affected_rows( $this->link ) < 0 ) return 0; } return 1; } function _checkTree( $left, $parent ){ $select = "SELECT COUNT(*) FROM " . $this->table_name . " WHERE ( traverz_left BETWEEN '" . $left . "' AND '" . $this->traverz_right . "' ) "; $num = intval( @mysql_result(mysql_query($select, $this->link), 0, 0) - 1 ); return ( $num == 0 ? 1 : 0 ); } function deleteTree( $left, $id ){ $left = intval( $left ); $id = intval( $id ); $parent = self::select_parent($left, 1); if(self::_checkTree($left, $parent) == 0) return -1; if( self::_renumberTree($left) == 0 ) return 0; $delete = "DELETE FROM " . $this->table_name . " WHERE id_traverz = '" . $id . "' LIMIT 1;"; mysql_query($delete, $this->link); return ( mysql_affected_rows( $this->link ) == 0 ? 0 : 1 ); } function _select_parent_check($left1, $left2){ $select = "SELECT traverz_parent, traverz_right FROM " . $this->table_name . " WHERE traverz_left IN ('" . $left1 . "', '" . $left2 . "') ORDER BY traverz_left;"; if( ( $data = mysql_query($select, $this->link) ) == FALSE ){ return 0; } return array( @mysql_result($data, 0, 0), @mysql_result($data, 1, 0), @mysql_result($data, 0, 1), @mysql_result($data, 1, 1) ); } function _checkingTree($left1, $left2){ $parents = self::_select_parent_check($left1, $left2); if( sizeof($parents) != 4 ) return 0; else if( $parents[0] != $parents[1] ) return 0; $select = "SELECT traverz_parent, traverz_left, traverz_right FROM " . $this->table_name . " WHERE traverz_left < '" . ( $left1 < $left2 ? $left1 : $left2) . "' AND traverz_parent = '" . ($parents[0] - 1) . "' ORDER BY traverz_left DESC LIMIT 1;"; $data = mysql_query($select, $this->link); if( mysql_error() != NULL ) return 0; $h1 = @mysql_result($data, 0, 1); $h2 = @mysql_result($data, 0, 2); //test left1 if( !(($h1 < $left1) && ($h2 > $left2)) ){ return 0; } //test left2 else if( !(($h1 < $left2) && ($h2 > $left2)) ){ return 0; } else { return $parents; } } function changeTree($left1, $left2){ if( ($svalues = self::_checkingTree($left1, $left2)) == 0 ) return 0; $this->parametrs[ 'parent' ] = $svalues[0]; $this->parametrs[ 'left1' ] = ( $left1 < $left2 ? $left1 : $left2 ); $this->parametrs[ 'right1' ] = ( $svalues[2] < $svalues[3] ? $svalues[2] : $svalues[3] ); $this->parametrs[ 'left2' ] = ( $left1 < $left2 ? $left2 : $left1 ); $this->parametrs[ 'right2' ] = ( $svalues[2] < $svalues[3] ? $svalues[3] : $svalues[2] ); switch( self::_edit_TREE()){ case 0 : return 0; break; case -1 : return -1; break; default: return 1; } } function _select_ids($left, $left2){ $select = "SELECT id_traverz, traverz_left FROM " . $this->table_name . " WHERE traverz_left > '" . ( $left - 1 ) . "' AND traverz_right < '" . $left2 . "' ORDER BY traverz_left"; $data = mysql_query($select, $this->link); if( mysql_num_rows($data) == 0 ) return 0; while( $row = mysql_fetch_object($data) ){ $this->ids[$row->traverz_left] = $row->id_traverz; } return ( sizeof($this->ids) > 0 ? 1 : 0 ); } function _edit_TREE(){ if( self::_select_ids($this->parametrs['left1'], $this->parametrs['left2']) == 0 ) return 0; $right_tree = $this->parametrs['left2'] - $this->parametrs['left1']; $update = "UPDATE " . $this->table_name . " SET traverz_left = traverz_left - '" . $right_tree . "', traverz_right = traverz_right - '" . $right_tree . "' WHERE traverz_left > '" . ( $this->parametrs['left2'] - 1 ) . "' AND traverz_right < '" . ( $this->parametrs['right2'] + 1 ) . "' "; mysql_query($update, $this->link); if( ( $mysql_affected_rows = mysql_affected_rows($this->link)) < 1 ) return 0; $left_array = ( $this->parametrs['right1'] - $this->parametrs['left1'] - 1 ) / 2 ; $right_array = ( $this->parametrs['right2'] - $this->parametrs['left2'] - 1 ) / 2 ; if( $left_array != $right_array ){ if( ($num_rows = self::_edit_bettwen( $mysql_affected_rows)) < 0 ) return -1; } else $num_rows = 0; //$num_rows = $num_rows - 1; $p_a = (abs( $mysql_affected_rows ) + abs( $num_rows )) * 2; reset($this->ids); $ids = array(); foreach( $this->ids as $id => $value ) if( ($id <= $this->parametrs['right1']) && ($id >= $this->parametrs['left1']) ) $ids[] = $value; $update = "UPDATE " . $this->table_name . " SET traverz_left = (traverz_left + " . $p_a . "), traverz_right = traverz_right + " . $p_a . " WHERE id_traverz IN( "; foreach( $ids as $value ) $update .= $value . ','; $update = substr($update, 0, -1) . ') LIMIT ' . sizeof($ids) . ';'; mysql_query($update, $this->link); if( mysql_error() != NULL ){ return -1; } else return 1; } function _edit_bettwen($affected_rows){ if( ($this->parametrs['right1'] + 1 ) == $this->parametrs['left2'] ) return 0; $p_a = ( ($this->parametrs['left1']) + (2 * $affected_rows)) - ($this->parametrs['right1'] + 1 ); if( $p_a == 0 ) return 0; reset($this->ids); foreach($this->ids as $id => $value) if( ($id > $this->parametrs['right1']) && ($id < $this->parametrs['left2']) ) $ids[] = $value; $update_value = ( $p_a > 0 ? ' + ' : ' - ') . "'" . abs($p_a) . "'"; $update = "UPDATE " . $this->table_name . " SET traverz_left = (traverz_left " . $update_value . "), traverz_right = traverz_right " . $update_value . " WHERE id_traverz IN( "; foreach( $ids as $value ) $update .= $value . ','; $update = substr($update, 0, -1) . ') LIMIT ' . sizeof($ids) . ';'; mysql_query($update, $this->link); return mysql_affected_rows($this->link); } function moveTree( $left, $shift ){ switch( $shift ){ case 'up' : return self::_moveUpTree( $left ); break; case 'down' : return self::_moveDownTree( $left ); break; default: return 0; } return 1; } function _moveUpTree($left){ $parent = self::select_parent( $left ); $select = "SELECT MAX(traverz_left) FROM " . $this->table_name . " WHERE( traverz_left < '" . $left . "') AND ( traverz_parent = '" . ( $parent - 1 ) . "')"; $data = mysql_query( $select, $this->link ); if( mysql_num_rows( $data ) == 0 ) return 0; $left2 = mysql_result( $data, 0, 0 ); return self::changeTree( $left2, $left); } function _moveDownTree( $left ){ $parent = self::select_parent( $left ); $select = "SELECT MIN( traverz_left) FROM " . $this->table_name . " WHERE( traverz_left > '" . $left . "') AND( traverz_parent = '" . ( $parent - 1 ) . "')"; $data = mysql_query( $select, $this->link ); if( mysql_num_rows( $data ) == 0 ) return 0; $left2 = mysql_result( $data, 0, 0 ); return self::changeTree( $left2, $left); } function renameCell($new_name, $left){ $update = "UPDATE " . $this->table_name . " SET traverz_name = '" . trim(htmlspecialchars( $new_name )) . "', url = '" . $_REQUEST['url'] . "' WHERE traverz_left = '" . intval( $left ) . "' AND lang = '" . $_SESSION['lang_prefix'] . "' LIMIT 1;"; mysql_query($update, $this->link); return ( mysql_affected_rows( $this->link ) == 1 ? 1 : 0 ); } } |