Thread: tree wont run
View Single Post
  #1 (permalink)  
Old May 20th, 2004, 12:04 AM
mafuka mafuka is offline
Registered User
 
Join Date: Mar 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default tree wont run

hi again can somebody help me with my binary tree, i compile it and it marks zero errors and then when i run it, the exe window just opens and then closes back again, here is my code

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

struct treeNode {
   struct treeNode *leftPtr;
   int data;
   struct treeNode *rightPtr;
};

typedef struct treeNode TREENODE;
typedef TREENODE *TREENODEPTR;

void insertNode(TREENODEPTR *, int);
void inOrder(TREENODEPTR);

main()
{
   int i, item;
   TREENODEPTR rootPtr = NULL;

   srand(time(NULL));

   /* insert random values between 1 and 15 in the tree */
   printf("The numbers being placed in the tree are:\n");

   for (i = 1; i <= 10; i++) {
      item = rand() % 15;
      printf("%3d", item);
      insertNode(&rootPtr, item);
   }

   /* traverse the tree inOrder */
   printf("\n\nThe inOrder traversal is:\n");
   inOrder(rootPtr);

   return 0;
}

void insertNode(TREENODEPTR *treePtr, int value)
{
   if (*treePtr == NULL) { /* *treePtr is NULL */
      *treePtr = new (TREENODE);

      if (*treePtr != NULL) {
         (*treePtr)->data = value;
         (*treePtr)->leftPtr = NULL;
         (*treePtr)->rightPtr = NULL;
      }
      else
         printf("%d not inserted. No memory available.\n",
                value);

   }
   else
      if (value < (*treePtr)->data)
         insertNode(&((*treePtr)->leftPtr), value);
      else
         if (value > (*treePtr)->data)
            insertNode(&((*treePtr)->rightPtr), value);
         else
            printf("dup");
}

void inOrder(TREENODEPTR treePtr)
{
   if (treePtr != NULL) {
      inOrder(treePtr->leftPtr);
      printf("%3d", treePtr->data);
      inOrder(treePtr->rightPtr);
   }
}