2

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");
}
17
  • 1
    Since you are using C++ you should use new treeNode instead of (struct treeNode *)malloc(sizeof(struct treeNode)). Commented Apr 27, 2015 at 0:51
  • 1
    And fixing that, %d and std::string aren't going to play nice at all in your output statements. Any particular reason you're averse to using formatted C++ stream output? Commented Apr 27, 2015 at 0:51
  • 1
    To print std::string using printf you should do printf("%s", node->data.c_str());. However the preferable way to do it is to do std::cout << node->data;. Commented Apr 27, 2015 at 0:54
  • 1
    @Matt Please stop using malloc. You cannot create dynamic instances of treeNode using malloc. Your program produces undefined behavior as it stands right now. The reason is that your treeNode contains a std::string, therefore it is a non-POD type and you don't create dynamic instances of non-POD's with malloc (unless you're later on going to use placement-new). Commented Apr 27, 2015 at 1:00
  • 1
    @Matt -- 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 use malloc to create such instances. It isn't a matter of opinion, it is a fact. The only time for usage of malloc with non-POD types is if you will use placement-new. Commented Apr 27, 2015 at 1:07

1 Answer 1

1

The reason for the output of numbers is that you are using printf with the wrong format specifier.

    printf("%d ", node->data);

This is not correct. To fix this, you really should use std::cout, as this is C++:

    #include <iostream>
    //...
    std::cout << node->data;

If you used C++ streams, you would never have gotten into the trouble that printf causes, and that it is not typesafe and requires you to give it the correct format specifier.

Your original code not only outputted the integers, it also invoked undefined behavior. Giving printf the wrong format specifier for the data to be printed results in undefined behavior.

If you really wanted to use printf, then the correct way to use it would have been:

printf("%s", node->data.c_str());
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You. I have fixed everything!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.