tree(3bsd)

πŸ“¦
version 0.12.2 Β· 3 versions
, in alpine 3.22, alpine 3.23, alpine 3.24, arch rolling, debian trixie, epel 10, epel 9, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, ubuntu noble, ubuntu questing, ubuntu resolute, ubuntu stonking

πŸŽ€ tree(3bsd) πŸŽ€

okay bestie, pull up a chair because we're about to get so cozy with tree(3bsd) β€” that's the manual for all the tree macros living in libbsd. Yes, the tree macros. The ones that give you data structures for free. It's a LOCAL page, it's giving very much "systems programming but make it cute", and honestly? Obsessed. ✨

This page is long, so it's split into parts β€” this is part 1. More is coming, don't you dare close the tab. πŸ’…


✨ NAME ✨

Here's the whole squad, no cap. These are all implementations of splay and red-black trees:

SPLAY_PROTOTYPE, SPLAY_GENERATE, SPLAY_ENTRY, SPLAY_HEAD, SPLAY_INITIALIZER, SPLAY_ROOT, SPLAY_EMPTY, SPLAY_NEXT, SPLAY_MIN, SPLAY_MAX, SPLAY_FIND, SPLAY_LEFT, SPLAY_RIGHT, SPLAY_FOREACH, SPLAY_INIT, SPLAY_INSERT, SPLAY_REMOVE, RB_PROTOTYPE, RB_PROTOTYPE_STATIC, RB_GENERATE, RB_GENERATE_STATIC, RB_ENTRY, RB_HEAD, RB_INITIALIZER, RB_ROOT, RB_EMPTY, RB_NEXT, RB_PREV, RB_MIN, RB_MAX, RB_FIND, RB_NFIND, RB_LEFT, RB_RIGHT, RB_PARENT, RB_FOREACH, RB_FOREACH_SAFE, RB_FOREACH_REVERSE, RB_FOREACH_REVERSE_SAFE, RB_INIT, RB_INSERT, RB_REMOVE β€” implementations of splay and red-black trees 🌳


πŸ“š LIBRARY πŸ“š

Utility functions from BSD systems, aka libbsd, link it with -lbsd. 🫢


🌈 SYNOPSIS 🌈

#include <sys/tree.h>
(See libbsd(7) for include usage.)

SPLAY_PROTOTYPE(NAME, TYPE, FIELD, CMP);

SPLAY_GENERATE(NAME, TYPE, FIELD, CMP);

SPLAY_ENTRY(TYPE);

SPLAY_HEAD(HEADNAME, TYPE);

struct TYPE *
SPLAY_INITIALIZER(SPLAY_HEAD *head);

SPLAY_ROOT(SPLAY_HEAD *head);

int
SPLAY_EMPTY(SPLAY_HEAD *head);

struct TYPE *
SPLAY_NEXT(NAME, SPLAY_HEAD *head, struct TYPE *elm);

struct TYPE *
SPLAY_MIN(NAME, SPLAY_HEAD *head);

struct TYPE *
SPLAY_MAX(NAME, SPLAY_HEAD *head);

struct TYPE *
SPLAY_FIND(NAME, SPLAY_HEAD *head, struct TYPE *elm);

struct TYPE *
SPLAY_LEFT(struct TYPE *elm, SPLAY_ENTRY NAME);

struct TYPE *
SPLAY_RIGHT(struct TYPE *elm, SPLAY_ENTRY NAME);

SPLAY_FOREACH(VARNAME, NAME, SPLAY_HEAD *head);

void
SPLAY_INIT(SPLAY_HEAD *head);

struct TYPE *
SPLAY_INSERT(NAME, SPLAY_HEAD *head, struct TYPE *elm);

struct TYPE *
SPLAY_REMOVE(NAME, SPLAY_HEAD *head, struct TYPE *elm);

RB_PROTOTYPE(NAME, TYPE, FIELD, CMP);

RB_PROTOTYPE_STATIC(NAME, TYPE, FIELD, CMP);

RB_GENERATE(NAME, TYPE, FIELD, CMP);

RB_GENERATE_STATIC(NAME, TYPE, FIELD, CMP);

RB_ENTRY(TYPE);

RB_HEAD(HEADNAME, TYPE);

RB_INITIALIZER(RB_HEAD *head);

struct TYPE *
RB_ROOT(RB_HEAD *head);

int
RB_EMPTY(RB_HEAD *head);

struct TYPE *
RB_NEXT(NAME, RB_HEAD *head, struct TYPE *elm);

struct TYPE *
RB_PREV(NAME, RB_HEAD *head, struct TYPE *elm);

struct TYPE *
RB_MIN(NAME, RB_HEAD *head);

struct TYPE *
RB_MAX(NAME, RB_HEAD *head);

struct TYPE *
RB_FIND(NAME, RB_HEAD *head, struct TYPE *elm);

struct TYPE *
RB_NFIND(NAME, RB_HEAD *head, struct TYPE *elm);

struct TYPE *
RB_LEFT(struct TYPE *elm, RB_ENTRY NAME);

struct TYPE *
RB_RIGHT(struct TYPE *elm, RB_ENTRY NAME);

struct TYPE *
RB_PARENT(struct TYPE *elm, RB_ENTRY NAME);

RB_FOREACH(VARNAME, NAME, RB_HEAD *head);

RB_FOREACH_SAFE(VARNAME, NAME, RB_HEAD *head, TEMP_VARNAME);

RB_FOREACH_REVERSE(VARNAME, NAME, RB_HEAD *head);

RB_FOREACH_REVERSE_SAFE(VARNAME, NAME, RB_HEAD *head, TEMP_VARNAME);

void
RB_INIT(RB_HEAD *head);

struct TYPE *
RB_INSERT(NAME, RB_HEAD *head, struct TYPE *elm);

struct TYPE *
RB_REMOVE(NAME, RB_HEAD *head, struct TYPE *elm);

πŸ’– DESCRIPTION πŸ’–

So basically, these macros define data structures for different types of trees: splay trees and red-black trees. Two iconic flavors, pick your fighter. πŸ¦‹

In the macro definitions, a couple of things are doing heavy lifting:

  • TYPE is the name tag of a user defined structure, and that struct must contain a field named FIELD, of type SPLAY_ENTRY or RB_ENTRY.
  • HEADNAME is the name tag of a user defined structure that must be declared using the macros SPLAY_HEAD() or RB_HEAD().
  • NAME has to be a unique name prefix for every tree that is defined. Unique, bestie. Don't be out here reusing names. πŸ™ˆ

The function prototypes get declared with SPLAY_PROTOTYPE, RB_PROTOTYPE, or RB_PROTOTYPE_STATIC. The function bodies get generated with SPLAY_GENERATE, RB_GENERATE, or RB_GENERATE_STATIC. And yes, there are examples further down in the page that explain how these macros are actually used β€” iykyk, keep reading. ✨


🌸 SPLAY TREES 🌸

A splay tree is a self-organizing data structure. Every operation on the tree causes a splay to happen. The splay moves the requested node to the root of the tree and partly rebalances it. It's giving main character energy β€” whatever you touch ends up at the top. πŸ‘‘

This has the benefit that request locality causes faster lookups, since the requested nodes move to the top of the tree. On the other hand β€” and this is the tradeoff, no cap β€” every lookup causes memory writes.

The Balance Theorem bounds the total access time for m operations and n inserts on an initially empty tree as O((m + n)lg n). The amortized cost for a sequence of m accesses to a splay tree is O(lg n). Math ate. πŸ”₯

A splay tree is headed by a structure defined by the SPLAY_HEAD() macro. A SPLAY_HEAD structure is declared as follows:

SPLAY_HEAD(HEADNAME, TYPE) head;

where HEADNAME is the name of the structure to be defined, and struct TYPE is the type of the elements to be inserted into the tree.

The SPLAY_ENTRY() macro declares a structure that allows elements to be connected in the tree.

In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the SPLAY_PROTOTYPE() macro, where NAME is a unique identifier for this particular tree. The TYPE argument is the type of the structure that is being managed by the tree. The FIELD argument is the name of the element defined by SPLAY_ENTRY().

The function bodies are generated with the SPLAY_GENERATE() macro. It takes the same arguments as the SPLAY_PROTOTYPE() macro, but should be used only once. One time. That's it. Don't be greedy. πŸ’…

Finally, the CMP argument is the name of a function used to compare trees' nodes with each other. That function takes two arguments of type struct TYPE *. Here's the vibe check on its return value:

  • If the first argument is smaller than the second, the function returns a value smaller than zero.
  • If they are equal, the function returns zero.
  • Otherwise, it should return a value greater than zero.

The compare function defines the order of the tree elements. ⭐

The SPLAY_INIT() macro initializes the tree referenced by head.

The splay tree can also be initialized statically by using the SPLAY_INITIALIZER() macro, like this:

SPLAY_HEAD(HEADNAME, TYPE) head = SPLAY_INITIALIZER(&head);

The SPLAY_INSERT() macro inserts the new element elm into the tree. Upon success, NULL is returned. If a matching element already exists in the tree, the insertion is aborted, and a pointer to the existing element is returned. Polite of it to tell you who was already there, honestly.

The SPLAY_REMOVE() macro removes the element elm from the tree pointed by head. Upon success, a pointer to the removed element is returned. NULL is returned if elm is not present in the tree.

The SPLAY_FIND() macro can be used to find a particular element in the tree:

struct TYPE find, *res;
find.key = 30;
res = SPLAY_FIND(NAME, &head, &find);

The SPLAY_ROOT(), SPLAY_MIN(), SPLAY_MAX(), and SPLAY_NEXT() macros can be used to traverse the tree:

for (np = SPLAY_MIN(NAME, &head); np != NULL; np = SPLAY_NEXT(NAME, &head, np))

Or, for simplicity, one can use the SPLAY_FOREACH() macro:

SPLAY_FOREACH(np, NAME, &head)

The SPLAY_EMPTY() macro should be used to check whether a splay tree is empty. πŸ•³οΈ


🧚 RED-BLACK TREES 🧚

A red-black tree is a binary search tree with the node color as an extra attribute. It fulfills a set of conditions β€” and yes, they're numbered, we love a checklist:

  1. every search path from the root to a leaf consists of the same number of black nodes,
  2. each red node (except for the root) has a black parent,
  3. each leaf node is black.

Every operation on a red-black tree is bounded as O(lg n). The maximum height of a red-black tree is 2lg (n+1). Balanced queen behavior, periodt. πŸ‘‘

A red-black tree is headed by a structure defined by the RB_HEAD() macro. A RB_HEAD structure is declared as follows:

RB_HEAD(HEADNAME, TYPE) head;

where HEADNAME is the name of the structure to be defined, and struct TYPE is the type of the elements to be inserted into the tree.

The RB_ENTRY() macro declares a structure that allows elements to be connected in the tree.

In order to use the functions that manipulate the tree structure, their prototypes need to be declared with the RB_PROTOTYPE() or RB_PROTOTYPE_STATIC() macros, where NAME is a unique identifier for this particular tree. The TYPE argument is the type of the structure that is being managed by the tree. The FIELD argument is the name of the element defined by RB_ENTRY().

The function bodies are generated with the RB_GENERATE() or RB_GENERATE_STATIC() macros. These macros take the same arguments as the RB_PROTOTYPE() and RB_PROTOTYPE_STATIC() macros, but should be used only once.

Finally, the CMP argument is the name of a function used to compare trees' nodes with each other. That function takes two arguments of type struct TYPE *. Same deal as before:

  • If the first argument is smaller than the second, the function returns a value smaller than zero.
  • If they are equal, the function returns zero.
  • Otherwise, it should return a value greater than zero.

The compare function defines the order of the tree elements. πŸ“

The RB_INIT() macro initializes the tree referenced by head.

The red-black tree can also be initialized statically by using the RB_INITIALIZER() macro, like this:

RB_HEAD(HEADNAME, TYPE) head = RB_INITIALIZER(&head);

The RB_INSERT() macro inserts the new element elm into the tree. Upon success, NULL is returned. If a matching element already exists in the tree, the insertion is aborted, and a pointer to the existing element is returned.

The RB_REMOVE() macro removes the element elm from the tree pointed by head. RB_REMOVE() returns elm. 🫢

The RB_FIND() and RB_NFIND() macros can be used to find a particular element in the tree. RB_FIND() finds the node with the same key as elm. RB_NFIND() finds the first node greater than or equal to the search key.

struct TYPE find, *res;
find.key = 30;
res = RB_FIND(NAME, &head, &find);

The RB_ROOT(), RB_MIN(), RB_MAX(), RB_NEXT(), and RB_PREV() macros can be used to traverse the tree:

for (np = RB_MIN(NAME, &head); np != NULL; np = RB_NEXT(NAME, &head, np))

Or, for simplicity, one can use the RB_FOREACH() or RB_FOREACH_REVERSE() macros:

RB_FOREACH(np, NAME, &head)

The macros RB_FOREACH_SAFE() and RB_FOREACH_REVERSE_SAFE() traverse the tree referenced by head in a forward or reverse direction respectively, assigning each element in turn to np. However, unlike their unsafe counterparts, they permit both the removal of np as well as freeing it from within the loop safely without interfering with the traversal. This is the iconic one, bestie β€” delete stuff mid-loop and the traversal doesn't even flinch. ✨

The RB_EMPTY() macro should be used to check whether a red-black tree is empty. πŸ•³οΈ

🌟 Okay that's where part 1 wraps its little section β€” but we are not done, more parts follow, so stay tuned! 🌟

✨ EXAMPLES ✨

okay bestie, this example is literally showing you how to declare a red-black tree holding integers. values get inserted into it, the contents of the tree get printed in order, and then β€” last but not least β€” the internal structure of the tree gets printed. it's giving tutorial slay. πŸ’–

          #include <sys/tree.h>
          #include <err.h>
          #include <stdio.h>
          #include <stdlib.h>

          struct node {
                  RB_ENTRY(node) entry;
                  int i;
          };

          int     intcmp(struct node *, struct node *);
          void    print_tree(struct node *);

          int
          intcmp(struct node *e1, struct node *e2)
          {
                  return (e1->i < e2->i ? -1 : e1->i > e2->i);
          }

          RB_HEAD(inttree, node) head = RB_INITIALIZER(&head);
          RB_PROTOTYPE(inttree, node, entry, intcmp)
          RB_GENERATE(inttree, node, entry, intcmp)

          int testdata[] = {
                  20, 16, 17, 13, 3, 6, 1, 8, 2, 4, 10, 19, 5, 9, 12, 15, 18,
                  7, 11, 14
          };

          void
          print_tree(struct node *n)
          {
                  struct node *left, *right;

                  if (n == NULL) {
                          printf("nil");
                          return;
                  }
                  left = RB_LEFT(n, entry);
                  right = RB_RIGHT(n, entry);
                  if (left == NULL && right == NULL)
                          printf("%d", n->i);
                  else {
                          printf("%d(", n->i);
                          print_tree(left);
                          printf(",");
                          print_tree(right);
                          printf(")");
                  }
          }

          int
          main(void)
          {
                  int i;
                  struct node *n;

                  for (i = 0; i < sizeof(testdata) / sizeof(testdata[0]); i++) {
                          if ((n = malloc(sizeof(struct node))) == NULL)
                                  err(1, NULL);
                          n->i = testdata[i];
                          RB_INSERT(inttree, &head, n);
                  }

                  RB_FOREACH(n, inttree, &head) {
                          printf("%d\n", n->i);
                  }
                  print_tree(RB_ROOT(&head));
                  printf("\n");
                  return (0);
          }

🌸 SEE ALSO 🌸

if you're in your curious era, queue(3bsd) is right there. go read it, iykyk. πŸ’…


πŸ¦‹ HISTORY πŸ¦‹

the tree macros first appeared in FreeBSD 4.6. iconic origin story, periodt. ✨


πŸ’… NOTES πŸ’…

okay so trying to free a tree in the following way? yeah, that's a common error. don't do it bestie.

             SPLAY_FOREACH(var, NAME, &head) {
                     SPLAY_REMOVE(NAME, &head, var);
                     free(var);
             }
             free(head);

since var is free'd, the FOREACH() macro refers to a pointer that may have been reallocated already. proper code needs a second variable. so do this instead:

             for (var = SPLAY_MIN(NAME, &head); var != NULL; var = nxt) {
                     nxt = SPLAY_NEXT(NAME, &head, var);
                     SPLAY_REMOVE(NAME, &head, var);
                     free(var);
             }

πŸ‘‘ AUTHORS πŸ‘‘

the author of the tree macros is Niels Provos. king behavior, honestly πŸ‘‘πŸ’–


this lil page came from the Debian squad, dated May 10, 2019, and it's all about tree(3bsd). mwah πŸ’‹

πŸ“¦ where this page comes from