The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 3 of 3

Thread: [C++] trying to create binary tree, crash when adding nodes.

  1. #1

    [C++] trying to create binary tree, crash when adding nodes.

    Code:
    struct TreeNode {
                  int item;         // The data in this node.
                  TreeNode *left;   // Pointer to the left subtree.
                  TreeNode *right;  // Pointer to the right subtree.
    };
    int main (void){
    	TreeNode myTree;
    	myTree.item = 1;
    	myTree.left->item = 2;
    	myTree.right->item = 3;  //it starts crashing here. 
    	myTree.left->left->item = 4;
    	myTree.left->right->item = 5;
    	myTree.right->left->item = 6;
    	myTree.right->right->item = 7;
    	return 0;
    }
    it compiles fine, and i think the syntax is fine. but it crashes when i run it. i removed the lines starting at item = 3, and it will run fine.

  2. #2
    The left and right fields in your struct are just pointers, so you haven't actually allocated any memory for the left or right TreeNode (you've only allocated memory for an int and two pointers). Before you try to access any members of left or right, you should make another TreeNode.

    Code:
    #include <stdio.h>
    
    struct TreeNode {
      int item;         // The data in this node.
      TreeNode *left;   // Pointer to the left subtree.
      TreeNode *right;  // Pointer to the right subtree.
    };
    int main (void){
      TreeNode myTree;
      myTree.left = new TreeNode();
      myTree.right = new TreeNode();
      myTree.left->left = new TreeNode();
      myTree.left->right = new TreeNode();
      myTree.right->left = new TreeNode();
      myTree.right->right = new TreeNode();
      myTree.item = 1;
      myTree.left->item = 2;
      myTree.right->item = 3;  //it starts crashing here. 
      myTree.left->left->item = 4;
      myTree.left->right->item = 5;
      myTree.right->left->item = 6;
      myTree.right->right->item = 7;
      printf("%d %d\n", myTree.left->left->item, myTree.right->right->item); // 4 7
      return 0;
    }
    Also, it's probably more convenient if you use a loop or recursive call to make the nodes, but I'm guessing that you were planning to get to that later.
    “Who were you, Krilnon, and how did you know so much about AS4?”
    The historian sighed as she gazed up at the sky and saw… not stars. A story.

  3. #3
    oh thanks man. i'll try using a loop too.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012