8 avlprev \- Balanced binary search tree routines
10 .ta 0.75i 1.5i 2.25i 3i 3.75i 4.5i
11 .\" .ta 0.7i +0.7i +0.7i +0.7i +0.7i +0.7i +0.7i
17 typedef struct Avl Avl;
18 typedef struct Avltree Avltree;
21 Avl *c[2]; /* children */
23 schar balance; /* balance factor */
26 int (*cmp)(Avl*, Avl*);
30 Avltree *avlcreate(int(*cmp)(Avl*, Avl*));
31 Avl *avlinsert(Avltree *tree, Avl *new);
32 Avl *avldelete(Avltree *tree, Avl *key);
33 Avl *avllookup(Avltree *tree, Avl *key);
39 These routines allow creation and maintenance of in-memory balanced
42 An empty tree is created by calling
44 with a comparison function as an argument.
45 The comparison function must take two pointers to
47 structures and return an integer less than, equal to, or
48 greater than 0 as the first is
49 respectively less than,
50 equal to, or greater than the second.
54 node into the tree and returns an existing
55 node with the same key that has been removed
56 from the tree and may be freed.
59 node that matches the key or
63 removes the node matching the key from the tree and returns
64 it. It returns nil of no matching key is found.
69 node in an in-order walk of the AVL tree
72 returns the previous node.
74 Intended usage is to embed the
76 structure anonymously.
77 For example, the following will create a key-value store
78 with strings as keys and integers as values.
88 nodecmp(Avl *la, Avl *lb)
94 return strcmp(a->key, b->key);
98 get(Avltree *t, char *key)
103 h = (Node*)avllookup(t, &n);
104 return h ? h->val : -1;
107 Avltree *t = avlcreate(nodecmp);
114 Donald Knuth, ``The Art of Computer Programming'', Volume 3. Section 6.2.3
117 returns nil on error.
119 This implementation was written for 9front (Dec, 2016).