binary tree and AVL tree unhandled exception

Hello,

I am working on a tree and AVL tree program and I have everything done, but It crashes when run (no compiling errors) so i run it in debug through visual studios and it says:

Unhandled exception at 0x004018C9 in p-tree.exe: 0xC0000005: Access violation reading location 0x51BAA9F4.

I have no idea what to do.. debugging was only briefly and not very well covered in my class. Any help would be appreciated!

if I comment out the printTree in main() it runs fine, so not sure if the problem is there or just doesn't become issue until then.

My code:

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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155

                #include<iostream>
                #include<fstream>
                #include<vector>
                #include<iomanip>

                using namespace std;

                struct binaryNode{

                    int height;
                    int value;
                    binaryNode *left;
                    binaryNode *right;

                };//end node

                int findHeight(binaryNode* root){
                        if(&root == nullptr){
                        return 0;
                    }else{
                        int l = findHeight(root->left);
                        int r = findHeight(root->right);
                        return max(l,r) + 1;
                    }
                };//end findHeight

                void insertNode(binaryNode* head, int x){
                    int depth;

                    if(head == NULL){
                        head = new binaryNode;
                        head->height = findHeight(head);
                        head->value = x;
                    }else if(x < head->value){
                        insertNode(head->left, x);
                    }else if(x > head->value){
                        insertNode(head->right, x);
                    }else{};//end if else
                };//end create tree

                void rotateWithLeftChild(binaryNode* k2){

                    binaryNode* k1 = k2->left;
                    k2->left = k1->right;
                    k1->right = k2;
                    k2->height = max(findHeight(k2->left), findHeight(k2->right)) + 1;
                    k1->height = max(findHeight(k1->left), k2->height) + 1;
                    k2 = k1;
                };//end rotate with left child

                void rotateWithRightChild(binaryNode* k1){

                    binaryNode *k2 = k1->right;
                    k1->right = k2->left;
                    k2->left = k1;
                    k1->height = max(findHeight(k1->left), findHeight(k1->right)) + 1;
                    k2->height = max(findHeight(k2->right), k1->height) + 1;
                    k1 = k2;

                }; // end with rotate with right child

                void doubleWithLeftChild(binaryNode* k3){
                    rotateWithRightChild(k3->left);
                    rotateWithLeftChild(k3);

                }; //end double with left child

                void doubleWithRightChild(binaryNode* k1){
                    rotateWithLeftChild(k1->right);
                    rotateWithRightChild(k1);
                }

                void insertAVLtree(binaryNode* head, int x){

                    int height;

                    if(head == NULL){
                        head = new binaryNode;
                        head->height = findHeight(head);
                        head->value = x;
                    }else if(x < head->value){
                        insertAVLtree(head->left, x);
                        if((findHeight(head->left) - findHeight(head->right)) == 2){
                            if(x < head->left->value){
                                rotateWithLeftChild(head);

                            }else{
                                doubleWithLeftChild(head);
                            }
                        }
                    }else if(head->value < x){
                        insertAVLtree(head->right, x);
                        if((findHeight(head->right) - findHeight(head->left) == 2)){
                            if(head->right->value < x){
                                rotateWithRightChild(head);
                            }else{
                                doubleWithRightChild(head);
                            }
                        }//end if height
                    }else{
                        head->height = max(findHeight(head->left), findHeight(head->right)) + 1;
                    }

                };//end insertAVL

                void printTree(binaryNode* head, int indent=0){
                    if(head != NULL){
                        if(head->left != NULL){
                            printTree(head->left, indent + 4);
                        }
                        if(head->right != NULL){
                            printTree(head->right, indent + 4);
                        }
                        if(indent){
                            cout << setw(indent) << " ";
                        }
                        cout << head->value << "\n";
                    }
                };//end print tree

                main(){

                vector<int> fileArray;
                int number;

                ifstream file; //create stream
                file.open("treenode.txt"); //open file


                if(file.is_open()){
                    while(file >> number){

                        fileArray.push_back(number);
                        char comma;
                        file >> comma;
                    }
                }

                file.close();//close file

                binaryNode* head;
                binaryNode* headAVL;

                for(int i = 0; i < fileArray.size(); i++){
                    insertNode(head, fileArray[i]);
                }
                for(int j = 0; j < fileArray.size(); j++){
                    insertAVLtree(headAVL, fileArray[j]);
                }

                printTree(head);
                printTree(headAVL);

                };//end main 

Last edited on
I got some warnings cpp.sh generated from this code:

In function 'int findHeight(binaryNode*)':
19:37: warning: the address of 'root' will never be NULL [-Waddress]
In function 'void insertNode(binaryNode*, int)':
29:25: warning: unused variable 'depth' [-Wunused-variable]
In function 'void insertAVLtree(binaryNode*, int)':
76:25: warning: unused variable 'height' [-Wunused-variable]
At global scope:
122:22: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
In function 'int main()':
145:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
148:34: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
152:32: warning: 'head' may be used uninitialized in this function [-Wmaybe-uninitialized]
149:57: warning: 'headAVL' may be used uninitialized in this function [-Wmaybe-uninitialized]

Maybe the problem has something to do with this
Last edited on
I think the problem starts with
1
2
3
4
5
  binaryNode* head;
  binaryNode* headAVL;
  for(int i = 0; i < fileArray.size(); i++){
    insertNode(head, fileArray[i]);
 }

head points to a random location and when you try to use it in insertNode (line 35 or 37) this causes the crash.

Some material about debugging:
http://www.cprogramming.com/debugging/debugging_strategy.html
https://www.amazon.co.uk/Effective-Debugging-Specific-Software-Development/dp/0134394798/ref=pd_cp_14_1?_encoding=UTF8&psc=1&refRID=9QNN85SZHNEB0863KXJM
Topic archived. No new replies allowed.