|
-
October 20th, 2009, 01:50 PM
#1
PreOrder Binary Tree HELP!
Hi all. So I have to build a binary tree using the pre order method. I am passing one value at a time to my Insert() function and building it for each value passed. It's confusing me how I would build the function as I'm sort of new to recursion. Any help would be greatly appreciated. I have my Insert() function which compiles but is logically wrong. It works fine up until it reaches the end of the left tree path, then screws everything up from there. I should let you know that if the user inputs -1, this means it is NULL. (The terminal/leaf nodes)
void Insert(treeNode *&ptr, int nodeVal)
{
if (ptr == NULL)
ptr = new treeNode(nodeVal);
else if(ptr->data == -1)
return;
else
{
Insert(ptr->left, nodeVal);
Insert(ptr->right, nodeVal);
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|