Structure

M

maths_fan

I'd like to make a tree without any functions. I mean just using the
structure.
I have a structure:
struct TTree
{
int key;

struct TTree* pLeft;
struct TTree* pRight;
} Tree[] =
{

??????????
}

In the place with quastions I should write something for making a
tree. (Any tree). I just need to know how is it possible. I wrote
something like
struct TTree
{
int key;

struct TTree* pLeft;
struct TTree* pRight;
} Tree[] =
{
5, Tree[3], Tree[6],
4, NULL, NULL,
6, NULL, NULL
}
It didn't work.
 
H

Hallvard B Furuseth

maths_fan said:
struct TTree
{
int key;

struct TTree* pLeft;
struct TTree* pRight;
} Tree[] =
{
5, Tree[3], Tree[6],
4, NULL, NULL,
6, NULL, NULL
}
It didn't work.

struct TTree { ... };
static struct TTree node4 = { 4, NULL, NULL }, node6 = { 6, NULL, NULL };
static struct TTree node5 = { 5, &node4, &node6 };
struct TTree *Tree = &node5;

This is assuming you want your Tree variable to be a pointer and not the
root node itself. I.e. if you want to be able to change which node is
the root node. Otherwise you could rename node5 to Tree.
 
T

Thomas Matthews

maths_fan said:
I'd like to make a tree without any functions. I mean just using the
structure.
I have a structure:
struct TTree
{
int key;

struct TTree* pLeft;
struct TTree* pRight;
} Tree[] =
{

??????????
}

In the place with quastions I should write something for making a
tree. (Any tree). I just need to know how is it possible. I wrote
something like
struct TTree
{
int key;

struct TTree* pLeft;
struct TTree* pRight;
} Tree[] =
{
5, Tree[3], Tree[6],
4, NULL, NULL,
6, NULL, NULL
}
It didn't work.

Nit picking: How do you print it without any functions?

Try this:
#include <stdio.h>

struct TTree
{
int key;

struct TTree* pLeft;
struct TTree* pRight;
} Tree[] =
{
/* Tree[0] */ 5, &Tree[1], &Tree[2],
/* Tree[1] */ 4, NULL, NULL,
/* Tree[2] */ 6, NULL, NULL
};

int main(void)
{
puts("Tree Program");
return 0;
}

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
 

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
474,082
Messages
2,570,588
Members
47,210
Latest member
JuliaMulli

Latest Threads

Top