forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathavl_tree.php
192 lines (160 loc) · 5.11 KB
/
avl_tree.php
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
<?php
class AVLTree
{
var $left;
var $right;
var $depth;
var $data;
function AVLTree()
{
$this->left = NULL;
$this->right = NULL;
$this->depth = 0;
$this->data = NULL;
}
function balance()
{
$ldepth = $this->left !== NULL
? $this->left->depth
: 0;
$rdepth = $this->right !== NULL
? $this->right->depth
: 0;
if( $ldepth > $rdepth + 1 )
{ // LR or LL rotation
$lldepth = $this->left->left !== NULL
? $this->left->left->depth
: 0;
$lrdepth = $this->left->right !== NULL
? $this->left->right->depth
: 0;
if( $lldepth < $lrdepth )
{ // LR rotation
$this->left->rotateRR(); // consist of a RR rotation of the left child ...
} // ... plus a LL rotation of this node, which happens anyway
$this->rotateLL();
}
else if( $ldepth + 1 < $rdepth )
{ // RR or RL rorarion
$rrdepth = $this->right->right !== NULL
? $this->right->right->depth
: 0;
$rldepth = $this->right->left !== NULL
? $this->right->left->depth
: 0;
if( $rldepth > $rrdepth )
{ // RR rotation
$this->right->rotateLL(); // consist of a LL rotation of the right child ...
} // ... plus a RR rotation of this node, which happens anyway
$this->rotateRR();
}
}
function rotateLL()
{ // the left side is too long => rotate from the left (_not_ leftwards)
$data_before =& $this->data;
$right_before =& $this->right;
$this->data =& $this->left->data;
$this->right =& $this->left;
$this->left =& $this->left->left;
$this->right->left =& $this->right->right;
$this->right->right =& $right_before;
$this->right->data =& $data_before;
$this->right->updateInNewLocation();
$this->updateInNewLocation();
}
function rotateRR()
{ // the right side is too long => rotate from the right (_not_ rightwards)
$data_before =& $this->data;
$left_before =& $this->left;
$this->data =& $this->right->data;
$this->left =& $this->right;
$this->right =& $this->right->right;
$this->left->right =& $this->left->left;
$this->left->left =& $left_before;
$this->left->data =& $data_before;
$this->left->updateInNewLocation();
$this->updateInNewLocation();
}
// -- updateInNewLocation
// may be overloaded by derived class
function updateInNewLocation()
{
$this->getDepthFromChildren();
}
function getDepthFromChildren()
{
$this->depth = $this->data !== NULL ? 1 : 0;
if( $this->left !== NULL )
$this->depth = $this->left->depth+1;
if( $this->right !== NULL && $this->depth <= $this->right->depth )
$this->depth = $this->right->depth+1;
}
// --- debugging functions
function toString()
{
$s = "<table border><tr>\n".$this->toTD(0)."</tr>\n";
$depth = $this->depth - 1;
for( $d = 0; $d < $depth; ++$d )
{
$s .= "<tr>";
$s .= $this->left !== NULL
? $this->left->toTD( $d )
: "<td></td>";
$s .= $this->right !== NULL
? $this->right->toTD( $d )
: "<td></td>";
$s .= "</tr>\n";
}
$s .= "</table>\n";
return $s;
}
function toTD( $depth )
{
if( $depth == 0 )
{
$s = "<td align=center colspan=".$this->getNLeafs().">";
$s .= $this->data."[".$this->depth."]</td>\n";
}
else
{
if( $this->left !== NULL )
{
$s = $this->left->toTD( $depth-1);
}
else
{
$s="<td></td>";
}
if( $this->right !== NULL )
{
$s .= $this->right->toTD( $depth-1);
}
else
{
if( $this->left !== NULL )
$s.="<td></td>";
}
}
return $s;
}
function getNLeafs()
{
if( $this->left !== NULL )
{
$nleafs = $this->left->getNLeafs();
if( $this->right !== NULL )
$nleafs += $this->right->getNLeafs();
else
++$nleafs; // lus one for the right "stump"
}
else
{
if( $this->right !== NULL )
$nleafs = $this->right->getNLeafs() + 1; // plus one for the left "stump"
else
$nleafs = 1; // this node is a leaf
}
return $nleafs;
}
}
?>