RedBlack(3pm)
π Tree::RedBlack π
okay so bestie, this is the RedBlack(3pm) page, straight out of the User Contributed Perl Documentation β¨ β and no cap, it's giving "balanced tree but make it fashion".
β¨ NAME β¨
Tree::RedBlack β a Perl implementation of a Red/Black tree, which is a type of balanced tree. iconic behavior fr fr π
πΈ 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;
literally just vibes and tree operations, periodt π
π¦ DESCRIPTION π¦
So this is a Perl implementation of the Red/Black tree algorithm from the book "Algorithms" by Cormen, Leiserson & Rivest β the one everyone lowkey just calls "CLR" or "The White Book" iykyk πβ¨
A Red/Black tree is a binary tree that stays "balanced" β meaning the longest length from root to a node is at most one more than the shortest such length. it's basically the tree that went to therapy and learned moderation π
And it's fairly efficient besties: no operation takes more than O(lg(n)) time. efficiency? she ate. π₯
A Tree::RedBlack object supports the following methods:
π new ()
Creates a new RedBlack tree object. new era, new tree, we love to see it π
π root ()
Returns the root node of the tree. heads up: this will either be undef if no nodes have been added to the tree, or a Tree::RedBlack::Node object. see the Tree::RedBlack::Node manual page for all the details on the Node object π«Ά
π cmp (&)
Use this method to set a comparator subroutine. by default the tree does lexical comparisons, which is giving "no thoughts, head empty, alphabetical". this subroutine should be just like a comparator subroutine to sort, except it doesn't do the $a, $b trick β the two elements you're comparing will just be the first two items on the stack. so no $a and $b shenanigans ok?? stack energy only π
π insert ($;$)
Adds a new node to the tree. first argument is the key of the node, second is its value. if a node with that key already exists, its value gets replaced with the given value and the old value is returned. otherwise, undef is returned. she's either giving you the old one back or giving you nothing bestie π
π 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!!! β οΈπ like, actual bugs, pls be aware, don't be surprised bestie.
π 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 literally can't tell the difference between finding a node with value undef and not finding a node at all π like they are the same picture fr. if you need to determine whether a node with a given key actually exists, use the node method, below. yes queen, there's a whole other method for that, keep reading π
π node ($)
Searches the tree to find the node with the given key. returns that node object if it's found, undef otherwise. the node object is a Tree::RedBlack::Node object. this is the one that actually tells you if it exists, so like, use her π§
π min ()
Returns the node with the minimal key. the smallest, the baby, the littlest one πΈ
π max ()
Returns the node with the maximal key. the biggest, the main character, the queen of the tree π
π AUTHOR π
Benjamin Holzman <bholzman@earthlink.net>
β SEE ALSO β
Tree::RedBlack::Nodeβ go read her next, she's the whole supporting cast π
and that's the whole page bestie!! this was perl v5.36.0, dated 2022-10-22 π stay balanced, stay iconic, and remember: no operation takes more than O(lg(n)) time, so you always have time to slay πβ¨π¦
π¦ 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