structs

B

Bill Cunningham

Structs with pointers and members not involving members are easy in C.
But with trees I ask why are pointers preferred and recursion is a little
confusing. I have this code:

#include <stdio.h>

struct tree {
char *name;
char *color;
};

int main() {

struct tree p;
struct tree *pt=&p;
pt->name="striper";
pt->color="mau";

printf("%s %s\n",pt->name,pt->color);
return 0;
}

When prints out name and color of cat. but the step I am missing is the
memory allocation (malloc()) and members that are called *left and *right
nodes. I understand there is a root node and a node that is copied from
that. Then several right and left nodes with a BST. How do I allocate memory
using this?

Bill
 
M

Mug

    Structs with pointers and members not involving members are easy in C.
But with trees I ask why are pointers preferred and recursion is  a little
confusing. I have this code:

#include <stdio.h>

struct tree {
char *name;
char *color;

};

int main() {

    struct tree p;
    struct tree *pt=&p;
 pt->name="striper";
 pt->color="mau";

 printf("%s %s\n",pt->name,pt->color);
 return 0;

}

When prints out name and color of cat. but the step I am missing is the
memory allocation (malloc()) and members that are called *left and *right
nodes. I understand there is a root node and a node that is copied from
that. Then several right and left nodes with a BST. How do I allocate memory
using this?

Bill

in your structure,u need have
 
M

Mug

    Structs with pointers and members not involving members are easy in C.
But with trees I ask why are pointers preferred and recursion is  a little
confusing. I have this code:

#include <stdio.h>

struct tree {
char *name;
char *color;

};

int main() {

    struct tree p;
    struct tree *pt=&p;
 pt->name="striper";
 pt->color="mau";

 printf("%s %s\n",pt->name,pt->color);
 return 0;

}

When prints out name and color of cat. but the step I am missing is the
memory allocation (malloc()) and members that are called *left and *right
nodes. I understand there is a root node and a node that is copied from
that. Then several right and left nodes with a BST. How do I allocate memory
using this?

Bill

in your stucture , u need have members which point to it's left nodes
and right nodes.
struct tree {
char *name;
char *color;
struct tree* left;
struct tree* right;
};

int main()
{
struct tree *root=malloc(sizeof(struct tree));
tree->name="stripper";
tree->color="red";
tree>left=NULL;
tree->right=NULL;
printf .....
}

recursive : for exemple u want to print the tree with all their nodes:
void print_tree(struct tree* my_tree)
{
if(my_tree==NULL)
{
return;
}
printf("%s %s\n",my_tree->name,my_tree->color)
print_tree(my_tree->left); // recursive calls
print_tree(my_tree->right);
}
 
B

Bill Cunningham

in your stucture , u need have members which point to it's left nodes
and right nodes.
struct tree {
char *name;
char *color;
struct tree* left;
struct tree* right;
};

int main()
{
struct tree *root=malloc(sizeof(struct tree));
tree->name="stripper";
tree->color="red";
tree>left=NULL;
tree->right=NULL;
printf .....
}

recursive : for exemple u want to print the tree with all their nodes:
void print_tree(struct tree* my_tree)
{
if(my_tree==NULL)
{
return;
}
printf("%s %s\n",my_tree->name,my_tree->color)
print_tree(my_tree->left); // recursive calls
print_tree(my_tree->right);
}


Ok I'm catching on. Slowly, but making progress. Is there a need at some
point to free memory with free() since malloc() is being used?

Bill
 
B

Bill Cunningham

in your stucture , u need have members which point to it's left nodes
and right nodes.
struct tree {
char *name;
char *color;
struct tree* left;
struct tree* right;
};

[...]

What about a int num; member of tree? Something to keep track of numbers
that's how I want my tree to work. For example,

Root
|
Node (0)
/ \
1left (1) 1 right (2)
/ \
2nd left (3) 2nd right (4)

Above the even numbers are right leaves. The odd numbers are left leaves.

Bill
 
M

Mug

in your stucture , u need have members which point to it's left nodes
and right nodes.
  struct tree {
   char *name;
   char *color;
   struct tree* left;
   struct tree* right;
 };

int main()
{
  struct tree *root=malloc(sizeof(struct tree));
  tree->name="stripper";
  tree->color="red";
  tree>left=NULL;
  tree->right=NULL;
  printf .....

}

recursive : for exemple u want to print the tree with all their nodes:
void print_tree(struct tree* my_tree)
{
  if(my_tree==NULL)
  {
    return;
  }
  printf("%s %s\n",my_tree->name,my_tree->color)
  print_tree(my_tree->left);  // recursive calls
  print_tree(my_tree->right);

}

Ok I'm catching on. Slowly, but making progress. Is there a need at some
point to free memory with free() since malloc() is being used?
yes sure, once u don't need those struct anymore u should free them,
it will be something look
like these:

void free_recursive(struct tree* my_tree)
{
if(my_tree==NULL)
{
printf("nothing to do,ptr is null\n");
return;
}
if(my_tree->left!=NULL)
{
free_recursive(my_tree->left);
}
if(my_tree->right!=NULL)
{
free_recursive(my_tree->right);
}
free(my_tree);

}
 
M

Mug

in your stucture , u need have members which point to it's left nodes
and right nodes.
  struct tree {
   char *name;
   char *color;
   struct tree* left;
   struct tree* right;
 };

[...]

What about a int num; member of tree? Something to keep track of numbers
that's how I want my tree to work. For example,

Root
   |
Node (0)
        /        \
1left (1)  1 right (2)
      /             \
2nd left (3)  2nd right (4)

Above the even numbers are right leaves. The odd numbers are left leaves.

Bill

well, u may want to add a hind in structure, add a int track; member,
and then in ur initilize function u need have something like

void insert_left(struct *tree root ,struct tree *left)
{
static int trk = 1; // static variable just initilized once in
program
root->left=left;
root->track=trk;
trk+=2; // next time trk will be 3;
}

with it u can be sure all odd number will be left member, and
insert_right function is similar.
 
O

osmium

Bill Cunningham said:
in your stucture , u need have members which point to it's left nodes
and right nodes.
struct tree {
char *name;
char *color;
struct tree* left;
struct tree* right;
};

[...]

What about a int num; member of tree? Something to keep track of numbers
that's how I want my tree to work. For example,

Root
|
Node (0)
/ \
1left (1) 1 right (2)
/ \
2nd left (3) 2nd right (4)

Above the even numbers are right leaves. The odd numbers are left leaves.

It's a good idea to decide what you want to do before you do it. This is
called "design"
What does this new field, number represent? Can you think of it as a serial
number assigned to each cat? Your last paragraph suggests (to me) it has
something to do with leftness or rightness of the node. I can't imagine
this as being useful information.

Some people find it helpful to assign variable names suggestive of the real
world; it helps the mind doing the analyzing. For example, you have a tree
of cats, but you chose to name it p. where you have
struct tree p;
I would have used
struct tree cats;

or cat or cat_list, something that contains cat somewhere in the name.

Perhaps a first step might be to come up with a more descriptive name for
this new field than simply "num". num could be the number of apples on a
tree or the value of a clunker car ($4,500) in the US.
 
N

Nobody

Ok I'm catching on. Slowly, but making progress. Is there a need at some
point to free memory with free() since malloc() is being used?

Maybe. If you finish using the tree before the end of the program, you
should free it. If the program is going to terminate soon, there's
no need.
 
B

Bill Cunningham

It's a good idea to decide what you want to do before you do it. This is
called "design"
What does this new field, number represent? Can you think of it as a
serial number assigned to each cat? Your last paragraph suggests (to me)
it has something to do with leftness or rightness of the node. I can't
imagine this as being useful information.

Some people find it helpful to assign variable names suggestive of the
real world; it helps the mind doing the analyzing. For example, you have
a tree of cats, but you chose to name it p. where you have
struct tree p;
I would have used
struct tree cats;

or cat or cat_list, something that contains cat somewhere in the name.

Perhaps a first step might be to come up with a more descriptive name for
this new field than simply "num". num could be the number of apples on a
tree or the value of a clunker car ($4,500) in the US.

Yeah I guess I could've used,

typedef struct
{
char *color;
char *name;
}cat;

It might've been simpler that way. Or more descriptive. I'm not sure if
that's what your meaning. It's the trees that are a little confusing to me
still. I've got the structs down. It's the tree algorithm that I still am a
little confused on.

Bill
 
O

osmium

Bill Cunningham said:
Yeah I guess I could've used,

typedef struct
{
char *color;
char *name;
}cat;

It might've been simpler that way. Or more descriptive. I'm not sure if
that's what your meaning. It's the trees that are a little confusing to me
still. I've got the structs down. It's the tree algorithm that I still am
a little confused on.

No, that's not what I meant. I *showed* you your line and then my proposed
line. I am trying to retain, as far possible, the look and feel of the
post by Ben that you are using as a model. Making the change you suggest
above makes things worse by introducing a new word for your primary
structure.
 
B

Bill Cunningham

No, that's not what I meant. I *showed* you your line and then my
proposed line. I am trying to retain, as far possible, the look and
feel of the post by Ben that you are using as a model. Making the change
you suggest above makes things worse by introducing a new word for your
primary structure.

Oh OK. Sorry.

Bill
 
N

Nick Keighley

well, u may want to add a hind in structure, add a int track; member,
and then in ur initilize function u need have something like

could you post in standard english, please?
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,059
Latest member
cryptoseoagencies

Latest Threads

Top