Hello.

im having a small problem creating a tree. im not sure where im going wrong.
Can anyone please help? Thanks.

Here is the code:

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

struct tree_el {
   int val;
   struct tree_el *right; 
   struct tree_el *left;
};

typedef struct tree_el node;

void printout(node *tree) 
{
   if (tree -> left) 
	   printout(tree->left);
   
   printf("%d\n",tree->val);
   
   if (tree -> right)
	   printout(tree->right);
}


void insert(node *parent, node *child)
{
	if (parent == NULL) {
		parent = child;
		return;
	} 
	if (child -> val < parent -> val) {
		insert(parent -> left, child);
	} else {
		insert(parent -> right, child);
	}
}

int main(void)
{
   node *curr, *root;
   int i;

   root = NULL;

   for (i = 1; i <= 10; i++) {
      /* Allocate memory */
      curr = (node *)malloc(sizeof(node));

      curr->left = curr->right = NULL;
      curr->val = i;

	  /* Insert the node */
      insert(root, curr);
   }

   printout(root);
   getchar();

   return 0;
}

You know this is written in C not C++...

>You know this is written in C not C++...
You know that this compiles as C++ too...oh, apparently not. :icon_rolleyes:

If I was creating this simple binary tree I would create my structure like so

struct tree_el 
{
  int *val;
  struct tree_el *right; 
  struct tree_el *left;
};

With an int pointer...That way you can test if the pointer exists with a simple statement like

struct tree_el *root = (struct tree_el *)malloc(sizeof(struct tree_el));

if (root->val)
{

}

oh! im sorry about that. i use Visual C++.So i just posted it in here. Sorry about that. i will post it in the C forum.

@Narue
i did not get you. It does compile. Throws a runtime error, which was my problem actually.

If I was creating this simple binary tree I would create my structure like so

struct tree_el 
{
  int *val;
  struct tree_el *right; 
  struct tree_el *left;
};

With an int pointer...That way you can test if the pointer exists with a simple statement like

struct tree_el *root = (struct tree_el *)malloc(sizeof(struct tree_el));

if (root->val)
{

}

Um, perhaps you could elaborate on the logic of this for us slower kids? Because your idea as stated is both illogical and broken.

>@Narue
>i did not get you. It does compile.

Yes, it does compile. That was my point. gerard was wrong in saying that the code is not C++.

Um, perhaps you could elaborate on the logic of this for us slower kids? Because your idea as stated is both illogical and broken.

>@Narue
>i did not get you. It does compile.

Yes, it does compile. That was my point. gerard was wrong in saying that the code is not C++.

Please electorate I'm interested in your comments.

If it was C++ shouldn't the includes be

#include <cstdio>
#include <cstdlib>

>If it was C++ shouldn't the includes be
Not necessarily. The standard C headers are still supported by standard C++, just deprecated. It's unlikely that they'll be removed from the C++ standard as well, because that would destroy code compatibility with C (a guiding principle from day one).

@Narue

Ok. Anyways, can you please tell what the problem is? The subsequent calls to insert does not seem to be working.

You're not updating the root node:

node *insert(node *parent, node *child)
{
    if (parent == NULL) {
        parent = child;
        return parent;
    } 
    if (child -> val < parent -> val) {
        parent->left = insert(parent -> left, child);
    } else {
        parent->right = insert(parent -> right, child);
    }

    return parent;
}
root = insert(root, curr);

The code you have fails because printout doesn't take into account that tree might be a null pointer.

@Narue

Thanks a lot. i changed it as per your suggestions. The tree is built properly. But the printout function seems to have a problem.

void printout(node *tree) 
{
	if (tree == NULL)
		return;

   if (tree -> left) 
	   printout(tree->left);
   
   printf("%d\n",tree->val);
   
   if (tree -> right)
	   printout(tree->right);
}

This is how i changed it. Can you please tell what the problem is?
Thanks.

@Narue.

It worked. Thanks a lot for the help. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.