/usr/share/perl5/Tree/RedBlack/Node.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 | package Tree::RedBlack::Node;
use strict;
=head1 NAME
Tree::RedBlack::Node - Node class for Perl implementation of Red/Black tree
=head1 SYNOPSIS
use Tree::RedBlack;
my $t = new Tree::RedBlack;
$t->insert(3, 'dog');
my $node = $t->node(3);
$animal = $node->val;
=head1 DESCRIPTION
A Tree::RedBlack::Node object supports the following methods:
=over 4
=item key ()
Key of the node. This is what the nodes are sorted by in the tree.
=item val ($)
Value of the node. Can be any perl scalar, so it could be a hash-ref,
f'rinstance. This can be set directly.
=item color ()
Color of the node. 1 for "red", 0 or undef for "black".
=item parent ()
Parent node of this one. Returns undef for root node.
=item left ()
Left child node of this one. Returns undef for leaf nodes.
=item right ()
Right child node of this one. Returns undef for leaf nodes.
=item min ()
Returns the node with the minimal key starting from this node.
=item max ()
Returns the node with the maximal key starting from this node.
=item successor ()
Returns the node with the smallest key larger than this node's key, or this
node if it is the node with the maximal key.
=item predecessor ()
Similar to successor. WARNING: NOT YET IMPLEMENTED!!
=back
You can use these methods to write utility routines for actions on red/black
trees. For instance, here's a routine which writes a tree out to disk, putting
the byte offsets of the left and right child records in the record for each
node.
sub dump {
my($node, $fh) = @_;
my($left, $right);
my $pos = tell $fh;
print $fh $node->color ? 'R' : 'B';
seek($fh, 8, 1);
print $fh $node->val;
if ($node->left) {
$left = dump($node->left,$fh);
}
if ($node->right) {
$right = dump($node->right,$fh);
}
my $end = tell $fh;
seek($fh, $pos+1, 0);
print $fh pack('NN', $left, $right);
seek($fh, $end, 0);
$pos;
}
You would call it like this:
my $t = new Tree::RedBlack;
...
open(FILE, ">tree.dump");
dump($t->root,\*FILE);
close FILE;
As another example, here's a simple routine to print a human-readable dump of
the tree:
sub pretty_print {
my($node, $fh, $lvl) = @_;
if ($node->right) {
pretty_print($node->right, $fh, $lvl+1);
}
print $fh ' 'x($lvl*3),'[', $node->color ? 'R' : 'B', ']', $node->key, "\n";
if ($node->left) {
pretty_print($this->left, $fh, $lvl+1);
}
}
A cleaner way of doing this kind of thing is probably to allow sub-classing of
Tree::RedBlack::Node, and then allow the Tree::RedBlack constructor to take an
argument saying what class of node it should be made up out of. Hmmm...
=head1 AUTHOR
Benjamin Holzman <bholzman@earthlink.net>
=head1 SEE ALSO
Tree::RedBlack
=cut
sub new {
my $type = shift;
my $this = {};
if (ref $type) {
$this->{'parent'} = $type;
$type = ref $type;
}
if (@_) {
@$this{'key','val'} = @_;
}
return bless $this, $type;
}
sub DESTROY {
if ($_[0]->{'left'}) {
(delete $_[0]->{'left'})->DESTROY;
}
if ($_[0]->{'right'}) {
(delete $_[0]->{'right'})->DESTROY;
}
delete $_[0]->{'parent'};
}
sub key {
my $this = shift;
if (@_) {
$this->{'key'} = shift;
}
$this->{'key'};
}
sub val {
my $this = shift;
if (@_) {
$this->{'val'} = shift;
}
$this->{'val'};
}
sub color {
my $this = shift;
if (@_) {
$this->{'color'} = shift;
}
$this->{'color'};
}
sub left {
my $this = shift;
if (@_) {
$this->{'left'} = shift;
}
$this->{'left'};
}
sub right {
my $this = shift;
if (@_) {
$this->{'right'} = shift;
}
$this->{'right'};
}
sub parent {
my $this = shift;
if (@_) {
$this->{'parent'} = shift;
}
$this->{'parent'};
}
sub successor {
my $this = shift;
if ($this->{'right'}) {
return $this->{'right'}->min;
}
my $parent = $this->{'parent'};
while ($parent && $this == $parent->{'right'}) {
$this = $parent;
$parent = $parent->{'parent'};
}
$parent;
}
sub min {
my $this = shift;
while ($this->{'left'}) {
$this = $this->{'left'};
}
$this;
}
sub max {
my $this = shift;
while ($this->{'right'}) {
$this = $this->{'right'};
}
$this;
}
1;
|