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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#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.