Commits

Anonymous committed 5334221

actually don't use/modify reference on list _del, and change bstree
_del_all to _del_sub

Comments (0)

Files changed (3)

     if (p)                                                 \
         *p = *q;                                           \
 }                                                          \
-void Name##_del_all(Name **q)                              \
+void Name##_del_sub(Name **q)                              \
 {                                                          \
     Name *n;                                               \
     if (!q || !*q)                                         \
         return;                                            \
     n  = *q;                                               \
-    Name##_del_all(&n->left);                              \
-    Name##_del_all(&n->right);                             \
+    Name##_del_sub(&n->left);                              \
+    Name##_del_sub(&n->right);                             \
     if (n->parent) {                                       \
         if (n->parent->left == n) n->parent->left  = NULL; \
         else                      n->parent->right = NULL; \
         Listn_insert(&head, i);
     Listn_for_each(head, print_int);
     printf("\n");
-    Listn_del(&head->prev->prev);
+    Listn_del(head->prev->prev);
 
     // reverse traversal
     Listn *p = head->prev;
     /* bstree demo */
     Btree *root = NULL;
     printf("Btree inserting: ");
-    for (i = 20; i; i--) {
+    for (i = 50; i; i--) {
         printf("%zu,", j = rand() % 20);
         Btree_insert(&root, j);
     }
     printf("\ndepth: %zu\ncount: %zu\n", btree_depth(root, 0), btree_count(root));
     Btree_for_each(root, print_int);
-    Btree *btn = Btree_find(root, j);
-    printf("\nfind then del %d\n", btn ? btn->data : -1);
-    Btree_del(&btn);
+    // Up to use to not _del_sub on a separate pointer to root...
+    Btree *btn;
+    do btn = Btree_find(root, j = rand() % 20);
+    while (!btn || btn == root);
+    printf("\nfind, print subtree, delete subtree for %zu\n", j);
+    Btree_for_each(btn, print_int);
+    Btree_del_sub(&btn);
+    printf("\nbtn is%s NULL\n", btn ? " not" : "");
     Btree_for_each(root, print_int);
-    printf("\n");
-    printf("root: %d\n", root->data);
+
+    printf("\nroot: %d\n", root->data);
     printf("del root\n");
     Btree_del(&root);
     printf("root: %d\n", root->data);
     printf("root: %d\n", root->data);
     Btree_for_each(root, print_int);
     printf("\ndepth: %zu\ncount: %zu\n", btree_depth(root, 0), btree_count(root));
-    Btree_del_all(&root);
+    Btree_del_sub(&root);
 
     return 0;
 }
     *n = (Name){ n, n, t };                       \
     return n;                                     \
 }                                                 \
-void Name##_del(Name **l)                         \
+void Name##_del(Name *l)                          \
 {                                                 \
-    Name *p;                                      \
-    if (!l || !*l)                                \
+    if (!l)                                       \
         return;                                   \
-    p = *l;                                       \
-    p->next->prev = p->prev;                      \
-    p->prev->next = p->next;                      \
-    *l = (p->next == p) ? NULL : p->next;         \
-    free(p);                                      \
+    l->next->prev = l->prev;                      \
+    l->prev->next = l->next;                      \
+    free(l);                                      \
 }                                                 \
 void Name##_del_all(Name **l)                     \
 {                                                 \