There are no compile errors. Whenever I execute the program will crash and when I try to debug it, it will point to "newNode->data = n;" Am I doing something wrong with inserting the string??
// Assignment 5.cpp : Defines the entry point for the console application.
// Matthew - Assignment 5
#include "stdafx.h"
#include <stdlib.h>
#include <string>
using namespace std;
struct treeNode
{
string data;
struct treeNode *left;
struct treeNode *right;
};
void insert(struct treeNode **node, string n);
void preOrder(struct treeNode *node);
void inOrder(struct treeNode *node);
void postOrder(struct treeNode *node);
int _tmain(int argc, _TCHAR* argv[])
{
struct treeNode *root = NULL;
insert(&root, "polymorphism");
insert(&root, "object");
insert(&root, "templates");
insert(&root, "structure");
insert(&root, "class");
insert(&root, "pointer");
insert(&root, "reference");
insert(&root, "traversal");
insert(&root, "inheritance");
insert(&root, "exceptions");
insert(&root, "recursive");
insert(&root, "overloading");
printAll(root);
printf("\n\n");
return 0;
}
void insert(struct treeNode **node, string n)
{
if (*node == NULL)
{ //tree (or the current sub tree) is empty
struct treeNode *newNode;
newNode = new treeNode;
newNode->data = n;
newNode->left = NULL;
newNode->right = NULL;
*node = newNode;
}
else if (n < (*node)->data)
insert(&((*node)->left), n);
else if (n >(*node)->data)
insert(&((*node)->right), n);
}
Regarding my traversal code. Does it look good?
void preOrder(struct treeNode *node)
{
if (node != NULL)
{
printf("%d ", node->data);
preOrder(node->left);
preOrder(node->right);
}
}
void inOrder(struct treeNode *node)
{
if (node != NULL)
{
inOrder(node->left);
printf("%d ", node->data);
inOrder(node->right);
}
}
void postOrder(struct treeNode *node)
{
if (node != NULL)
{
postOrder(node->left);
postOrder(node->right);
printf("%d ", node->data);
}
}
void printAll(struct treeNode *node)
{
printf("preOrder: ");
preOrder(node);
printf("\n");
printf("inOrder: ");
inOrder(node);
printf("\n");
printf("postOrder: ");
postOrder(node);
printf("\n");
}
new treeNodeinstead of(struct treeNode *)malloc(sizeof(struct treeNode)).%dandstd::stringaren't going to play nice at all in your output statements. Any particular reason you're averse to using formatted C++ stream output?std::stringusingprintfyou should doprintf("%s", node->data.c_str());. However the preferable way to do it is to dostd::cout << node->data;.malloc. You cannot create dynamic instances oftreeNodeusing malloc. Your program produces undefined behavior as it stands right now. The reason is that yourtreeNodecontains astd::string, therefore it is a non-POD type and you don't create dynamic instances of non-POD's withmalloc(unless you're later on going to useplacement-new).my professor always tells us...Stop. Your professor is wrong, and this is not an opinion. They are just plain wrong. When a type is non-POD, you cannot usemallocto create such instances. It isn't a matter of opinion, it is a fact. The only time for usage ofmallocwith non-POD types is if you will useplacement-new.