plan9fox/sys/man/2/avl

133 lines
2.7 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, int dir);
2018-06-08 16:37:39 +00:00
Avl *avlmin(Avltree *tree);
Avl *avlmax(Avltree *tree);
2016-12-22 22:47:41 +00:00
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
searches for a given key and returns
the closest node less than the given key,
2017-04-24 15:50:03 +00:00
equal to,
or the closest node greater than the key depending on whether
.I dir
2017-04-24 15:50:03 +00:00
is less than, equal to, or greater than zero, respectively. If
.I dir
is zero and there is no matching key, it returns
.BR nil .
2016-12-22 22:47:41 +00:00
.I Avldelete
removes the node matching the key from the tree and returns
it. It returns nil if no matching key is found.
.PP
.I Avlmin
returns the minimum
.B Avl
node in the tree and
.I avlmax
returns the maximum node.
.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).