Binary Tree Operations

Hello,

I am getting a strange error:
"Assign6(33183) malloc: *** error for object 0x7f9d2a4038e0: pointer being freed was not allocated*** set a breakpoint in malloc_error_break to debug "

The program will run, and the calculations seem to be correct, but I am getting this warning at the beginning or end of my output in my console. (It varies)

I've attached my code below. If anyone has any suggestions I'm all ears. Thanks! -maebe

PS~Also getting errors that my nodeCount and leavesCount are not recognizable functions. I've commented them out for now, for testing purposes, but if you have any solutions for that as well I'd appreciate it.

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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * intBST.cpp
 *
 *  Created on: Jul 1, 2013
 */


#include "intBST.h"
#include<iostream>

using namespace std;

//***********************************************
//Function inserts a node into tree
//
//param- 1- node Ptr that is already in the tree
//param- 2- node that needs to be inserted to tree
//***********************************************
void biTree::insert(treeNode *&nodePtr, treeNode *&newNode){
	if(nodePtr ==NULL)
		nodePtr = newNode; //insert the node.
	else if(newNode->data < nodePtr->data)
		insert(nodePtr->left, newNode);
	else
		insert(nodePtr->right, newNode);
}
//***********************************************
//Function creates new nodes to tree
//
//param- 1- value to be store in treeNode
//***********************************************
void biTree::insertNode(int num){
	treeNode *newNode; 		//pointer to new data

	//create a node and store it in num
	newNode = new treeNode;
	newNode->data= num;
	newNode->left = newNode->right = NULL;

	//insert node
	insert(root, newNode);
}
//***********************************************
//Function is called by destructor to delete nodes
//
//param- 1- tree to be deleted
//***********************************************
void biTree::destroySubTree(treeNode *nodePtr){
	if(nodePtr){
		if(nodePtr->left)
			destroySubTree(nodePtr->left);
	}
	if(nodePtr->right)
		destroySubTree(nodePtr->left);
	delete nodePtr;
}
//***********************************************
//Function finds a node in tree
//
//param- 1- num we are serching for
//return- if node is found true, if not false.
//***********************************************
bool biTree::searchNode(int num){
	treeNode *nodePtr = root;

	while(nodePtr){
		if(nodePtr->data ==num)
			return true;
		else if(num < nodePtr->data)
			nodePtr= nodePtr->left;
		else
			nodePtr= nodePtr->right;
	}
	return false;
}
//***********************************************
//Function that is public and calls deleteNode
//
//param- 1- data to be removed
//***********************************************
void biTree::remove(int num){
	deleteNode(num, root);
}
//***********************************************
//Function deletes a node, but not the whole tree
//
//param- 1- number/data we are looking to delete
//param- 2- the tree
//***********************************************
void biTree::deleteNode(int num, treeNode *&nodePtr){
	if(num < nodePtr->data)
		deleteNode(num, nodePtr->left);
	else if(num > nodePtr->data)
		deleteNode(num, nodePtr->right);
	else
		makeDeletion(nodePtr);
}
//***********************************************
//Function moves pointer to the node being removed/deleted
//
//param- 1- node that is being deleted
//***********************************************
void biTree::makeDeletion(treeNode*& nodePtr){
	treeNode *buffer; //buffer locations to store data while moving ptr's around


	if(nodePtr ==NULL)
		cout<<"ERROR:: Node is empty, removal is not possible from an empty node. \n";

	//if right empty, move child on left in it's place
	else if(nodePtr->right ==NULL){
		buffer = nodePtr;
		nodePtr = nodePtr->left;
		delete buffer;
	}
	//if left empty, move child on right in it's place
	else if(nodePtr->left ==NULL){
		buffer = nodePtr;
		nodePtr = nodePtr->right;
		delete buffer;
	}
	//Case when node has 2 children
	else{
		buffer = nodePtr->right;

		while(buffer->left)
			buffer = buffer->left;
		buffer->left = nodePtr->left;
		buffer = nodePtr;
		nodePtr = nodePtr->right;
		delete buffer;
	}
}
//***********************************************
//Function displays list InOrder
//
//param- 1- tree is passed to the function
//***********************************************
void biTree::displayInOrder(treeNode* nodePtr) const{
	if(nodePtr){
		displayInOrder(nodePtr->left);
		cout<< nodePtr->data <<endl;
		displayInOrder(nodePtr->right);
	}
}
//***********************************************
//Function displays the list in PreOrder
//
//param- 1- tree that needs to be displayed.
//***********************************************
void biTree::displayPreOrder(treeNode *nodePtr)const{
	if(nodePtr){
		cout<< nodePtr->data <<endl;
		displayPreOrder(nodePtr->left);
		displayPreOrder(nodePtr->right);
	}
}
//***********************************************
//Function displays list in postOrder
//
//param- 1- tree to be displayed is passed
//***********************************************
void biTree::displayPostOrder(treeNode* nodePtr)const{
	if(nodePtr){
		displayPostOrder(nodePtr->left);
		displayPostOrder(nodePtr->right);
		cout<< nodePtr->data <<endl;
	}
}
int biTree::leavesCount(treeNode* node)const{
	if(node == NULL)
	    return 0;
	  if(node->left == NULL && node->right==NULL)
	    return 1;
	  else
	    return leavesCount(node->left)+
	           leavesCount(node->right);
}
int biTree::nodeCount(treeNode* node)const{

	if (node == 0)
		return 0;
	return 1 + nodeCount(node->left) + nodeCount(node->right);

}


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
/*
 * untBSTDruver.cpp
 *
 *  Created on: Jul 1, 2013
 */
#include "intBST.h"
#include<iostream>

using namespace std;
int main(){

	biTree tree;

	cout << "Inserting nodes. ";

	tree.insertNode(5);
	tree.insertNode(8);
	tree.insertNode(3);
	tree.insertNode(12);
	tree.insertNode(9);
	cout << "Done.\n";
	// Display inorder.
	cout << "Inorder traversal:\n";
	tree.displayInOrder();
	// Display preorder.
	cout << "\nPreorder traversal:\n";
	tree.displayPreOrder();
	// Display postorder.
	cout << "\nPostorder traversal:\n";
	tree.displayPostOrder();
	// Search for the value 3.
	if (tree.searchNode(3))
	cout << "3 is found in the tree.\n";
	else
	cout << "3 was not found in the tree.\n";
	// Search for the value 100.
	if (tree.searchNode(100))
		cout << "100 is found in the tree.\n";
	else
		cout << "100 was not found in the tree.\n";

	/*Counting Number of Nodes.
	cout << "Number of Nodes: "<< tree.nodeCount() << endl;
	// Counting Number of Leaves .
	cout << "Number or Leaves: "<< tree.leavesCount() << endl;*/

	cout << endl;

	// Display the values.
	cout << "Here are the values in the tree:\n";
	tree.displayInOrder();

	// Delete the value 8.
	cout << "Deleting 8...\n";
	tree.remove(8);

	// Delete the value 12.
	cout << "Deleting 12...\n";
	tree.remove(12);

	// Display the values.
	cout << "Now, here are the nodes:\n";
	tree.displayInOrder();

}
Oh and I have this all set up in a separate Header file that just has the biTree class and treeNode struct inside it.
I believe in display sub tree, the if (nodeptr) doesnt extend to the right node.

I would do:
1
2
3
4
5
6
if ( !nodeptr ) return;

destroy( nodeptr->left );
destroy( nodeptr->right );

delete nodeptr;


As a side, at 107, put that if statement in brakets. I know you dont have to. I say, if one condition requires brakets, then they should all get them.

Nice tree though.
Ah, I see EXACTLY what you're talking about. I bet that will fix the issue. Thank you so much!

-maebe
Topic archived. No new replies allowed.