Node(3pm)
β¨ NAME β¨
okay so bestie, this is Node(3pm) from the User Contributed Perl Documentation β aka Tree::RedBlack::Node β Node class for Perl implementation of Red/Black tree. she's literally the building block of the whole tree, no cap π«Άπ
β¨ SYNOPSIS β¨
use Tree::RedBlack; my $t = new Tree::RedBlack; $t->insert(3, 'dog'); my $node = $t->node(3); $animal = $node->val;
β¨ DESCRIPTION β¨
A Tree::RedBlack::Node object supports the following methods:
key ()β the key of the node. this is what the nodes are sorted by in the tree, so it's the main character of the ordering vibe πval ($)β the value of the node. can be any perl scalar, so it could be a hash-ref, for example. this can be set directly. yes bestie, direct set, we love a mutable moment πcolor ()β the color of the node.1for "red",0orundeffor "black". that's the whole red/black thing, periodt πparent ()β parent node of this one. returnsundeffor the root node. root has no parent, she's independent β¨left ()β left child node of this one. returnsundeffor leaf nodes. leaf means no left baby, okay? πright ()β right child node of this one. returnsundeffor leaf nodes. same energy on the right side π¦min ()β returns the node with the minimal key starting from this node. go find the smallest, bestie π₯max ()β returns the node with the maximal key starting from this node. go find the biggest, slay π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. so if you're already maxxed out, you get yourself back, iconic πpredecessor ()β similar tosuccessor. WARNING: NOT YET IMPLEMENTED!! do not come for her, she is not ready ππ
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... noted, bestie π§π
β¨ AUTHOR β¨
Benjamin Holzman <bholzman@earthlink.net> β the author of this whole vibe, give her a follow or whatever π
β¨ SEE ALSO β¨
Tree::RedBlack β go check out the main tree module, iykyk π
kisses bestie, this page is from perl v5.36.0, dated 2022-10-22, and it's literally Node(3pm) β periodt π
β¨
π¦ where this page comes from
- ubuntu noble: libtree-redblack-perl 0.5-4 Β· Artistic-1.0-Perl
- ubuntu resolute: libtree-redblack-perl 0.5-4 Β· Artistic-1.0-Perl
- ubuntu stonking: libtree-redblack-perl 0.5-4 Β· Artistic-1.0-Perl
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms