]> git.kaiwu.me - klib.git/commitdiff
added a simpler tree deallocator
authorHeng Li <lh3@me.com>
Sun, 15 Apr 2018 01:51:26 +0000 (21:51 -0400)
committerHeng Li <lh3@me.com>
Sun, 15 Apr 2018 01:51:26 +0000 (21:51 -0400)
kavl.h

diff --git a/kavl.h b/kavl.h
index a1c40f3865730245cdb11a07bc339466c5c9035d..6e9102ccb5baf0e4f5b1d292f27462d8321f2a1f 100644 (file)
--- a/kavl.h
+++ b/kavl.h
@@ -251,6 +251,20 @@ int main(void) {
                return p; \
        }
 
+#define kavl_free(__type, __head, __root, __free) do { \
+               __type *_p, *_q; \
+               for (_p = __root; _p; _p = _q) { \
+                       if (_p->__head.p[0] == 0) { \
+                               _q = _p->__head.p[1]; \
+                               __free(_p); \
+                       } else { \
+                               _q = _p->__head.p[0]; \
+                               _p->__head.p[0] = _q->__head.p[1]; \
+                               _q->__head.p[1] = _p; \
+                       } \
+               } \
+       } while (0)
+
 #define __KAVL_ITR(suf, __scope, __type, __head, __cmp) \
        struct kavl_itr_##suf { \
                const __type *stack[KAVL_MAX_DEPTH], **top, *right; /* _right_ points to the right child of *top */ \