Thread: binary tree: adding first node works than crashes

  1. #1

    Unhappy binary tree: adding first node works than crashes

    Hi all - any help would be appreciated

    My main creates random numbers (per user input) for the number of nodes to add to tree. The first number works then I get this error msg:
    The instruction at "0x0040115c" referenced memory at "0xcccccccc". The memory could not be "read". Click ok to terminate program.

    Here's my main program, and below it is my add node function:
    void main()

    BasicTree rewt;
    TreeNode temproot;
    TreeNode addit;
    int nodenumb;

    cout << "How many nodes do you want to create?";
    cin >> nodenumb;
    for (int i = 0; i < nodenumb; i++)
    addit.key = rand();
    rewt.add_item_to_tree(&addit, &temproot); // this statement I think is giving me all the trouble.. what am I doing wrong?
    cout << "Throw this in the binary tree: " << addit.key << endl;

    Here's my add node function:

    bool BasicTree::add_node_to_tree(TreeNode *item,TreeNode *subtree)
    bool ret_val = true;

    if (root == NULL)
    { //we are adding the first node to the tree
    root = item;

    if (item->key <= subtree->key)
    if (subtree->left == NULL)
    subtree->left = item;
    } else
    ret_val = add_item_to_tree(item, subtree->left);
    } else
    if (subtree->right == NULL)
    subtree->right = item;
    } else
    ret_val = add_item_to_tree(item, subtree->right);
    return ret_val;

  2. #2
    Registered User
    Join Date
    Mar 2002

    Strange code

    I don't get it, where is the add_item_to_tree() function? The function add_node_to_tree() you have listed is not called from your main loop. ???

  3. #3
    i'm sorry.. the add_node_to_tree function is the add_item_to_tree

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Balancing A Binary Tree
    By mike_g in forum C Programming
    Replies: 5
    Last Post: 08-12-2008, 04:01 PM
  2. Interpreter.c
    By moussa in forum C Programming
    Replies: 4
    Last Post: 05-28-2008, 05:59 PM
  3. arrays vs lists? And containers in general!
    By clegs in forum C++ Programming
    Replies: 22
    Last Post: 12-03-2007, 02:02 PM
  4. Binary Tree, couple questions
    By scoobasean in forum C Programming
    Replies: 3
    Last Post: 03-12-2005, 09:09 PM
  5. binary tree node structure
    By Kirsten in forum C Programming
    Replies: 2
    Last Post: 04-26-2002, 08:02 PM