You are using a version without Ads of this website. Please, consider donating:

### Binary tree element insertion

So I'm having problems with inserting new nodes into a binary tree. What I need to do is depending on inputs add a left or right node to a specific parent node.
Input file starts with the root for the free.

Input file example:
 ```1 L 1 2 R 1 3 F```

Input always starts with root node, "L 1 2" means that a left child (which has 2 in data field) should be added to the node which has 1 in it. Same logic for the right insert. Input file ends with F.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115`` ``````#include #include "stdlib.h" #include "stdio.h" using namespace std; struct TreeNode { int item; // The data in this node. TreeNode *left; // Pointer to left subtree. TreeNode *right; // Pointer to right subtree. TreeNode(int str) { item = str; left = NULL; right = NULL; } }; // end struct Treenode void treeInsert(TreeNode *&root, int newItem) { // Add the item to the binary sort tree to which the parameter // "root" refers. Note that root is passed by reference since // its value can change in the case where the tree is empty. if ( root == NULL ) { // The tree is empty. Set root to point to a new node containing // the new item. This becomes the only node in the tree. root = new TreeNode( newItem ); // NOTE: The left and right subtrees of root // are automatically set to NULL by the constructor. // This is important! return; } else if ( newItem < root->item ) { treeInsert( root->left, newItem ); } else { treeInsert( root->right, newItem ); } } // end treeInsert() void treeInsertLeft(TreeNode *&root, int parent, int child) { if ( root->item == parent ) { root->left = new TreeNode(child); return;} if(root==NULL)return; else{ treeInsertLeft( root->left, parent, child ); treeInsertLeft( root->right, parent, child ); } } void treeInsertRight(TreeNode *&root, int parent, int child) { if ( root->item == parent ) { root->right = new TreeNode(child); return;} if(root==NULL)return; else{ treeInsertRight( root->left, parent, child ); treeInsertRight( root->right, parent, child ); } } int main() { FILE* inFile; FILE* outFile; int aFirst; int aSecond; int tRoot; char aString[1]; TreeNode *root; // Pointer to the root node in the tree. root = NULL; // Start with an empty tree. inFile = fopen("input.in", "r"); outFile = fopen("output.out", "w+"); fscanf(inFile, "%d", &tRoot); treeInsert(root,tRoot); treeInsertRight(root,tRoot,2); treeInsertLeft(root,tRoot,3); /* treeInsertRight(root,3,4); treeInsertLeft(root,3,9); */// <-- Crashes when trying to use these two while(aString[0]!='F') {fscanf(inFile, "%s", aString); if(aString[0]=='L'||aString[0]=='R') fscanf(inFile, "%d %d", &aFirst, &aSecond); aFirst=aSecond=NULL; } fclose(inFile); fclose(outFile); return 0; }``````

So the code crashes when trying to insert elements beyond roots children, I most likely have got the recursion wrong but I need help in understanding why and how I could fix it.

P.S. I know the tree should be a private class with methods but that's currently not the concern. Also I should learn how to use the debugger but with my limited skills I currently don't even have an idea why it isn't working in CodeBlocks so that's no help aswell. Any help from you guys would be greatly appreciated, a bit stuck at this problem.
Last edited on
Still stuck, any ideas? :<
Lines 48 and 63 need to be at the beginning of their function; otherwise you could be dereferencing a null pointer with the -> operator.
Thanks, works like a charm now :) Wasn't familiar with the concept of dereferencing a null pointer before, lesson learned.
Topic archived. No new replies allowed.