Binary Tree

Hello!
I need to make a binary tree that contains names and numbers. I'm pretty sure I got most of the code right. The only trouble I am having is how to order the tree using the names of people. My book explains how to order using numbers and that makes sense but I'm not sure how to do that with letters.

Also there are a few errors that I can't seem to fix.

Thanks for the help!!


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
#include <iostream>

using namespace std;

struct TreeNode;
struct ItemType;

class TreeType
{
public:
	TreeType();
	~TreeType();
	bool IsEmpty() const;
	bool IsFull() const;
	void RetrieveItem(ItemType& item, bool& found) const;
	void InsertItem(ItemType item);
	void DeleteItem(ItemType item);
	void Print(std::ofstream& outFile) const;
private:
	TreeNode* root;
};

struct ItemType
{
	string name;
	string number;
};

struct TreeNode
{
	ItemType info;
	TreeNode* left;
	TreeNode* right;
};

bool TreeType::IsFull() const
{
	TreeNode* location;
	try
	{
		location=new TreeNode;
		delete location;
		return false;
	}
	catch(std::bad_alloc exeception)
	{
		return true;
	}
}

bool TreeType::IsEmpty() const
{
	return root==NULL;
}

void Retrieve(TreeNode* tree, ItemType& item, bool& found);

void TreeType::RetrieveItem(ItemType& item, bool& found) const
{
	Retrieve(root, item, found);
}

void Retrieve(TreeNode* tree, ItemType& item, bool& found)
{
	if (tree==NULL)
	{
		found=false;
	}

	else if (item < tree->info)
	{
		Retrieve(tree->left, item, found);
	}

	else if (item > tree->info)
	{
		Retrieve(tree->right, item, found);
	}

	else
	{
		item=tree->info;
		found=true;
	}
}

void Insert(TreeNode*& tree, ItemType item);

void TreeType::InsertItem(ItemType item)
{
	Insert(root, item);
}

void Insert(TreeNode*& tree, ItemType item)
{
	if (tree==NULL)
	{
		tree=new TreeNode;
		tree->right= NULL;
		tree->left= NULL;
		tree->info= item;
	}

	else if (item < tree->info)
	{
		Insert(tree->left, item);
	}

	else
	{
		Insert(tree->right, item);
	}

void DeleteNode(TreeNode*& tree);

void Delete(TreeNode*& tree, ItemType item);

void TreeType::DeleteItem(ItemType item)
{
	Delete(root, item);
}

void Delete(TreeNode*& tree, ItemType item) 

//Error: member function "TreeType::DeleteItem" may not be redeclared outside its class

{
	if (item < tree->info)
	{
		Delete(tree->left, item);
	}

	else if (item > tree->info);
	{
		Delete(tree->right, item)
	}

	else
	{
		DeleteNode(tree);
	}
}

void GetPredecessor(TreeNode* tree, ItemType& data);

void DeleteNode(TreeNode*& tree)
	{
		ItemType data;
		TreeNode* tempPtr;

		tempPtr=tree;
	
		if(tree->left==NULL)
		{
			tree=tree->right;
			delete tempPtr;
		}

		else if(tree->right==NULL)
		{
			tree=tree->left;
			delete tempPtr;
		}

		else
		{
			GetPredecessor(tree->left, data);
			tree->info=data;
			Delete(tree->left, data); 
//Error: identifier "data" is undefined	

		}
}

void GetPredecessor(TreeNode* tree, ItemType& data)
{
	while(Tree->right !=NULL)
		tree=tree->right;
		data=tree->info;
}

void PrintTree(TreeNode* tree, std::ofstream& outFile)
{
	if(tree !=NULL)
	{
		PrintTree(tree->left, outfile);
		outFile<<tree->info;
		PrintTree(tree->right, outfile);
	}
}

void TreeType::Print(std::ofstream& outFile) const
{
	PrintTree(root,outFile);
}

TreeType::TreeType()
{
	root=NULL;
}

void Destory(TreeNode*& tree);

TreeType::~TreeType()
{
	Destory(root);
}

void Destroy(TreeNode*& tree)
{
	if (tree !=NULL)
	{
		Destroy(tree->left);
		Destory(tree->right);
		delete tree;
	}
}
Number and letters work the same way in c++. Look up the Ascii table if you want verification, since it is the basis for most computer code. Other languages may be slightly different but in the same concept.

One question I have is why do I have two functions for every step? If I define it in the base of the tree I can use it from the class and I don't need the second definition. Tree's would be a lot simpler than this in their code structure.
Yeah I thought I knew that much but my code has an error where ever I used "<" or ">" so I thought maybe it was cause I used a string.

And I'm not sure. My book had it structured and I didn't want to mess things up.
Ok so I have looked over my code a little more and this is what I came up with.




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
#include <iostream>

using namespace std;

struct TreeNode;
struct ItemType;

class TreeType
{
public:
	TreeType();
	~TreeType();
	bool IsEmpty() const;
	bool IsFull() const;
	void RetrieveItem(ItemType& item, bool& found) const;
	void InsertItem(ItemType item);
	void DeleteItem(ItemType item);
	void Print(std::ofstream& outFile) const;
private:
	TreeNode* root;
};

struct ItemType
{
	string name;
	string number;
};

struct TreeNode
{
	ItemType info;
	TreeNode* left;
	TreeNode* right;
};

bool TreeType::IsFull() const
{
	TreeNode* location;
	try
	{
		location=new TreeNode;
		delete location;
		return false;
	}
	catch(std::bad_alloc exeception)
	{
		return true;
	}
}

bool TreeType::IsEmpty() const
{
	return root==NULL;
}

void RetrieveItem(TreeNode* tree, ItemType& item, bool& found)
{
	if (tree==NULL)
	{
		found=false;
	}

	else if (item.name < tree->info)
	{
		RetrieveItem(tree->left, item, found);
	}

	else if (item.name > tree->info)
	{
		RetrieveItem(tree->right, item, found);
	}

	else
	{
		item=tree->info;
		found=true;
	}
}

void TreeType::InsertItem(ItemType item)
{
	Insert(root, item);
}

void Insert(TreeNode*& tree, ItemType item)
{
	if (tree==NULL)
	{
		tree=new TreeNode;
		tree->right= NULL;
		tree->left= NULL;
		tree->info= item;
	}

	else if (item.name < tree->info)
	{
		Insert(tree->left, item);
	}

	else
	{
		Insert(tree->right, item);
	}
}

//void Delete(TreeNode*& tree, ItemType item);

void DeleteItem(TreeNode*& tree, ItemType item)
{
	if (item < tree->info)
	{
		DeleteItem(tree->left, item);
	}

	else if (item > tree->info);
	{
		DeleteItem(tree->right, item);
	}

	else
	{
		DeleteNode(tree);
	}
}

void GetPredecessor(TreeNode* tree, ItemType& data);

void DeleteNode(TreeNode*& tree)
	{
		ItemType data;
		TreeNode* tempPtr;

		tempPtr=tree;
	
		if(tree->left==NULL)
		{
			tree=tree->right;
			delete tempPtr;
		}

		else if(tree->right==NULL)
		{
			tree=tree->left;
			delete tempPtr;
		}

		else
		{
			GetPredecessor(tree->left, data);
			//tree->info=data;
			DeleteItem(tree->left,data);
		}
}

void GetPredecessor(TreeNode* tree, ItemType& data)
{
	while(Tree->right !=NULL)
		tree=tree->right;
		data=tree->info;
}

void PrintTree(TreeNode* tree, std::ofstream& outFile)
{
	if(tree !=NULL)
	{
		PrintTree(tree->left, outfile);
		outFile<<tree->info;
		PrintTree(tree->right, outfile);
	}
}

void TreeType::Print(std::ofstream& outFile) const
{
	PrintTree(root,outFile);
}

TreeType::TreeType()
{
	root=NULL;
}

void Destory(TreeNode*& tree);

TreeType::~TreeType()
{
	Destory(root);
}

void Destroy(TreeNode*& tree)
{
	if (tree !=NULL)
	{
		Destroy(tree->left);
		Destory(tree->right);
		delete tree;
	}
}
Topic archived. No new replies allowed.