/usr/share/perl5/Tree/RedBlack.pm is in libtree-redblack-perl 0.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | package Tree::RedBlack;
use strict;
use Tree::RedBlack::Node;
use vars qw($VERSION);
$VERSION = '0.5';
=head1 NAME
Tree::RedBlack - Perl implementation of Red/Black tree, a type of balanced tree.
=head1 SYNOPSIS
use Tree::RedBlack;
my $t = new Tree::RedBlack;
$t->insert(3, 'cat');
$t->insert(4, 'dog');
my $v = $t->find(4);
my $min = $t->min;
my $max = $t->max;
$t->delete(3);
$t->print;
=head1 DESCRIPTION
This is a perl implementation of the Red/Black tree algorithm found in the book
"Algorithms", by Cormen, Leiserson & Rivest (more commonly known as "CLR" or
"The White Book"). A Red/Black tree is a binary tree which remains "balanced"-
that is, the longest length from root to a node is at most one more than the
shortest such length. It is fairly efficient; no operation takes more than
O(lg(n)) time.
A Tree::RedBlack object supports the following methods:
=over 4
=item new ()
Creates a new RedBlack tree object.
=item root ()
Returns the root node of the tree. Note that this will either be undef if no
nodes have been added to the tree, or a Tree::RedBlack::Node object. See the
L<Tree::RedBlack::Node> manual page for details on the Node object.
=item cmp (&)
Use this method to set a comparator subroutine. The tree defaults to lexical
comparisons. This subroutine should be just like a comparator subroutine to
sort, except that it doesn't do the $a, $b trick; the two elements to compare
will just be the first two items on the stack.
=item insert ($;$)
Adds a new node to the tree. The first argument is the key of the node, the
second is its value. If a node with that key already exists, its value is
replaced with the given value and the old value is returned. Otherwise, undef
is returned.
=item delete ($)
The argument should be either a node object to delete or the key of a node
object to delete. WARNING!!! THIS STILL HAS BUGS!!!
=item find ($)
Searches the tree to find the node with the given key. Returns the value of
that node, or undef if a node with that key isn't found. Note, in particular,
that you can't tell the difference between finding a node with value undef and
not finding a node at all. If you want to determine if a node with a given key
exists, use the node method, below.
=item node ($)
Searches the tree to find the node with the given key. Returns that node
object if it is found, undef otherwise. The node object is a
Tree::RedBlack::Node object.
=item min ()
Returns the node with the minimal key.
=item max ()
Returns the node with the maximal key.
=back
=head1 AUTHOR
Benjamin Holzman <bholzman@earthlink.net>
=head1 SEE ALSO
Tree::RedBlack::Node
=cut
sub new {
my $type = shift;
return bless {'null' => Tree::RedBlack::Node::->new,
'root' => undef}, $type;
}
sub DESTROY { if ($_[0]->{'root'}) { $_[0]->{'root'}->DESTROY } }
sub root {
my $this = shift;
return $this->{'root'};
}
sub cmp {
my($this, $cr) = @_;
$this->{'cmp'} = $cr;
}
sub insert {
my($this, $key, $value) = @_;
my $cmp = $this->{'cmp'};
my $node = $this->{'root'};
my $parent;
while ($node) {
$parent = $node;
if ($cmp ? $cmp->($key, $node->key) < 0 : $key lt $node->key) {
$node = $node->left;
} else {
$node = $node->right;
}
}
if ($parent) {
# Handle case of inserting node with duplicate key.
if ($cmp ? $cmp->($parent->key, $key) == 0 : $parent->key eq $key) {
my $val = $parent->val;
$parent->val($value);
return $val;
}
$node = $parent->new($key, $value);
if ($this->{'cmp'} ? $this->{'cmp'}->($key, $parent->key) < 0
: $key lt $parent->key) {
$parent->left($node);
} else {
$parent->right($node);
}
} else {
$this->{'root'} = $node = Tree::RedBlack::Node::->new($key, $value);
}
$node->color(1);
while ($node != $this->{'root'} && $node->parent->color) {
if (defined $node->parent->parent->left && $node->parent == $node->parent->parent->left) {
my $uncle = $node->parent->parent->right;
if ($uncle && $uncle->color) {
$node->parent->color(0);
$uncle->color(0);
$node->parent->parent->color(1);
$node = $node->parent->parent;
} else {
if ($node == $node->parent->right) {
$node = $node->parent;
$this->left_rotate($node);
}
$node->parent->color(0);
$node->parent->parent->color(1);
$this->right_rotate($node->parent->parent);
}
} else {
my $uncle = $node->parent->parent->left;
if ($uncle && $uncle->color) {
$node->parent->color(0);
$uncle->color(0);
$node->parent->parent->color(1);
$node = $node->parent->parent;
} else {
if (defined $node->parent->left && $node == $node->parent->left) {
$node = $node->parent;
$this->right_rotate($node);
}
$node->parent->color(0);
$node->parent->parent->color(1);
$this->left_rotate($node->parent->parent);
}
}
}
$this->{'root'}->color(0);
return;
}
sub left_rotate {
my($this, $node) = @_;
my $child = $node->right;
$node->right($child->left);
if ($child->left) {
$child->left->parent($node);
}
$child->parent($node->parent);
if ($node->parent) {
if ($node == $node->parent->left) {
$node->parent->left($child);
} else {
$node->parent->right($child);
}
} else {
$this->{'root'} = $child;
}
$child->left($node);
$node->parent($child);
}
sub right_rotate {
my($this, $node) = @_;
my $child = $node->left;
$node->left($child->right);
if ($child->right) {
$child->right->parent($node);
}
$child->parent($node->parent);
if ($node->parent) {
if ($node == $node->parent->right) {
$node->parent->right($child);
} else {
$node->parent->left($child);
}
} else {
$this->{'root'} = $child;
}
$child->right($node);
$node->parent($child);
}
sub delete {
my($this, $node_or_key) = @_;
my $node;
if (ref $node_or_key && $node_or_key->isa('Tree::RedBlack::Node')) {
$node = $node_or_key;
} else {
$node = $this->node($node_or_key) or return;
}
my($successor, $successor_child);
if (!($node->left && $node->right)) {
$successor = $node;
} else {
$successor = $node->successor;
}
if ($successor->left) {
$successor_child = $successor->left;
} else {
$successor_child = $successor->right || $this->{'null'};
}
$successor_child->parent($successor->parent);
if (!$successor_child || !$successor_child->parent) {
$this->{'root'} = $successor_child;
} elsif ($successor == $successor->parent->left) {
$successor->parent->left($successor_child);
} else {
$successor->parent->right($successor_child);
}
if ($successor != $node) {
$node->key($successor->key);
$node->val($successor->val);
}
if (!$successor->color) {
$this->delete_fixup($successor_child);
}
if (!$successor_child->parent) {
$this->{'root'} = undef;
}
$successor;
}
sub delete_fixup {
my($this, $x) = @_;
while ($x != $this->{'root'} && !$x->color) {
if ($x == $x->parent->left) {
my $w = $x->parent->right;
if ($w->color) {
$w->color(0);
$x->parent->color(1);
$this->left_rotate($x->parent);
}
if (!$w->left->color && !$w->right->color) {
$w->color(1);
$x = $x->parent;
} else {
if (!$w->right->color) {
$w->left->color(0);
$w->color(1);
$this->right_rotate($w);
$w = $x->parent->right;
}
$w->color($x->parent->color);
$x->parent->color(0);
$w->right->color(0);
$this->left_rotate($x->parent);
$x = $this->{'root'};
}
} else {
my $w = $x->parent->left;
if ($w->color) {
$w->color(0);
$x->parent->color(1);
$this->right_rotate($x->parent);
}
if (!$w->left->color && !$w->right->color) {
$w->color(1);
$x = $x->parent;
} else {
if (!$w->left->color) {
$w->right->color(0);
$w->color(1);
$this->left_rotate($w);
$w = $x->parent->left;
}
$w->color($x->parent->color);
$x->parent->color(0);
$w->left->color(0);
$this->right_rotate($x->parent);
$x = $this->{'root'};
}
}
}
$x->color(0);
}
sub min {
my $this = shift;
if ($this->{'root'}) {
if ($this->{'root'}->left) {
return $this->{'root'}->left->min;
} else {
return $this->{'root'};
}
}
return;
}
sub max {
my $this = shift;
if ($this->{'root'}) {
if ($this->{'root'}->right) {
return $this->{'root'}->right->max;
} else {
return $this->{'root'};
}
}
return;
}
sub find {
my($this, $key) = @_;
my $cmp = $this->{'cmp'};
my $node = $this->{'root'};
while ($node) {
if ($cmp ? $cmp->($key, $node->key) == 0 : $key eq $node->key) {
return $node->val;
} elsif ($cmp ? $cmp->($key, $node->key) < 0 : $key lt $node->key) {
$node = $node->left;
} else {
$node = $node->right;
}
}
# Got to the end without finding the node.
return;
}
sub node {
my($this, $key) = @_;
my $cmp = $this->{'cmp'};
my $node = $this->{'root'};
while ($node) {
if ($cmp ? $cmp->($key, $node->key) == 0 : $key eq $node->key) {
return $node;
} elsif ($cmp ? $cmp->($key, $node->key) < 0 : $key lt $node->key) {
$node = $node->left;
} else {
$node = $node->right;
}
}
# Got to the end without finding the node.
return;
}
1;
|