Building a Tree problem

Not asking for solution or something
but seems like every Time i wan tto run this code
its stucks ..(something wrong) and i cant find where's the problem
in my program
about the code :
building Treanry Tree
the found function is to check weather the father = son1* son2 *son3;
:

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
 #pragma once

class TT {          
private:
	TT *left;
	TT *right;
	TT *mid;
	int data;
public:
	TT(int);
	~TT();
	void setRight(TT*);
	void setLeft(TT*);
	void setMid(TT*);
	int getd();
     TT *getLeft();
	TT *getRight();
	TT *getMid();
	void setd(int);
	int found(TT*);       


};

#include "TT.h"
#include<iostream>
TT::TT(int data) {
	left = right = mid= NULL;
	this->data = data;
}
TT::~TT() {
	delete right;
	delete left;
	delete mid;
}
void TT::setRight(TT* right) {
	this->right = right;
}
void TT::setLeft(TT *l) {
	left = l;
}
void TT::setMid(TT *m) {
	mid = m;

}
TT *TT::getRight() { return right; }
 TT *TT::getLeft(){ return left; }
TT *TT::getMid() { return mid; }
int TT::getd() { return data; }
void TT::setd(int data) { this->data = data; }


int TT::found(TT *pointer) {
	int s = 0;
	if (pointer->getLeft() == NULL && pointer->getMid() == NULL &&pointer->getMid() == NULL) {
		return 0;
	}
	if (pointer->getRight() != NULL) {
		s += found(pointer->getRight());
	}
	if (pointer->getLeft() != NULL) {
		s += found(pointer->getLeft());
	}
	if (pointer->getRight() != NULL) {
		s += found(pointer->getMid());
	}
	if (pointer->getRight() != NULL && pointer->getLeft() != NULL && pointer->getMid() != NULL && pointer->getLeft()->getd()*pointer->getRight()->getd()*pointer->getMid()->getd()==pointer->getd()) {
		s = s + 1;
	}
	return s;
}


#include"TT.h"
using namespace std;
#include<iostream>

int main() {
	TT * a = new TT(5040);
	a->setLeft(new TT(24));
	a->getLeft()->setLeft(new TT(2));
	a->getLeft()->setMid(new TT(3));
	a->getLeft()->setRight(new TT(4));


	a->setRight(new TT(210));
	a->getRight()->setLeft(new TT(5));
	a->getRight()->setMid(new TT(6));
	a->getRight()->setRight(new TT(7));

	cout <<(a->found(a)) << endl;
	//delete a;
	system("pause");
	return 0;
};
Last edited on
if (pointer->getLeft() == NULL && pointer->getMid() == NULL &&pointer->getMid() == NULL)
Not sure if it's the only problem, but you seem to have a copy-paste holdover in there :)
getRight() is never checked.

Edit: You make a similar copy here
1
2
3
4
5
6
7
8
	if (pointer->getRight() != NULL) {
		s += found(pointer->getRight());
	}
	if (pointer->getLeft() != NULL) {
		s += found(pointer->getLeft());
	}
	if (pointer->getRight() != NULL) { // change to getMid()
		s += found(pointer->getMid());


Also, a tip:
Every time i want to run this code, its stucks

You should be more specific in explaining the problem.
Is the code crashing, or does the code simply never end (infinite loop)?

Crashing is more likely when you are dereferencing a null pointer; an infinite loop is more likely when you have circular pointers referencing each other. But in your case, it's probably crashing when you try to dereference pointer->getMid() in your 4th if-statement.
Last edited on
yes i already fix what u said agian.
but its says "the system cant find the file specified "
and still have probelm with calling the getleft();
here the comiler point me with the problem

if (pointer->getLeft() == NULL && pointer->getRight() == NULL &&pointer->getMid() == NULL ) {
return 0;
}

its point me to the pointer->getLeft() and its says expression must be modeifiable lvalue
Last edited on
the system cant find the file specified

Well, that sounds like a problem with your environment, not the code. You have a misplaced source file, or something. Would need more details.

Edit: Assume that your code is in separate files since you have #include "TT.h", and that isn't your literal code.
Last edited on
I don't get that error. Can you post your updated code?
ganado tnx alot for youre help i mange to fix it .
very appricate it <:
You're welcome.
Registered users can post here. Sign in or register to post.