Click to See Complete Forum and Search --> : Programming bin tree help...again


acid45
04-25-2003, 09:49 AM
I got it prety much. only the tree isn't sorted when reading from file and it only reads nodes to the right side of root and not to left...I fixed a bit of my code and I'm having trouble with this and need to be able to fill in the left side of the tree I don't kno wwhy it ****s up it seems liek it should work to me.


void readrest()
{
linkwork = linkhead;
while(linkwork->next != NULL)
{
work = root;
while(work != NULL && linkwork->bookkey < work->bookkey && work->bookkey > 0)
{
if(work == root)
{
if(work->left == NULL)
{
work->left = (struct node *) malloc(sizeof(struct node));
}
work = work->left;
root->left = work;
}
else
{
if(work->left == NULL)
{
work->left = (struct node *) malloc(sizeof(struct node));
}
parent = work;
work = work->left;
printf("%i\n",work->bookkey);
parent->left = work;
parent->right = NULL;
}
}
while(work != NULL && work->bookkey > 0 && linkwork->bookkey > work->bookkey)
{
if(work == root)
{
if(work->right == NULL)
{
work->right = (struct node *) malloc(sizeof(struct node));
}
work = work->right;
root->right = work;
}
else
{
if(work->right == NULL)
{
work->right = (struct node *) malloc(sizeof(struct node));
}
parent = work;
work = work->right;
printf("%i\n",work->bookkey);
parent->right = work;
parent->left = NULL;
}
}
if(work != root)
{
strcpy(work->bookauthor, linkwork->bookauthor);
work->bookkey = linkwork->bookkey;
work->bookpages = linkwork->bookpages;
strcpy(work->booktitle, linkwork->booktitle);
work->bookloaned.dd = linkwork->bookloaned.dd;
work->bookloaned.mm = linkwork->bookloaned.mm;
work->bookloaned.yyyy = linkwork->bookloaned.yyyy;
work->deleted = 0;
work->right = NULL;
work->left = NULL;
}
linkwork = linkwork->next;
}
}

If anyone could point me in the right direction. I figured out my last problem when soemone said struct linknode's next pointer pointed to a struct node which rang bells..."oh jesus "fudge" (tastes good :P) am I a moron or what??? linknode->next points to node...GAH"...I jsut need a mention of what's wrong. I miss small stupid SEVERE errors...IF I ever become a programmer I'll need to hire a personal debugger :P

acid45
04-25-2003, 06:37 PM
The question has changed so I just edited the last one and realized people who already looked at it won't look at it unless there is a reply...if they even look at it agan...so read above...PLEASE

acid45
04-26-2003, 01:56 PM
I've yet agani fixed my problem but ran into a new one. Well old one but it was just discovered after getting the left side of the tree populated...the new code is below. What seems to happen is it reads elements greater than root several time but I don't know why. I seem to have fixed that but now it writes root at the end of the tree which it shouldn't because it will only put elements of the tree is they key value != the keyvalue fo root which was already put into the tree...




This is the code where it assigns the middle element of the linked list, which the file was read into, and puts it as root of the bin tree. Otherwise it sets root to null and continues...What might be happening is it might be putting ALL of the elements to the right of root and it screws em up because root is lower than some values...I don't know how it would be doing this though...
It's not letting me use the [ code] [ /code] so I can't post my code and have it readable...ah well...I think I know how to fix it...or **** it up even more...
ANY help with this would be GREAT. Thanks.


Yeah i set the date loand for the one set as root to be 999/999/999 and the rest to be 0/0/0 and the last oen was the one that was root...If there isn't a root then it won't work...I'll try it without assigning one to root and see what happens.


Nope now none get into the tree...hmmmm