Hi people. I have a remove function that should set a hide member of a node to 1 instead of actually remove the node. The function works well when I insert and remove top and tap respectively (both nodes have their hide member set to 1) but when I insert and remove tap and top in this order, only top has its hidden member set to 1; tap is not found. I will attach a .zip since this program has some headers and sources.
int main(void)
{
BiTree tree;
char* target;
char sarray[12][STRSIZ];
char tarray[12][STRSIZ];
// load an array with the data to search
strcpy(sarray[hop], "hop");
strcpy(sarray[hat], "hat");
strcpy(sarray[tap], "tap");
strcpy(sarray[bat], "bat");
strcpy(sarray[tip], "tip");
strcpy(sarray[mop], "mop");
strcpy(sarray[mom], "mom");
strcpy(sarray[cat], "cat");
strcpy(sarray[zoo], "zoo");
strcpy(sarray[wax], "wax");
strcpy(sarray[top], "top");
strcpy(sarray[dip], "dip");
// initialize the binary search tree
bistree_init(&tree, compare_str, NULL);
/* run some BST operations */
printf("inserting some nodes\n");
// insert a key into the tree
if(bistree_insert(&tree, sarray[top]) != 0)
return 1;
// print the size of the tree
printf("tree size is %d\n", bistree_size(&tree));
// traverse the tree in preorder
printf("preorder traversal\n");
preorder_tree(bitree_root(&tree));
// insert a key into the tree
if(bistree_insert(&tree, sarray[tap]) != 0)
return 1;
// print the size of the tree
printf("tree size is %d\n", bistree_size(&tree));
// traverse the tree in preorder
printf("preorder traversal\n");
preorder_tree(bitree_root(&tree));
// insert a key into the tree
if(bistree_insert(&tree, sarray[cat]) != 0)
return 1;
// print the size of the tree
printf("tree size is %d\n", bistree_size(&tree));
// traverse the tree in preorder
printf("preorder traversal\n");
preorder_tree(bitree_root(&tree));
// insert a key into the tree
if(bistree_insert(&tree, sarray[bat]) != 0)
return 1;
// print the size of the tree
printf("tree size is %d\n", bistree_size(&tree));
// traverse the tree in preorder
printf("preorder traversal\n");
preorder_tree(bitree_root(&tree));
// attempt remove a node from the tree
printf("removing %s\n", sarray[tap]);
// can't remove data from the tree
if(bistree_remove(&tree, &sarray[tap]) != 0)
printf("could not find %s\n", sarray[tap]);
// the key with its data was removed
else
{
// print the size of the tree
printf("tree size is %d\n", bistree_size(&tree));
// traverse the tree in preorder
printf("preorder traversal\n");
preorder_tree(bitree_root(&tree));
}
// attempt remove a node from the tree
printf("removing %s\n", sarray[top]);
// can't remove data from the tree
if(bistree_remove(&tree, &sarray[top]) != 0)
printf("could not find %s\n", sarray[top]);
// the key with its data was removed
else
{
// print the size of the tree
printf("tree size is %d\n", bistree_size(&tree));
// traverse the tree in preorder
printf("preorder traversal\n");
preorder_tree(bitree_root(&tree));
}
// lookup
printf("looking up some nodes\n");
strcpy(tarray[0], "top");
strcpy(tarray[1], "cat");
strcpy(tarray[2], "wax");
strcpy(tarray[3], "hat");
strcpy(tarray[4], "xxx");
target = tarray[0];
// determine whether the data was found or not
if(bistree_lookup(&tree, (void**)&target) != 0)
printf("could not find %s\n", target);
else
printf("Found %s\n", target);
target = tarray[2];
// determine whether the data was found or not
if(bistree_lookup(&tree, (void**)&target) != 0)
printf("could not find %s\n", target);
else
printf("Found %s\n", target);
// destroy the tree
printf("Destroying the search tree\n");
bistree_destroy(&tree);
return EXIT_SUCCESS;
}