Tree class with node class

I wrote this as part of an assignment that I turned in, problem is I could not get some of the functions to work. I do have the add working and was able to get output in the add function, just not in the search or inorder. I now have a test in a couple of weeks and feel if I know what I did wrong on this one then I can avoid the same mistakes in the test. So any help in understanding what I'm doing wrong with this would be greatly appreciated.

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
  
// Node Header 
#ifndef node_H
#define node_H
#include <iostream>

using namespace std;

class node
{
private:
    int num;
	node *left;
	node *right;
	
public:
	node();
	
	node(int num);
 	
	friend class tree;
		
};

#endif 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
// Node.cpp file
#include "node.h"
#include <iostream>
using namespace std;

node::node()
{
    num;
	*left;
	*right;
}

node::node(int num)
{
    num = num;
	node *left;
	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
// Tree header
#ifndef TREE_H
#define TREE_H
#include <string>
#include "node.h"
using namespace std;

class tree
{
	private:
		node *root;
		//void inorder(node *);
		void add(int num, node *root);	
		//bool search(int num, node *root);
		
		
		
	public:
		tree();
		//bool search(int num);
		void add(int num);
		void xdelete(int num);
		void inorder();
		int height(node*);
		void inorder(node *);
		//{
		//inorder(root);
		//}

};

#endif 

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
// Tree.cpp
#include "tree.h"
#include <iostream>
using namespace std;


	tree::tree()
	{
	root = NULL;
	int num = 0;
	return;
	}
	/*
	bool tree::search(int num)
	{
		return search(num, root);
	}
	
	bool tree::search(int num, node *root)
	{
		if(root == NULL)
		return false;
		
		if(root->num == num)
		return true;
		
		if(root->num < num)
		return search(root->left);
		
		if(root->num > num)
		return search(root->right);
	} // end search
	*/
	void tree::add(int num, node *root)
	{
		if(num < root->num)
			{
			if(root->left != NULL)
			add(num, root->left);
				else
				{
					root->left = new node;
					root->left->num = num;
					root->left->left = NULL;
					root->left->right = NULL;
				}
				//cout << "root->left->num " << root->left->num << " ";
			}
			else
			if(num > root->num)
			{
				if(root->right != NULL)
					add(num, root->right);
				else
				{
					root->right = new node;
					root->right->num = num;
					root->right->left = NULL;
					root->right->right = NULL;
				}
				//cout << "root->right->num " << root->right->num << " ";
			}
	}
	
	
	void tree::add(int num)
	{
	if(root != NULL)
	add(num, root);
	else
		{
			root = new node;
			root->num = num;
			root->left = NULL;
			root->right = NULL;
			//cout << "root->num " << root->num << " ";
		}
	}
	/*	
	void tree::inorder(node *out)
	{
		if (out != NULL)
		{
			inorder(out->left);
			cout << out->num << " ";
			inorder(out->right);
		}
	}

	void tree::xdelete(num)
	{
		
	}	
	int tree::height(node *root)
	{
	int left;
	int right;
	if(root == NULL)
		return 0;
	else
		{
			left = height(root->left);
			right = height(root->right);
			
			if(left > right)
				return (left + 1);
			else
				return (right + 1);

		}
	}
*/

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
//Main file
using namespace std;

#include <iostream>
#include <sstream>
#include <string>
#include "tree.h"


int menu(); // menu function to send i/o to proper sections of program

int main()  // start of main function
{

menu();  // call to menu function

return 0;
}  // end of main function

// ****** start of menu functon *******************
int menu()  

// ***** declaired variables for use with menu **************

{
string action;   
int num;
tree tree1;

do  // start of do/while loop
{

cout << "bst> "; // main output
cin >> action;
	if(action == "quit") // start of conditionals
	{
		break;
	}
	
	else if(action == "add") // adds node
	{
	cin >> num;
		tree1.add(num);
	}
	/*
	else if(action == "delete")  // deletes a node 
	{
	cin >> num;
	tree1.xdelete(num);
	}
	
	else if(action == "search")  // compares input to nodes looking for a match
	{
	cin >> num;
	tree1.search(num);
	}
	
	
	else if(action == "height")  // prints out how tall the tree is
	{
	tree1.height(node*);
	}
	
	
	else if(action == "inorder")  // prints out the tree inorder
	{
	tree1.inorder(node);
	}
	
	else
	{
	cout << "Error! Invalid Command!" << endl;  // err command
	}
	*/
}while(action != "quit");
}  // end of do/while loop
Topic archived. No new replies allowed.