2011-03-30 13:49:47 +00:00
|
|
|
.TH AVL 2
|
|
|
|
.SH NAME
|
2016-12-22 22:47:41 +00:00
|
|
|
avlcreate,
|
|
|
|
avlinsert,
|
|
|
|
avldelete,
|
|
|
|
avllookup,
|
|
|
|
avlnext,
|
|
|
|
avlprev \- Balanced binary search tree routines
|
2011-03-30 13:49:47 +00:00
|
|
|
.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
|
2011-03-30 13:49:47 +00:00
|
|
|
.EX
|
|
|
|
#include <u.h>
|
|
|
|
#include <libc.h>
|
|
|
|
#include <avl.h>
|
2016-12-22 22:47:41 +00:00
|
|
|
|
2011-03-30 13:49:47 +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;
|
2011-03-30 13:49:47 +00:00
|
|
|
};
|
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);
|
2017-04-22 18:59:37 +00:00
|
|
|
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);
|
|
|
|
|
2011-03-30 13:49:47 +00:00
|
|
|
.EE
|
|
|
|
.SH DESCRIPTION
|
2016-12-22 22:47:41 +00:00
|
|
|
These routines allow creation and maintenance of in-memory balanced
|
|
|
|
binary search trees.
|
2011-03-30 13:49:47 +00:00
|
|
|
.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
|
2011-03-30 13:49:47 +00:00
|
|
|
.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
|
2011-03-30 13:49:47 +00:00
|
|
|
respectively less than,
|
2016-12-22 22:47:41 +00:00
|
|
|
equal to, or greater than the second.
|
2011-03-30 13:49:47 +00:00
|
|
|
.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
|
2017-04-22 18:59:37 +00:00
|
|
|
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,
|
2017-04-22 18:59:37 +00:00
|
|
|
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
|
2017-04-22 18:59:37 +00:00
|
|
|
it. It returns nil if no matching key is found.
|
2011-03-30 13:49:47 +00:00
|
|
|
.PP
|
2017-04-22 19:28:02 +00:00
|
|
|
.I Avlmin
|
|
|
|
returns the minimum
|
|
|
|
.B Avl
|
|
|
|
node in the tree and
|
|
|
|
.I avlmax
|
|
|
|
returns the maximum node.
|
2011-03-30 13:49:47 +00:00
|
|
|
.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
|
2011-03-30 13:49:47 +00:00
|
|
|
and
|
|
|
|
.I avlprev
|
2016-12-22 22:47:41 +00:00
|
|
|
returns the previous node.
|
2011-03-30 13:49:47 +00:00
|
|
|
.SH EXAMPLES
|
2016-12-22 22:47:41 +00:00
|
|
|
Intended usage is to embed the
|
2011-03-30 13:49:47 +00:00
|
|
|
.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.
|
2011-03-30 13:49:47 +00:00
|
|
|
.IP
|
|
|
|
.EX
|
|
|
|
typedef struct Node {
|
|
|
|
Avl;
|
2016-12-22 22:47:41 +00:00
|
|
|
char *key;
|
|
|
|
int val;
|
2011-03-30 13:49:47 +00:00
|
|
|
} 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;
|
|
|
|
}
|
2011-03-30 13:49:47 +00:00
|
|
|
\fI\&...\fP
|
2016-12-24 23:58:31 +00:00
|
|
|
Avltree *t = avlcreate(nodecmp);
|
2016-12-22 22:47:41 +00:00
|
|
|
|
2011-03-30 13:49:47 +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
|
2011-03-30 13:49:47 +00:00
|
|
|
.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).
|