Binary Tree Questions

Can someone please help me out. I am trying to get a the following:
nodeCount (This works)
leavesCount(Not counting correctly)
SingleParentCount(Also not counting correclty) Not sure how to do the last else statement when both the rLink and lLink exist.

Any help would be greatly appreciated. THANKS!

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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315

#include<iostream>
//#include<cstring>

using namespace std;

template <class T>
struct nodeType
{
	T info;
	nodeType<T> *lLink;
	nodeType<T> *rLink;
	
};

template <class T>
class binaryTreeType
{
public:
	const binaryTreeType<T>& operator =
		(const binaryTreeType<T>&);							//overload assignment operator
	bool isEmpty();											//Function to see if Tree is Empty
	void inorderTraversal();
	void preorderTraversal();
	void postorderTraversal();
	int treeHeight();										//Function to see the height of the tree
	int treeNodeCount();									//Function to count the number of nodes in the tree
	int treeLeavesCount();									//Function to count the number of leaves in the tree
	int treeSingleParentCount();							//Function to count the number of Single Parents in tree
	void destroyTree();										// Deallocates memory
	binaryTreeType(const binaryTreeType<T>& otherTree);		//copy constructor
	binaryTreeType();										//default constructor
	~binaryTreeType();										//Destructor
	
	

protected:
	nodeType<T> *root;
private:
	void copyTree(nodeType<T>* &copiedTreeRoot,
		nodeType<T>* otherTreeRoot);						//Function to copy the tree(to which p points)
	void destroy(nodeType<T>* &p);							//Function to destroy the tree(to which p points)
	void inorder(nodeType<T> *p);							//in order traversal of tree(to which p points)
	void preorder(nodeType<T> *p);							//pre order traversal of tree(to which p points)
	void postorder(nodeType<T> *p);							//post order traversal of tree(to which p points)
	int height(nodeType<T> *p);								//Function to see height of tree(to which p points)
	int max(int x, int y);									//Function to see which is larger x or y
	int nodeCount(nodeType<T> *p);							//Counts number of nodes(to which p points)
	int leavesCount(nodeType<T> *p);						//Counts number of leaves (to which p points)
	int singleParentCount(nodeType<T> *p);						//Returns Single Parent Nodes

};

template<class T>
int binaryTreeType<T>::singleParentCount(nodeType<T> *p)
{
	if(p==NULL)
		return 0;
	else
		if(p->lLink == NULL && p->rLink == NULL)
			return 1;
		else
			if (p->lLink == NULL && p->rLink != NULL)
				return 1 + singleParentCount(p->rLink);
			else
			if(p->rLink == NULL && p->lLink != NULL)
				return 1 + singleParentCount(p->lLink);
			else
				return singleParentCount(p->lLink); // need to return singleParentCount for right and left link
}

template<class T>
int binaryTreeType<T>::leavesCount(nodeType<T> *p)
{
	
	if(p == NULL)
		return 0;
	else
		if(p->lLink == NULL && p->rLink == NULL)
			return 1;
		else
			return 1 + leavesCount(p->lLink) + leavesCount(p->rLink);
}
	/*
	int leafCount;
	if (p->lLink != NULL)
	{
		 ++leafCount;
		leavesCount(p->lLink);
	}

	else if (p->rLink != NULL)
	{
		 ++leafCount;
		 leavesCount(p->rlink);
	}
	else
		leafCount = 0;

	return leafCount;

}
*/
template<class T>
int binaryTreeType<T>::nodeCount(nodeType<T> *p)
{
	if(p == NULL)
		return 0;
	else 
		return 1 + nodeCount(p->lLink) + nodeCount(p->rLink);
	
}

template<class T>
bool binaryTreeType<T>::isEmpty()
{
	return(root == NULL);
}

template <class T>
binaryTreeType<T>::binaryTreeType()
{
	root = NULL;
}

template<class T>
void binaryTreeType<T>::inorderTraversal()
{
	inorder(root);
}
template<class T>
void binaryTreeType<T>::preorderTraversal()
{
	preorder(root);
}

template<class T>
void binaryTreeType<T>::postorderTraversal()
{
	postorder(root);
}
template<class T>
int binaryTreeType<T>::treeHeight()
{
	return height(root);
}

template<class T>
int binaryTreeType<T>::treeNodeCount()
{
	return nodeCount(root);
}

template<class T>
int binaryTreeType<T>::treeLeavesCount()
{
	return leavesCount(root);
}

template<class T>
int binaryTreeType<T>::treeSingleParentCount()
{
	return singleParentCount(root);
}

template<class T>
void binaryTreeType<T>::inorder(nodeType<T> *p)
{
	if (p != NULL)
	{
		inorder(p->lLink);
		cout << p->info << " " ;
		inorder(p->rLink);
	}
}

template<class T>
void binaryTreeType<T>::preorder(nodeType<T> *p)
{
	if (p!= NULL)
	{
		cout << p->info << " ";
		preorder(p->lLink);
		preorder(p->rLink);
	}
}

template<class T>
void binaryTreeType<T>::postorder(nodeType<T> *p)
{
	if(p != NULL)
	{
		postorder (p->lLink);
		postorder (p->rLink);
		cout << p->info << " ";
	}
}

template<class T>
int binaryTreeType<T>::height(nodeType<T> *p)
{
	if(p == NULL)
		return 0;
	else
		return 1 + max(height(p->lLink), height(p->rLink));
}

template<class T>
int binaryTreeType<T>::max(int x, int y)
{
	if(x >= y)
		return x;
	else
		return y;
}

template<class T>
void binaryTreeType<T>::copyTree (nodeType<T>* &copiedTreeRoot,nodeType<T>* otherTreeRoot)

{
	if(otherTreeRoot == NULL)
	{
		copiedTreeRoot = NULL;
	}
	else
	{
		copiedTreeRoot = new nodeType<T>;
		copiedTreeRoot ->info = otherTreeRoot ->info;
		copyTree(copiedTreeRoot->lLink, otherTreeRoot->lLink);
		copyTree(copiedTreeRoot->rLink, otherTreeRoot->rLink);
	}
}

template<class T>
void binaryTreeType<T>::destroy(nodeType<T>* &p)
{
	if(p != NULL)
	{
		destroy(p->lLink);
		destroy(p->rLink);
		delete p;
		p = NULL;
	}
}

template<class T>
void binaryTreeType<T>::destroyTree()
{
	destroy(root);
}

template<class T>
binaryTreeType<T>::binaryTreeType(const binaryTreeType<T> &otherTree)
{
	if(otherTree.root == NULL)
		root = NULL;
	else
		copyTree(root,otherTree.root);
}
//---------------------------------------------- DESTRUCTOR ---------------------------------------------------
template<class T>
binaryTreeType<T>::~binaryTreeType()
{
	destroy(root);
}
//---------------------------------------------- OPERATOR ----------------------------------------------------
template<class T>
const binaryTreeType<T>& binaryTreeType<T>::operator =(const binaryTreeType<T>& otherTree)
{
	if(this != &otherTree)
	{
		if(root != NULL)
			destroy(root);
		if(otherTree.root == NULL)
			root = NULL;
		else
			copyTree(root,otherTree.root);
	}
	return *this;
}
//---------------------------------------------Derived Class bSearchTreeType------------------------------
template <class T>
class bSearchTreeType: public binaryTreeType<T>
{
public:
	bool search(const T& searchItem);
	void insert(const T& insertItem);
	void deleteNode(const T& deleteItem);
private:
	void deleteFromTree(nodeType<T>* &p);
};

template<class T>
bool bSearchTreeType<T>::search(const T& searchItem)
{
	nodeType<T> *current;
	bool found = false;

	if(root == NULL)
		cerr << "Tree is empty, cannot search. " << endl;
	else
	{
		current = root;
		while(current != NULL && !found)
		{
			if(current->info == searchItem)
			found = true;
			else
				if(current->info > searchItem)
					current = current->lLink;
				else
					current = current->rLink;
		}	
	}
}

Rest of code will be in reply
Here is the rest of the 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
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
186
187
188
189
190
191
192
193
194
195
196
//------------------------------------------------------------ INSERT ---------------------------------------------
template<class T>
void bSearchTreeType<T>::insert(const T& insertItem)
{
	nodeType<T> *current;
	nodeType<T> *trailCurrent;
	nodeType<T> *newNode;

	newNode = new nodeType<T>;
	//assert(newNode != NULL);
	newNode->info = insertItem;
	newNode->lLink = NULL;
	newNode->rLink = NULL;

	if(root == NULL)
		root = newNode;
	else
	{
		current = root;

		while(current != NULL)
		{
			trailCurrent = current;

	 		if(current->info == insertItem)
			{
				cerr << "This is already in the list. " << endl;
				cerr << "Duplicates are not Allowed. " << endl;
				return;
			}
			else
				if(current->info > insertItem)
					current = current->lLink;
				else
					current = current->rLink;
		}
		if(trailCurrent->info > insertItem)
			trailCurrent->lLink = newNode;
		else
			trailCurrent->rLink = newNode;
	}
}

template<class T>
void bSearchTreeType<T>::deleteFromTree(nodeType<T>* &p)
{
	nodeType<T> *current;

	nodeType<T> *trailCurrent;
	nodeType<T> *temp;

	if(p == NULL)
		cerr << "Error: the node to be deleted has no value. " <endl;
	else if(p->lLink == NULL && p->rLink == NULL)
	{
		temp = p;
		p = NULL;
		delete temp;
	}
	else if(p->lLink == NULL)
	{
		temp = p;
		p = temp->rLink;
		delete temp;
	}
	else if(p->rLink == NULL)
	{
		temp = p;
		p = temp->lLink;
		delete temp;
	}
	else
	{
		current = p->lLink;
		trailCurrent = NULL;

		while(current->rLink != NULL)
		{
			trailCurrent = current;
			current = current->rLink;
		}
		p->info = current->info;

		if(trailCurrent == NULL)
			p->lLink = current->lLink;
		else
			trailCurrent->rLink = current->lLink;
		delete current;
	}
}

template<class T>
void bSearchTreeType<T>::deleteNode(const T& deleteItem)
{
	nodeType<T> *current;
	nodeType<T> *trailCurrent;
	bool found = false;

	if(root == NULL)
		cerr << "Tree is empty, cannot delete." << endl;
	else
	{
		current = root;
		trailCurrent = root;

		while(current != NULL && !found)
		{
			if(current->info == deleteItem)
				found = true;
			else
			{
				trailCurrent = current;

				if(current->info > deleteItem)
					current = current->lLink;
				else
					current = current->rLink;
			}
		}

		if(current == NULL)
			cout << "The delete item is not in the list." << endl;
		else
			if(found)
			{
				if(current == root)
					deleteFromTree(root);
				else
					if(trailCurrent->info > deleteItem)
						deleteFromTree(trailCurrent->lLink);
					else
						deleteFromTree(trailCurrent->rLink);
			}
	}
}

/*
template <class T>
void binaryTreeType<T>::nonRecursiveInTraversal()
{
	stackType<nodeType<T>* > stack;
	nodeType<T> *current;
	current = root;

	while((current != NULL || (!stack.isEmptyStack()))
	if (current != NULL)
	{
		stack.push(current);
		current = current->lLink;
	}
	else
	{
		current = stack.top();
		stack.pop();
		cout<<current->info << "  ";
		current = current->rLink;
	}
	cout << endl;
}
*/






int main()
{

	 bSearchTreeType<int>myTree;
	 myTree.insert(32);
	 myTree.insert(16);
	 myTree.insert(64);
	 myTree.insert(48);
	 myTree.insert(80);
	 myTree.insert(5);
	 myTree.insert(15);
	 myTree.treeNodeCount();
	 myTree.treeLeavesCount();
	 myTree.treeSingleParentCount();

	

	/*
	int num;
	cout <<" Enter Values, when you want to quite enter -123" << endl;
	cin >> num;
	while(num != -123)
	{
		myTree.insert(num);
		cin >> num;
	}
	*/
	system("PAUSE");
	return 0;
}
Okay, there's this little thing called the this pointer you need to learn about:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

struct A{
	int a;
	int set(){
		return this->a=10;
	}
};

int main(){
	A a;
	std::cout <<a.set()<<std::endl;
	return 0;
}


Fix that and then come back.

EDIT: Just in case it wasn't clear enough, what I'm saying is that you're needlessly duplicating each function.
Last edited on
Topic archived. No new replies allowed.