#include <iostream>
#include <fstream>
using namespace std;
int finalheight = 0;
class Node
{
public:
int num;
int leftChild;
int rightChild;
};
class Tree
{
public:
Node* arraya;
void TreeSet(int n)
{
arraya = new Node[n];
}
~Tree()
{
delete [] arraya;
}
void Preorder(int n, int height)
{
if (n != -1)
{
if (height > finalheight)
finalheight = height;
height++;
Preorder(arraya[n].leftChild, height);
Preorder(arraya[n].rightChild, height);
}
}
void Preorder()
{
Preorder(1, 1);
}
};
int main()
{
ifstream input;
input.open("input.txt");
if(!input.is_open())
{
cerr << "File is not found" << endl;
return 0;
}
int datanum = 0;
input >> datanum;
Tree* tree = new Tree[datanum];
int i = 0;
for(int t = 0; t < datanum; t++)
{
int nodenum = 0;
i = 0;
for(i; !input.eof(); i++)
{
if ( i > nodenum ) break;
if ( i == 0 )
{
input >> nodenum;
tree[t].TreeSet(nodenum);
}
else
{
input >> tree[t].arraya[i].num >> tree[t].arraya[i].leftChild >> tree[t].arraya[i].rightChild;
}
}
}
for(int i = 0; i < datanum; i++)
{
tree[i].Preorder();
cout << finalheight << endl;
finalheight = 0;
}
input.close();
delete [] tree;
}
디버깅을 해서 오류가 나는 부분은 알아냈는데, 도대체 뭘 어떻게 고쳐야 되는지를 모르겠습니다.
저 오류가 나는 이유랑 해결법 좀 친절하게 가르쳐줄 횽님 없나요 ㅠㅠ
아 참, 일단은 동작을 시키는게 먼저라 캡슐화고 뭐고 안드로메다로 던져놨으니 거슬려보여도 넘어가주세요 ㅠㅠ
이 프로그램은 'input.txt' 에서 트리의 개수, 트리 내의 노드 개수, 트리 정보를 입력받아서 트리 높이를 알아내는 프로그램입니다 ㅜㅡ
추가 : Tree 클래스의 'arraya' 포인터에 할당한 동적 Node 객체배열을 delete[]하려하면 오류 뿜뿜.. 골치아픕니다
메모리 해제는 소멸자에서 하는거 아니다
ㄴ Aㅏ... 일단 고쳐보겠습니다
소멸자에서 빼서 따로 해봤는데도 오류 뿜뿜하네요 ㅜㅡ
소멸자 자체가 메모리 해제 위해 만들어진 문법 아니던가?
ㄴ 저도 그렇게 알고 있어서 소멸자에 넣어 봤었는데, 소멸자에서 하느냐 마느냐는 오류 뿜뿜에 별 관계가 없었습니다.. ㅜㅡ
니가 문제입니다