Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

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