Commits

Enrico Franchi committed ecf1cd2

Removed bug with tree traversal.

  • Participants
  • Parent commits 0d1cf9a

Comments (0)

Files changed (1)

File src/fibomodule.c

 Fibonacci_Tree_Traverse(Fibonacci_Tree *tree, visitproc visit, void *arg)
 {
     struct _STACK *stack = NULL;
-    if(tree->root.next != &tree->root) {
-        stack_push(tree->root.next, stack);
+    Fibonacci_Node* current_root = tree->root.next;
+    Fibonacci_Node* node;
+
+
+    for(; current_root != &tree->root;
+          current_root = current_root->next) {
+        stack_push(current_root, stack);
     }
+
     do {
-        Fibonacci_Node* node;
         stack_pop(node, stack);
         if(node == NULL) {
             return 0;
         } else {
             Py_VISIT(node->value);
             if(node->child != NULL) {
-                stack_push(node->child, stack);
-            }
-            if(node->next != &tree->root) {
-                stack_push(node->next, stack);
+                if (node->child != NULL) {
+                    Fibonacci_Node* list_sentinel = node->child;
+                    Fibonacci_Node* current_child = node->child;
+                    do {
+                        stack_push(current_child, stack);
+                        current_child = current_child->next;
+                    } while (list_sentinel != current_child);
+                }
             }
         }
     } while(1);
 
     for(; current_root != &tree->root;
           current_root = current_root->next) {
-        printf("Inserting root %p\n", current_root);
         stack_push(current_root, it->stack);
     }
 
     it->counter--;
 
     stack_pop(item_node, it->stack);
-    printf("%ul - %p\n", it->counter, item_node);
     assert(item_node != NULL);
     item = item_node->value;
     Py_INCREF(item);
         Fibonacci_Node* list_sentinel = item_node->child;
         Fibonacci_Node* current_child = item_node->child;
         do {
-            printf("from %p inserting %p\n", item_node, current_child);
             stack_push(current_child, it->stack);
             current_child = current_child->next;
         } while (list_sentinel != current_child);