Re: nodes
- From: cognacc <michael.cognacc@xxxxxxxxx>
- Date: Fri, 11 Sep 2009 01:36:42 -0700 (PDT)
On Sep 11, 4:14 am, "Bill Cunningham" <nos...@xxxxxxxxxxxxx> wrote:
"Bill Cunningham" <nos...@xxxxxxxxxxxxx> wrote in message
news:4aa99f79$0$23757$bbae4d71@xxxxxxxxxxxxxxxxxxxxxx
int main()
{
struct node s;
s->data = {
1};
printf("%s\n", insert(s, 1));
}
What am I doing here. I have caught an error now looking at the code.
But I don't think it's my main problem. struct node s;
struct node *pp;
pp=&s;
Well you accessed struct node s as a pointer (using s->data) instead
of
s.data for objects.
after that your insert function expects a pointer (struct node *node)
It's a bit hard seeing what you corrected. but i guess
you pass pp to the function insead of s right?.
I think you can just do insert(&s, 1);
Then your function return a type of struct node
and you try and print that returned struct node pointer.
in your printf call printf("%s\n", insert(s, 1));
you are telling it to print a character array (%s)
and in your function definition(your code block)
your are saying you are returning.
0 or 1;
So maybe you want to return 0 if you are not
inserting and 1 if there is a place to insert.
as i can see there is many thing wrong with this function.
q1: how do you traverse the tree to insert, often done recursively.
q2: you are testing your object (node) against NULL what do you mean
by that.
That should be a defined node, or do you want to insert "empty" nodes
into the tree.
I think you should have a tip/root/head pointer or node for the root
of your tree
I think you should write down some progam specification f.ex.
main:
create root pointer
create and initlize node to insert
call insert_tree function
insert_tree:
test for empty tree ?
compare size, traverse left or right according to comparation
repeat until reached correct position. insert node
You should probably read again about trees and traversal of trees.
then have a break and come back and look at your code again.
I think there are a few misunderstandings here.
So maybe are you sure its a good tutorial? link?
I think your basic insert function should be something like
---
returnwhat? insert(struct node *node, int target)
{
if (node == NULL) // what about this?
return 1;
else {
if (target < node->data)
printf("right");
insert(node->right, target) // using recursion
if (target > node->data)
printf("left");
insert(node->left, target) // using recursion
}
return 0;
}
---
there are still something wrong with this i think
unfortunately im running out of time, have to go to work now.
regards michael
.
- Prev by Date: Re: J'accuse
- Next by Date: Re: Confused with valgrind and memmove
- Previous by thread: Re: nodes
- Next by thread: Re: nodes
- Index(es):
Relevant Pages
|