Re: nodes



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

.



Relevant Pages

  • Re: question of style
    ... end of the day the quality of the code depends more on the quality of ... or the equivalent null pointer exceptions in Java, C, or whatever? ... But you have implemented a mutable tree. ...     def get: ...
    (comp.lang.python)
  • Re: nodes
    ...     int data; ... struct node *insert ... Here you say that you want it to return a pointer to a node. ... you haven't used a cast to convert from one to the other. ...
    (comp.lang.c)
  • Re: hacker challenge - traverse a list
    ... Here is a little challenge - print the contents of a binary tree ... I assume there are no up links, otherwise the algorithm is trivial. ... space hence unbounded number of bits in a pointer? ... Left branch *not* leaf, rotate: ...
    (comp.programming)
  • Re: The complete infinite binary tree has only countably many infinite paths, says WM.
    ...   That is not what you must do. ... Denote the leading node of any sequence of nodes by its number followed ... In that infinite tree, ...
    (sci.logic)
  • Re: Help me come up with a few and simple programming challenges
    ... >Dave Vandervies wrote: ... >> language, I'd probably pass around a pointer to the list's head pointer ... >(define (sublist tree lo hi) ...
    (comp.lang.scheme)