Hey guys,

I've been messing around trying to learn binary trees and have created a simple program to get the month and number of days in a month to be inserted into the tree then displayed. I am getting a successful build, however, when it launches the terminal window the program crashes. I can't seem to find my error. Hopefully someone in the community can help me.

Here is my BINARYTREE.h file:

#ifndef BINARY_TREE
#define BINARY_TREE
#include <iostream>
#include <string>

using namespace std;

class BinaryTree
{
private:
   struct Node
   {
      string monthName;
      int noDaysInMonth;//monthID is used strictly for 
      Node *left, *right;
   };

   Node *root;
public:
   BinaryTree();
   void createNode(string month, int days){ createNode(root, month, days); }
   void createNode(Node *&, string, int);
   void displayAlphabetically(){ displayAlphabetically(root); }
   void displayAlphabetically(Node *);
   void destroyTree(Node *&);
   ~BinaryTree(){ destroyTree(root); }
};
#endif

The matching implementation file BINARYTREE.cpp:

#include "BINARY_TREE.h"

BinaryTree :: BinaryTree()
{
    root = NULL;
}

void BinaryTree :: createNode(Node *&treeTop, string month, int days)
{
    if(treeTop != NULL)
    {
        treeTop = new Node;
        treeTop -> monthName = month; treeTop -> noDaysInMonth = days;
        treeTop -> left = NULL; treeTop -> right = NULL;
    }
    else if(month < treeTop -> monthName)
    {
        createNode(treeTop -> left, month, days);
    }
    else
    {
        createNode(treeTop -> right, month, days);
    }
}

void BinaryTree :: displayAlphabetically(Node *treeTop)
{
    if (treeTop != NULL)
    {
        displayAlphabetically(treeTop -> left);
        cout << treeTop -> monthName << " - " << treeTop -> noDaysInMonth << " days" << endl;
        displayAlphabetically(treeTop -> right);
    }
}

void BinaryTree :: destroyTree(Node *&treeTop)
{
    if (treeTop != NULL)
    {
        destroyTree(treeTop -> left); destroyTree(treeTop -> right);
        delete treeTop; treeTop = NULL;
    }
}

Lastly, this is how I call it from main:

#include "BINARY_TREE.h"

int main()
{
    string months[] = {"Dec", "Nov", "Oct", "Sep", "Aug", "Jul",
                        "Jun", "May", "Apr", "Mar", "Feb", "Jan"};
    int noDays[] = {31, 30, 31, 30, 31, 31, 30, 31, 30, 31, 29, 31};
    BinaryTree h;

    for(int i = 0; i < 12; i++)
    {
        h.createNode(months[i], noDays[i]);
    }

    h.displayAlphabetically();

    return 0;
}

First you need to figure out prececisely where this is crashing. It can be any number of places for any number of reasons. Quickest, easiest way to find that out is to stick some debugging statements in various parts of the program, followed by some "cin.get()" calls to pause the program. Limber up your index finger and step through the program by continuously hitting and you'll have a bunch of debugging statements that tell you where you've been and where you haven't been. From there you can narrow down the problem. You could be rereferencing a NULL or invalid pointer or have infinite recursion. You may want to add some brackets before line 8 and after line 15, then stick a cout statement before return 0 in order to force that h object to go out of scope so the destructor gets called before the end of the end of the program. Helps for easier debugging. This should narrow down where the problem is. After you've found it, take the debugging out.

Found it. The error was in my createNode function. I originally had it as:

if(treeTop != NULL)

when it was supposed to be:

if(treeTop == NULL)

Thank you for the help.

This question has already been answered. Start a new discussion instead.