data tree

B

Bill Cunningham

In using my old cat,name,age allegory I am trying to make this tree. I
did some copying but will it work so far?

#include <stdio.h>
#include <stdlib.h>

struct tree {
char *name;
int age;
char *color;
int num;
struct tree *right, *left;
};

struct tree *insert(struct tree *tp, int v, char *c, int age, char *n)
{
if (tp) {
if (tp->num > v)
tp->left = insert(tp->left, v, *c, age, *n);
else if (tp->num < v)
tp->right = insert(tp->right, v, *c, age, *n);
} else if (tp = malloc(sizeof *tp)) {
tp->num = v;
tp->right = tp->left = NULL;
}
return tp;
}

Incomplete.
 
B

Bill Cunningham

Richard Heathfield said:
Bill Cunningham said:


No; in fact, it won't compile.



insert()'s third argument is char *, and c is of type char *...



insert()'s third argument is char *, and c is of type char *, so *c is
of type char, which is not compatible with char *.

If your compiler did not diagnose this message, it probably means you
didn't even bother to compile it.
Ok I think now I see what you mean. I should just have n and s then. I
didn't compile it certainly without a main I just wanted to see if I am
catching onto what the hell I'm doing.

struct tree
{
char *name; /*cat's name*/
char *color;
int age;
int num; /*for a count I guess for the insert () */
struct tree *left, *right;
};


Bill
 
B

Bill Cunningham

I don't know how to declare a pointer to hold insert's return value or
if I even have to. I don't know what to put into the first parameter either.

Bill
 
O

osmium

:

For the umpteenth time: You are trying to write an interesting program
without going through the learning process with simpler, fundamental things
first!!
In using my old cat,name,age allegory I am trying to make this tree. I
did some copying but will it work so far?

I only look at your posts sporadically so I don't know about the cat
allegory. My post assumes a cat is the common house pet.
#include <stdio.h>
#include <stdlib.h>

struct tree {
char *name;
int age;
char *color;
int num;
struct tree *right, *left;
};

If you INSIST on pursuing this tree thing, why don't you at least make
things a bit easier by putting the data for the name and the color in the
structure? Like this:

struct tree
{
char name[15];
char color[[11]; // heliotrope will fit
//other stuff as above
};
....
In another of your posts I think you say you don't know what num is for but
think it might be for the count of the orange cats named "Agnes" that are
713 days old - or whatever. Why don't you leave num out for now, you can
revise your working program to put it back in.
struct tree *insert(struct tree *tp, int v, char *c, int age, char *n)
{
if (tp) {
if (tp->num > v)
tp->left = insert(tp->left, v, *c, age, *n);
else if (tp->num < v)
tp->right = insert(tp->right, v, *c, age, *n);
} else if (tp = malloc(sizeof *tp)) {
tp->num = v;
tp->right = tp->left = NULL;
}
return tp;
}

Others have already pointed out that a computer is a great way to detect
syntax errors. Why burden a human with such a task?
 

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

tree errors 95
How to use Densenet121 in monai 0
correct tree 4
Function giving exception 9
tree 6
Linking error , although linker flags are correct 6
C coding a rotate function (help me pleasee) 1
Fibonacci 0

Members online

No members online now.

Forum statistics

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

Latest Threads

Top