tree

  • Thread starter Bill Cunningham
  • Start date
B

Bill Cunningham

I have this data so far concerning a tree in a header called tree.h is
it considered valid? I thought I would submit it for opinions. kandr2 is
rough reading on trees. There are alot of new concepts like parenthesis in
certain places and the . and -> operators which involve pointer. I don't
know the difference in them.

/*tree constructs */

struct node {
int count;
char *name;
double price;
struct node *punode;
struct node *pdnode;
};
struct node *add(struct node *price, struct node *name);
struct node *rem(struct node *price, struct node *name);

rem is remove a node.
Bill
 
B

Ben Bacarisse

Bill Cunningham said:
I have this data so far concerning a tree in a header called tree.h is
it considered valid? I thought I would submit it for opinions. kandr2 is
rough reading on trees. There are alot of new concepts like parenthesis in
certain places and the . and -> operators which involve pointer. I don't
know the difference in them.

/*tree constructs */

struct node {
int count;
char *name;
double price;
struct node *punode;
struct node *pdnode;

I think that having two related members that differ in only one letter
is a recipe for confusion. Also, the 'p' is probably because these
are pointers and most people don't need to be reminded of that fact
(opinions do differ on this point) and the word 'node' adds little as
well. I.e. of the 6 letters in the name, only 1 really matters. I
have no problem with the extra stuff, but I would make more of the
difference.

Where do the 'u' and the 'p' come form? If they stand for something,
I'd write it out. Most trees have a 'left' and a 'right' pointer
rather than a 'u' and a 'd' one.
};
struct node *add(struct node *price, struct node *name);
struct node *rem(struct node *price, struct node *name);

The parameter names look rather odd as well.
 
B

Bill Cunningham

Ben Bacarisse said:
I think that having two related members that differ in only one letter
is a recipe for confusion. Also, the 'p' is probably because these
are pointers and most people don't need to be reminded of that fact
(opinions do differ on this point) and the word 'node' adds little as
well. I.e. of the 6 letters in the name, only 1 really matters. I
have no problem with the extra stuff, but I would make more of the
difference.

Where do the 'u' and the 'p' come form? If they stand for something,
I'd write it out. Most trees have a 'left' and a 'right' pointer

p as you guessed means pointer the u is up and d is down a node. Do I not
need up and down but right and left?
rather than a 'u' and a 'd' one.


The parameter names look rather odd as well.

I really don't know what they mean if anything. That's how pathetic I am
right now with trees. The add is to add an investment price and name and add
a node. The rem is to remove the same.

Bill
 
O

osmium

Bill Cunningham said:
I have this data so far concerning a tree in a header called tree.h is
it considered valid? I thought I would submit it for opinions. kandr2 is
rough reading on trees. There are alot of new concepts like parenthesis in
certain places and the . and -> operators which involve pointer. I don't
know the difference in them.

/*tree constructs */

struct node {
int count;
char *name;
double price;
struct node *punode;
struct node *pdnode;
};
struct node *add(struct node *price, struct node *name);
struct node *rem(struct node *price, struct node *name);

Do you believe that "price" and "name" in these function declarations have
something in common with price and name in the structure above? If they are
different things, give them different names. If you had five boys, you
wouldn't name all of them "Bill" would you? Can't you see that could be
confusing? If you can't think of appropriate names, use "abc" and "def"
temporarily.

A compiler decides if code is valid - your word. A human decides whether
code is acceptable. Valid is not a sufficient test on your code.
 
B

Bill Cunningham

[snip]

The parameters are referring to the struct. The function names are
difference. I am trying to create 2 functions called add and rem for add and
remove a node.

Bill
 
B

Ben Bacarisse

Bill Cunningham said:
p as you guessed means pointer the u is up and d is down a node. Do I not
need up and down but right and left?

A tree usually has two ways down (left and right) and sometimes a
pointer upwards (often called parent).

The names don't matter (except for readability). A node with two
pointers can be used to build a tree or it can be used to build doubly
linked list or any of a whole host of other shapes (they are called
graphs) that don't have enough importance to get a name. What will
make it a tree is if you build it in such a way that it is a tree.
I really don't know what they mean if anything. That's how pathetic I am
right now with trees. The add is to add an investment price and name and add
a node. The rem is to remove the same.

I would make the tree functions general -- you add a node and it does
not matter what that node represents -- and thus the names would be
very general names ('tree', or 'root' and 'node' or 'item').

However, even using your application specific names these don't make
sense. Why do you represent a price and a name both as node pointers
when a node includes both of these together? If the price and the
name are the parameters, how does the function know what tree to add
the data to? It looks as if you are planning to have one global tree.

I would expect an add function to be passed the tree to which the data
is to be added and the node that is to be added to it. The result
would be (as you have it) a pointer to the new tree.
 
B

Barry Schwarz

I have this data so far concerning a tree in a header called tree.h is
it considered valid? I thought I would submit it for opinions. kandr2 is
rough reading on trees. There are alot of new concepts like parenthesis in
certain places and the . and -> operators which involve pointer. I don't
know the difference in them.

The . operator does not involve a pointer. Go back an review it
again. DO NOT attempt to proceed with your effort till you understand
and are comfortable with the difference between . and ->!
 
N

Nick Keighley

the . and -> operators which involve pointer. I don't
know the difference in them.

so read your K&R about the . and -> operators.

Consider

#include <stdio.h>

typedef struct
{
int a;
int b;
} Mystruct;

int main (void)
{
Mystruct s;
Mystruct *ps;
ps = &s;

s.a = 9; /* use . operator */
s.b = 42;
printf ("struct s = (%d, %d)\n", s.a, s.b);

(*ps).a = 11; /* deref then use . operator */
(*ps).b = 43;
printf ("struct s = (%d, %d)\n", s.a, s.b);

ps->a = 12; /* use -> operator */
ps->b = 44;
printf ("struct s = (%d, %d)\n", s.a, s.b);

return 0;
}


in other words -> is just a shorthane for deref then .

(*ps).a is-equivalent-to ps->a


--
Nick Keighley

10.3. Transput declarations
{ "So it does!" said Pooh, "It goes in!"
"So it does!" said Piglet, "And it comes out!"
"Doesn't it?" said Eeyore, "It goes in and out like
anything,"
Winnie-the-Pooh, A.A. Milne.}

"Revised Report on the Algorithmic Language ALGOL 68"
 
B

Bill Cunningham

Nick Keighley said:
so read your K&R about the . and -> operators.

Consider

#include <stdio.h>

typedef struct
{
int a;
int b;
} Mystruct;

int main (void)
{
Mystruct s;
Mystruct *ps;
ps = &s;

s.a = 9; /* use . operator */
s.b = 42;
printf ("struct s = (%d, %d)\n", s.a, s.b);

(*ps).a = 11; /* deref then use . operator */
(*ps).b = 43;
printf ("struct s = (%d, %d)\n", s.a, s.b);

ps->a = 12; /* use -> operator */
ps->b = 44;
printf ("struct s = (%d, %d)\n", s.a, s.b);

return 0;
}


in other words -> is just a shorthane for deref then .

(*ps).a is-equivalent-to ps->a
I will save this post and study it. kandr2 has confused me more as to
structs.

Bill
 

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

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,122
Latest member
VinayKumarNevatia_
Top