unsigned char dir[KAVL_MAX_DEPTH]; \
int i, d = 0, cmp; \
fake.__head.p[0] = *root_, fake.__head.p[1] = 0; \
- for (cmp = -1, p = &fake; cmp; cmp = __cmp(x, p)) { \
- int which = (cmp > 0); \
- dir[d] = which; \
- path[d++] = p; \
- p = p->__head.p[which]; \
- if (p == 0) return 0; \
+ if (x) { \
+ for (cmp = -1, p = &fake; cmp; cmp = __cmp(x, p)) { \
+ int which = (cmp > 0); \
+ dir[d] = which; \
+ path[d++] = p; \
+ p = p->__head.p[which]; \
+ if (p == 0) return 0; \
+ } \
+ } else { \
+ for (p = &fake; p; p = p->__head.p[0]) \
+ dir[d] = 0, path[d++] = p; \
+ p = path[--d]; \
} \
for (i = 1; i < d; ++i) --path[i]->__head.size; \
if (p->__head.p[1] == 0) { /* ((1,.)2,3)4 => (1,3)4; p=2 */ \
*
* @param suf name suffix used in KAVL_INIT()
* @param proot pointer to the root of the tree (in/out: root may change)
- * @param x node value to delete (in)
+ * @param x node value to delete; if NULL, delete the first node (in)
*
* @return node removed from the tree if present, or NULL if absent
*/
#define kavl_erase(suf, proot, x) kavl_erase_##suf(proot, x)
+#define kavl_erase_first(suf, proot) kavl_erase_##suf(proot, 0)
#define kavl_itr_t(suf) struct kavl_itr_##suf
#define kavl_itr_first(suf, root, itr) kavl_itr_first_##suf(root, itr)