0

I'm trying to make a code that allows me to enter a tree node and then indicate its preorder traversal, but do not understand what happens. What am I doing wrong?

This is my code:

#include <stdio.h>
#include<iostream>
#include<queue>
#define MAX 100
#include <vector>
#include <stdlib.h>

typedef struct node {
    struct node *parent;
    struct node *left;
    struct node *right;

    int value;
} Node;

typedef struct tree {
    Node *root;
} Tree;

void findPlace(Node *_toAdd, Node *_current) {
    // set this node to the parent so we dont have to do it later (may change next recursion)
    _toAdd->parent = _current;

    // compare value of node we're adding to current node
    if(_toAdd->value < _current->value)
        // add the element to the left subtree
        if(_current->left == NULL)          // if no left subtree yet, add it right here
            _current->left = _toAdd;
        else                                        // otherwise continue recursion to left subtree
            findPlace(_toAdd, _current->left);
    else
        // add the element to the right subtree
        if(_current->right == NULL)     // if no right subtree yet, add it right here
            _current->right = _toAdd;
        else                                        // otherwise, continue recursing to the right subtree
            findPlace(_toAdd, _current->right);
}

Node *addNode(int _val, Tree* _tree) {
    Node *newNode = (Node *)malloc(sizeof(Node));
    newNode->value = _val;

    // if the tree is empty, this is the new root
    if(_tree->root == NULL)
        _tree->root = newNode;

    // otherwise, we need to find the right place to put it
    else
        findPlace(newNode, _tree->root);

    // return reference to the node
    return newNode;
}


void preOrder(Node *);


int main() {
    // create a tree to fool around with
    Tree *myTree = (Tree *)malloc(sizeof(Tree));

    // Add some values to the tree
    addNode(7, myTree);
    addNode(3, myTree);
    addNode(7, myTree);
    addNode(11, myTree);
    addNode(6, myTree);
    addNode(8, myTree);
    addNode(12, myTree);
    addNode(0, myTree);
    addNode(2, myTree);
    addNode(9, myTree);

    printf("Pre-Order Traversal: "); preOrder(myTree->root); printf("\n");



    return 0;
}

void preOrder(Node *_root)
{
    printf("%d ", _root->value);
    if(_root->left != NULL)
        preOrder(_root->left);
    if(_root->right != NULL)
        preOrder(_root->right);
}

It is assumed that calling preorder action, it should print the tree in that order but the program simply terminates and does not print anything, im stuck with this please help me, and excuse my English

novaKid
  • 233
  • 2
  • 4
  • 11
  • Are you getting any error messages? – JustinDanielson Jan 15 '12 at 08:43
  • 1
    Is it printing "Pre-Order Traversal: " ? – JustinDanielson Jan 15 '12 at 08:44
  • 1
    It does actually work here: http://ideone.com/xemIi, though not in pre-order. – Stephan Dollberg Jan 15 '12 at 08:46
  • 1
    What have you done to debug it so far? – Martin James Jan 15 '12 at 08:50
  • 1
    @bamboon -- I believe that the output is in the correct pre-order – Skyrim Jan 15 '12 at 08:53
  • The pre-order algorithm is correct. The tree does not balance or adjust, so it won't be in a sorted order. There are 4 nodes in the left subtree and the root. These are all printing before the second 7 which is the root of the right subtree. The formula is correct. – JustinDanielson Jan 15 '12 at 08:57
  • @Skyrim if it's right what Justin says, then yes, it should be in pre-order. – Stephan Dollberg Jan 15 '12 at 09:00
  • yeah, I just verified on paper...so the algorithm is working, I think it's just an issue with the program exiting right after it is run...just try to stall it by asking for some input...(if it's not running on a console, but doing something like ctrl-F5 in Visual Studio) – Skyrim Jan 15 '12 at 09:02
  • @JustinDanielson I don't understand that if you can compile Ideone.com and print the preorder traversal of the tree but my computer just finished, what I can I be doing wrong? I'm using Code:: Blocks and MinGW – novaKid Jan 15 '12 at 09:05

1 Answers1

1

I think your issue is that you don't have anything stalling the exiting of the program...just use cin

int x;
cin >> x;

return 0;
Skyrim
  • 865
  • 5
  • 6
  • I do not understand your point, why should I do that?. Excuse my ignorance. – novaKid Jan 15 '12 at 09:11
  • Is the terminal closing after the program executes? You wrote "but the program simply terminates and does not print anything" so I think you're running the program in the windows terminal. It brings up the terminal screen, executes and closes right away. I don't know why it closes after completion, but if you make your program wait for user input, it will not close. – JustinDanielson Jan 15 '12 at 09:15
  • well, I'm assuming, for examaple you run an exe or something and a console appears and then closes...this would fix that issue b/c the conosle won't close until the user enters a value and presses the return key...@JustinDanielson -- you read my mind :p – Skyrim Jan 15 '12 at 09:17
  • No, the terminal is not closing after the program executes, I'm using the IDE to program called codeblocks http://www.codeblocks.org/, which has its own console, just execute build and run, and opens a console that shows me the following information: inline `Process returned -1073741819 (0xC0000005) execution time : 0.031 s Press any key to continue.` – novaKid Jan 15 '12 at 09:28
  • ah...that's some kind of access violation...please google the solution using the error code & 0xc0000005...I hesitate posting any of those solutions without testing them myself! – Skyrim Jan 15 '12 at 09:47