Tree using Classes Problem

The Sum function is not working.please help me :D

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
  #include<iostream>
using namespace std;
struct node{
	int data;
	node *large;
	node *small;
};

class bst{
	public:
		bst();
		void add(int x);
		bool isempty();
		node *getroot();
		void print(node *p);
		int max();
		int min();
		int total(node *p);
private :
	node *root;
		};

bst::bst()
{

	root=NULL;
}

bool bst::isempty()
{
	return(root==NULL);
}
void bst::add(int x)
{
	node*cur=root,*trail,*ptr;
	ptr=new node;
	ptr->data=x;
	ptr->large=NULL;
	ptr->small=NULL;

	if(isempty())
	{
		root=ptr;
	}
	else{
		while(cur!=NULL)
		{
			trail=cur;
			if (cur->data== x)
			{
				cout << "The item is already in the tree \n";
				system("PAUSE");
				return;
			}

			else if(cur->data>x)
				cur=cur->small;
				else
					cur=cur->large;
		}
		if(trail->data>x)
			trail->small=ptr;
			else
				trail->large=ptr;


	}

}
node* bst::getroot()
{
	return root;
}
void bst::print(node *p)
{
	if(p!=NULL)
	{
		print(p->small);
		cout<<p->data<<" ";
		print(p->large);
	}
}
int bst:: max()
{
	node *cur=root;
	while(cur->large!=NULL)
		cur=cur->large;
	return cur->data;
}
int bst::min()
{
	node *cur=root;
	while(cur->small!=NULL)
		cur=cur->small;
	return cur->data;
}
int bst::total(node *p)
{
 static int sum1=0;
 static int sum2=0;

	node *cur=root;
	while(cur->large!=NULL){
		sum1=sum1+cur->data;
		cur=cur->large;}

	cur=root;
	while(cur->small!=NULL){
		sum2=sum2+cur->data;
		cur=cur->small;
	}
	return sum2+sum1+root->data;


}


	
		


int main()
{
	int x;
	bst tree;
	int y;
	cout<<"please enter a number "<<endl;
	cin>>x;
	while(x>0)
	{
		y=x%10;
		x=x/10;
		tree.add(y);
	}

	tree.print(tree.getroot());
	cout<<endl;
	
	cout<<"the max is "<<tree.max()<<endl;
	cout<<"The min is "<<tree.min()<<endl;
	cout<<"the sum is"<<tree.total(tree.getroot())<<endl;

	system("pause");
	return 0;
}



	

It is not working because you have not properly iterated the tree. You have simply calculated the sum of the outer nodes of the tree, the inner nodes still have to be accounted for.

Hint: How is your print function displaying all nodes?
Well, The print Function is by Recursion. i had thought about using recursion. i will try using recursion. Thank you.
i had solve it using this method:


[output][int main()
{
int x;
bst tree;
int y;
cout<<"please enter a number "<<endl;
cin>>x;
int sum=0;
while(x>0)
{
y=x%10;
sum=sum+y;
x=x/10;
tree.add(y);
}

tree.print(tree.getroot());
cout<<endl;

cout<<"the max is "<<tree.max()<<endl;
cout<<"The min is "<<tree.min()<<endl;
cout<<"the sum is"<<sum<<endl;

system("pause");
return 0;
}


/output]
Topic archived. No new replies allowed.