Reputation: 327
Ok, so the problem that I am trying to find is why when I call print_inOrder(), I don't get anything printed back. The assignment I am suppose to do is write a tree algorithm in descending order (meaning higher values on left and lower values on the right). I had already created a function that created a tree a while back so I had just modified it and it works as it should; however the signature for this assignment is different from my old assignment and when I tried changing the pointers around, I got it to compile, but nothing prints out. So if someone could double check my changes and explain where I went wrong and how I need to fix it, that would make my day! ^.^
Working Original Function:
Tnode add_tnode(Tnode **current_tnode, char *value)
{
if(!(*current_tnode))
{
*current_tnode = (Tnode*) malloc(sizeof(Tnode));
(*current_tnode)->strValue = value;
//initialize the children to null
(*current_tnode)->left = NULL;
(*current_tnode)->right = NULL;
}
//Greater values go to left
else if(strcmp(value, (*current_tnode)->strValue) >= 0)
{
return add_tnode(&(*current_tnode)->left, value);
}
//Lesser values go to right
else if(strcmp(value, (*current_tnode)->strValue) < 0)
{
return add_tnode(&(*current_tnode)->right, value);
}
}
How it's called in main:
Tnode *root;
root = NULL;
//Add some nodes with string values
add_tnode(&root, "pie");
add_tnode(&root, "hi");
add_tnode(&root, "hi");
add_tnode(&root, "l");
add_tnode(&root, "leg");
//Print nodes in descending order
print_inOrder(root);
Signature Required:
Tnode *add_tnode(Tnode *current_tnode, char* value)
My Attempt to Fix:
Tnode *add_tnode(Tnode *current_tnode, char* value)
{
if(!(current_tnode))
{
current_tnode = (Tnode*) malloc(sizeof(Tnode));
(current_tnode)->strValue = value;
/* initialize the children to null */
(current_tnode)->left = NULL;
(current_tnode)->right = NULL;
}
// Greater values go to left
else if(strcmp(value, (current_tnode)->strValue) >= 0)
{
return add_tnode((current_tnode)->left, value);
}
// Lesser values go to right
else if(strcmp(value, (current_tnode)->strValue) < 0)
{
return add_tnode((current_tnode)->right, value);
}
}
How it's called in Main:
Tnode *root;
root = NULL;
//Add some nodes with string values
add_tnode(root, "pie");
add_tnode(root, "hi");
add_tnode(root, "hi");
add_tnode(root, "l");
add_tnode(root, "leg");
//Print nodes in descending order
print_inOrder(root);
Here's print_inOrder() just in case someone wants to look at it
void print_inOrder(Tnode *current_tnode)
{
if (current_tnode)
{
print_inOrder(current_tnode->left);
printf("%s\n",current_tnode->strValue);
print_inOrder(current_tnode->right);
}
}
When I run it through the gdb debugger and the print function is called, it only goes through the if statement and ends which my guess it means that the tree wasn't created at all or the pass in value is incorrect. If someone could inform me on what the mistake is, I'd greatly appreciate it!
Upvotes: 0
Views: 186
Reputation: 34585
If your previous assignment worked, all you have to do is change the print function, exploring the right nodes before exploring the left nodes.
void print_inOrder(Tnode *current_tnode)
{
if (current_tnode)
{
print_inOrder(current_tnode->right);
printf("%s\n",current_tnode->strValue);
print_inOrder(current_tnode->left);
}
}
Upvotes: 0
Reputation: 958
Your problem is that your first function takes a Tnode **
, that is a pointer to a pointer, and modifies the TNode *
it points to. Your second function takes just the pointer, and modifies the passed-in argument; the caller can't see those changes, and so nothing is ever added to the tree.
You should allocate and assign the root
node before doing anything, then change the function so that it modifies the TNode
instead of the pointer thereto.
Upvotes: 1