plan9fox/sys/man/2/avl

120 lines
2.3 KiB
Plaintext
Raw Normal View History

.TH AVL 2
.SH NAME
2016-12-22 22:47:41 +00:00
avlcreate,
avlinsert,
avldelete,
avllookup,
avlnext,
avlprev \- Balanced binary search tree routines
.SH SYNOPSIS
2016-12-22 22:47:41 +00:00
.ta 0.75i 1.5i 2.25i 3i 3.75i 4.5i
.\" .ta 0.7i +0.7i +0.7i +0.7i +0.7i +0.7i +0.7i
.EX
#include <u.h>
#include <libc.h>
#include <avl.h>
2016-12-22 22:47:41 +00:00
typedef struct Avl Avl;
2016-12-22 22:47:41 +00:00
typedef struct Avltree Avltree;
struct Avl {
Avl *c[2]; /* children */
Avl *p; /* parent */
schar balance; /* balance factor */
};
struct Avltree {
int (*cmp)(Avl*, Avl*);
Avl *root;
};
2016-12-22 22:47:41 +00:00
Avltree *avlcreate(int(*cmp)(Avl*, Avl*));
Avl *avlinsert(Avltree *tree, Avl *new);
Avl *avldelete(Avltree *tree, Avl *key);
Avl *avllookup(Avltree *tree, Avl *key);
Avl *avlnext(Avl *n);
Avl *avlprev(Avl *n);
.EE
.SH DESCRIPTION
2016-12-22 22:47:41 +00:00
These routines allow creation and maintenance of in-memory balanced
binary search trees.
.PP
2016-12-22 22:47:41 +00:00
An empty tree is created by calling
.I avlcreate
with a comparison function as an argument.
The comparison function must take two pointers to
.B Avl
2016-12-22 22:47:41 +00:00
structures and return an integer less than, equal to, or
greater than 0 as the first is
respectively less than,
2016-12-22 22:47:41 +00:00
equal to, or greater than the second.
.PP
2016-12-22 22:47:41 +00:00
.I Avlinsert
adds a new
node into the tree and returns an existing
node with the same key that has been removed
from the tree and may be freed.
.I Avllookup
returns the
2016-12-22 22:47:41 +00:00
node that matches the key or
.B nil
2016-12-22 22:47:41 +00:00
if no node matches.
.I Avldelete
removes the node matching the key from the tree and returns
it. It returns nil of no matching key is found.
.PP
.I Avlnext
2016-12-22 22:47:41 +00:00
returns the next
.B Avl
node in an in-order walk of the AVL tree
and
.I avlprev
2016-12-22 22:47:41 +00:00
returns the previous node.
.SH EXAMPLES
2016-12-22 22:47:41 +00:00
Intended usage is to embed the
.B Avl
2016-12-22 22:47:41 +00:00
structure anonymously.
For example, the following will create a key-value store
with strings as keys and integers as values.
.IP
.EX
typedef struct Node {
Avl;
2016-12-22 22:47:41 +00:00
char *key;
int val;
} Node;
2016-12-22 22:47:41 +00:00
int
nodecmp(Avl *la, Avl *lb)
{
Node *a, *b;
a = (Node*)la;
b = (Node*)lb;
return strcmp(a->key, b->key);
}
int
2016-12-23 00:44:45 +00:00
get(Avltree *t, char *key)
2016-12-22 22:47:41 +00:00
{
Node *h, n;
n.key = key;
2016-12-23 00:44:45 +00:00
h = (Node*)avllookup(t, &n);
2016-12-22 22:47:41 +00:00
return h ? h->val : -1;
}
\fI\&...\fP
Avltree *t = avlcreate(nodecmp);
2016-12-22 22:47:41 +00:00
.EE
.SH SOURCE
.B /sys/src/libavl
.SH SEE ALSO
2016-12-22 22:47:41 +00:00
.nf
Donald Knuth, ``The Art of Computer Programming'', Volume 3. Section 6.2.3
.SH DIAGNOSTICS
2016-12-22 22:47:41 +00:00
.I Avlcreate
returns nil on error.
.SH HISTORY
This implementation was written for 9front (Dec, 2016).