Binary Tree

I have to create a Ordered Binary Tree for an assignment, and I'm stuck :/
I need to supply a default constructor for my CNode nested class or else when I allocate memory for the Nodes pointer I get an error. But once the default constructor values have been set for all of the elements I can't "overwrite" them to give the actual values I want. How can I do this?

Here's my tree class:
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
#pragma once
#include<iostream>

class CTree
{
private:
	class CNode
	{
	public:
		CNode* pRight;		// pointer to right child node
		CNode* pLeft;		// pointer to left child node
		int value;			// this nodes value

		CNode(int val = 1, CNode* right = 0, CNode* left = 0)
			:  value(val), pRight(right), pLeft(left)		// initialize everything
		{ }
	};
	
	CNode* nodes;
	int nNodes;		// number of nodes
public:							  
	void print();
	CTree();
	~CTree()		// free up memory
	{ delete[] nodes; }

};

CTree::CTree()
{
	nodes = new CNode[4];				// random data
	nodes[0] = (120, nodes[1], nodes[2]);
	nodes[1] = (437, nodes[3], 0);
	nodes[2] = (43, 0, 0);
	nodes[3] = (766, 0, 0);
	nNodes = 4;
}			  

void CTree::print()
{
	for(int i = nNodes-1; i >= 0; i--)			// output the nodes
		std::cout << "Node[" << i << "] = " << nodes[i].value << std::endl;
}


Thanks :)

EDIT: Crap, I had a typo :/ Now my output is just giving me this:

Node[3] = 0
Node[2] = 0
Node[1] = 0
Node[0] = 1
Last edited on
Got it working, never mind!
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
#pragma once
#include<iostream>

class CTree
{
private:
	class CNode
	{
	public:
		CNode* pRight;		// pointer to right child node
		CNode* pLeft;		// pointer to left child node
		int value;			// this nodes value

		CNode(int val = 1, CNode* right = 0, CNode* left = 0)
			:  value(val), pRight(right), pLeft(left)		// initialize everything
		{ }
	};		 
	
	CNode* nodes[4];
	int nNodes;		// number of nodes
public:							  
	void print();
	CTree();
};

CTree::CTree()
{
	nodes[0] = new CNode(120, nodes[1], nodes[2]);
	nodes[1] = new CNode(437, nodes[3], 0);
	nodes[2] = new CNode(43, 0, 0);
	nodes[3] = new CNode(766, 0, 0);
	nNodes = 4;
}			  

void CTree::print()
{
	for(int i = nNodes-1; i >= 0;  i--)
		std::cout << nodes[i]->value << std::endl;
}
Last edited on
Why are the nodes being stored in an array? Doesn't that circumvent the whole idea of a binary tree?
I see your point, but please don't make me rewrite this D:
I feel like I should, but I don't want to :-(
Last edited on
closed account (D80DSL3A)
Hi ascii. Is this chapter 9 exercise 4 from Hortons Beginning Visual C++ 2008 ?

Your binary tree is hard coded to have exactly 4 nodes. I think the idea is for the tree to hold as many data items as you have to store.
Notice the hint at the end of the exercise: Don't be afraid to use recursion!
Recursive methods seem to be very well suited to a binary tree.
Care to see what I got for this exercise?
fun2code, it is. It's been a good book so far but I don't want to have to buy a full version of Visual Studio just to use the MFC for one book (I'm never using it again xD) :-) I suppose I misinterpreted the question. For the first 7 or so chapters I kept overdoing all of the excersises, and I suppose I underdid this one. I already downloaded the answer and compared it to mine only to realize how wrong I was. Anyhow I'm on chapter 10 now, and having already seen the answer I don't really see the point in redoing it. Thank you anyhow :-)
Last edited on
closed account (D80DSL3A)
I agree about the book. Great through ch. 10 but then I'm outta there. I'd like to know more about windows programming but his exclusive use of the MFC is a turn off. I'll learn it elsewhere.

I'm going to get the C++ Primer next. Looks like that will keep me busy for a while!
Topic archived. No new replies allowed.