RedBlack(3pm)

πŸ“¦
version 0.5 Β· 3 versions
, in ubuntu noble, ubuntu resolute, ubuntu stonking

🌈 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