|
| 1 | +<?php |
| 2 | + |
| 3 | +class AVLTree |
| 4 | +{ |
| 5 | + var $left; |
| 6 | + var $right; |
| 7 | + var $depth; |
| 8 | + var $data; |
| 9 | + |
| 10 | + function AVLTree() |
| 11 | + { |
| 12 | + $this->left = NULL; |
| 13 | + $this->right = NULL; |
| 14 | + $this->depth = 0; |
| 15 | + $this->data = NULL; |
| 16 | + } |
| 17 | + |
| 18 | + function balance() |
| 19 | + { |
| 20 | + $ldepth = $this->left !== NULL |
| 21 | + ? $this->left->depth |
| 22 | + : 0; |
| 23 | + |
| 24 | + $rdepth = $this->right !== NULL |
| 25 | + ? $this->right->depth |
| 26 | + : 0; |
| 27 | + |
| 28 | + |
| 29 | + if( $ldepth > $rdepth + 1 ) |
| 30 | + { // LR or LL rotation |
| 31 | + $lldepth = $this->left->left !== NULL |
| 32 | + ? $this->left->left->depth |
| 33 | + : 0; |
| 34 | + |
| 35 | + $lrdepth = $this->left->right !== NULL |
| 36 | + ? $this->left->right->depth |
| 37 | + : 0; |
| 38 | + |
| 39 | + if( $lldepth < $lrdepth ) |
| 40 | + { // LR rotation |
| 41 | + $this->left->rotateRR(); // consist of a RR rotation of the left child ... |
| 42 | + } // ... plus a LL rotation of this node, which happens anyway |
| 43 | + |
| 44 | + $this->rotateLL(); |
| 45 | + } |
| 46 | + else if( $ldepth + 1 < $rdepth ) |
| 47 | + { // RR or RL rorarion |
| 48 | + $rrdepth = $this->right->right !== NULL |
| 49 | + ? $this->right->right->depth |
| 50 | + : 0; |
| 51 | + |
| 52 | + $rldepth = $this->right->left !== NULL |
| 53 | + ? $this->right->left->depth |
| 54 | + : 0; |
| 55 | + |
| 56 | + if( $rldepth > $rrdepth ) |
| 57 | + { // RR rotation |
| 58 | + $this->right->rotateLL(); // consist of a LL rotation of the right child ... |
| 59 | + } // ... plus a RR rotation of this node, which happens anyway |
| 60 | + |
| 61 | + $this->rotateRR(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + function rotateLL() |
| 66 | + { // the left side is too long => rotate from the left (_not_ leftwards) |
| 67 | + $data_before =& $this->data; |
| 68 | + $right_before =& $this->right; |
| 69 | + |
| 70 | + $this->data =& $this->left->data; |
| 71 | + $this->right =& $this->left; |
| 72 | + $this->left =& $this->left->left; |
| 73 | + $this->right->left =& $this->right->right; |
| 74 | + $this->right->right =& $right_before; |
| 75 | + $this->right->data =& $data_before; |
| 76 | + $this->right->updateInNewLocation(); |
| 77 | + $this->updateInNewLocation(); |
| 78 | + } |
| 79 | + |
| 80 | + function rotateRR() |
| 81 | + { // the right side is too long => rotate from the right (_not_ rightwards) |
| 82 | + $data_before =& $this->data; |
| 83 | + $left_before =& $this->left; |
| 84 | + |
| 85 | + $this->data =& $this->right->data; |
| 86 | + $this->left =& $this->right; |
| 87 | + $this->right =& $this->right->right; |
| 88 | + $this->left->right =& $this->left->left; |
| 89 | + $this->left->left =& $left_before; |
| 90 | + $this->left->data =& $data_before; |
| 91 | + $this->left->updateInNewLocation(); |
| 92 | + $this->updateInNewLocation(); |
| 93 | + } |
| 94 | + |
| 95 | + // -- updateInNewLocation |
| 96 | + // may be overloaded by derived class |
| 97 | + function updateInNewLocation() |
| 98 | + { |
| 99 | + $this->getDepthFromChildren(); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + function getDepthFromChildren() |
| 104 | + { |
| 105 | + $this->depth = $this->data !== NULL ? 1 : 0; |
| 106 | + if( $this->left !== NULL ) |
| 107 | + $this->depth = $this->left->depth+1; |
| 108 | + if( $this->right !== NULL && $this->depth <= $this->right->depth ) |
| 109 | + $this->depth = $this->right->depth+1; |
| 110 | + } |
| 111 | + |
| 112 | + // --- debugging functions |
| 113 | + |
| 114 | + function toString() |
| 115 | + { |
| 116 | + $s = "<table border><tr>\n".$this->toTD(0)."</tr>\n"; |
| 117 | + $depth = $this->depth - 1; |
| 118 | + for( $d = 0; $d < $depth; ++$d ) |
| 119 | + { |
| 120 | + $s .= "<tr>"; |
| 121 | + |
| 122 | + $s .= $this->left !== NULL |
| 123 | + ? $this->left->toTD( $d ) |
| 124 | + : "<td></td>"; |
| 125 | + |
| 126 | + $s .= $this->right !== NULL |
| 127 | + ? $this->right->toTD( $d ) |
| 128 | + : "<td></td>"; |
| 129 | + |
| 130 | + $s .= "</tr>\n"; |
| 131 | + } |
| 132 | + |
| 133 | + $s .= "</table>\n"; |
| 134 | + return $s; |
| 135 | + } |
| 136 | + |
| 137 | + function toTD( $depth ) |
| 138 | + { |
| 139 | + if( $depth == 0 ) |
| 140 | + { |
| 141 | + $s = "<td align=center colspan=".$this->getNLeafs().">"; |
| 142 | + $s .= $this->data."[".$this->depth."]</td>\n"; |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + if( $this->left !== NULL ) |
| 147 | + { |
| 148 | + $s = $this->left->toTD( $depth-1); |
| 149 | + } |
| 150 | + else |
| 151 | + { |
| 152 | + $s="<td></td>"; |
| 153 | + } |
| 154 | + |
| 155 | + if( $this->right !== NULL ) |
| 156 | + { |
| 157 | + $s .= $this->right->toTD( $depth-1); |
| 158 | + } |
| 159 | + else |
| 160 | + { |
| 161 | + if( $this->left !== NULL ) |
| 162 | + $s.="<td></td>"; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return $s; |
| 167 | + } |
| 168 | + |
| 169 | + function getNLeafs() |
| 170 | + { |
| 171 | + if( $this->left !== NULL ) |
| 172 | + { |
| 173 | + $nleafs = $this->left->getNLeafs(); |
| 174 | + |
| 175 | + if( $this->right !== NULL ) |
| 176 | + $nleafs += $this->right->getNLeafs(); |
| 177 | + else |
| 178 | + ++$nleafs; // lus one for the right "stump" |
| 179 | + } |
| 180 | + else |
| 181 | + { |
| 182 | + if( $this->right !== NULL ) |
| 183 | + $nleafs = $this->right->getNLeafs() + 1; // plus one for the left "stump" |
| 184 | + else |
| 185 | + $nleafs = 1; // this node is a leaf |
| 186 | + } |
| 187 | + |
| 188 | + return $nleafs; |
| 189 | + } |
| 190 | + |
| 191 | +} |
| 192 | +?> |
0 commit comments