nodes

B

Bill Cunningham

I have found a page that more simply takes about trees but I have goofed
up here. I want to write an insert function.

#include <stdio.h>

struct node {
int data;
struct node *left, *right;
};

struct node *insert(struct node *node, int target)
{
if (node == NULL)
return 1;
else {
if (target < node->data)
printf("right");
if (target > node->data)
printf("left");
}
return 0;
}

int main()
{
struct node s;
s->data = {
1};
printf("%s\n", insert(s, 1));
}

p.c: In function `insert':
p.c:11: warning: return makes pointer from integer without a cast
p.c: In function `main':
p.c:24: invalid type argument of `->'
p.c:24: parse error before '{' token
p.c:26: incompatible type for argument 1 of `insert'


Bill
 
B

Bill Cunningham

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;

Should be in there.

Bill
 
C

cognacc

    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
 
P

Paul N

    I have found a page that more simply takes about trees but I have goofed
up here. I want to write an insert function.

You need to decide what you want that function to return. You seem to
have three different ideas at the moment.
#include <stdio.h>

struct node {
    int data;
    struct node *left, *right;

};

struct node *insert(struct node *node, int target)

Here you say that you want it to return a pointer to a node.
{
    if (node == NULL)
        return 1;

Here you return a number.
    else {
        if (target < node->data)
            printf("right");
        if (target > node->data)
            printf("left");
    }
    return 0;

(This could be either a number or a pointer to node.)
}

int main()
{
    struct node s;
    s->data = {
    1};
    printf("%s\n", insert(s, 1));

But here you seem to be expecting it to return a string!
}

p.c: In function `insert':
p.c:11: warning: return makes pointer from integer without a cast

This seems clear enough. You're using return to return an integer (1),
but you said you were going to return a pointer (struct node*), and
you haven't used a cast to convert from one to the other. If you
really wanted to convert a number to a pointer (which you don't, here)
then you would use a cast.
p.c: In function `main':
p.c:24: invalid type argument of `->'

You've said s is a struct node. So to access its members you need to
use the . operator, not the -> operator. Alternatively, you could
change s to be a pointer to a struct node, and then it would be
correct to use the -> operatotr, but you would need to first make s
actually point to something.
p.c:24: parse error before '{' token

I don't think you can do anything like s->data = {1};
p.c:26: incompatible type for argument 1 of `insert'

This says that when you call insert, the type of the first argument is
wrong. Again, this is because s is a struct node when insert is
expecting a pointer to a struct node.

I would suggest writing a program that simply uses a struct to store
some data, before trying to master trees. Get the basics sorted first.

Hope that helps.
Paul.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

pointer array problem? 7
strange warning 192
struct problems 71
malloc 11
Queue in C 25
tree 6
puzzling error 49
Adding adressing of IPv6 to program 1

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top