https://www.acmicpc.net/problem/1991
#include <iostream>
#include <vector>
using namespace std;
class BinaryNode {
public:
char data;
BinaryNode* left;
BinaryNode* right;
BinaryNode(char val = ' ', BinaryNode* l = NULL, BinaryNode* r = NULL)
:data(val), left(l), right(r){}
~BinaryNode(){}
void setData(char val) {
data = val;
}
char getData() {
return data;
}
void setLeft(BinaryNode* l) {
left = l;
}
BinaryNode* getLeft() {
return left;
}
void setRight(BinaryNode* r) {
right = r;
}
BinaryNode* getRight() {
return right;
}
};
class BinaryTree {
BinaryNode* root;
public:
BinaryTree()
:root(NULL){}
~BinaryTree(){}
void setRoot(BinaryNode* n) {
root = n;
}
BinaryNode* getRoot() {
return root;
}
void preorder(BinaryNode* node) {
if (node != NULL) {
cout << node->getData();
if (node->getLeft() != NULL) preorder(node->getLeft());
if (node->getRight() != NULL) preorder(node->getRight());
}
}
void inorder(BinaryNode* node) {
if (node != NULL) {
if (node->getLeft() != NULL) inorder(node->getLeft());
cout << node->getData();
if (node->getRight() != NULL) inorder(node->getRight());
}
}
void postorder(BinaryNode* node) {
if (node != NULL) {
if (node->getLeft() != NULL) postorder(node->getLeft());
if (node->getRight() != NULL) postorder(node->getRight());
cout << node->getData();
}
}
};
int main() {
int N;
cin >> N;
vector<BinaryNode*> node;
node.resize(N);
// BinaryNode 생성
for (int i = 0;i < N;i++) {
node[i] = new BinaryNode('A' + i);
}
// 노드 간 링크 연결
for (int i = 0;i < N;i++) {
char p, left, right;
cin >> p >> left >> right;
if (left != '.') node[p - 'A']->setLeft(node[left - 'A']);
if (right != '.') node[p - 'A']->setRight(node[right - 'A']);
}
// BinaryTree 생성
BinaryTree* tree = new BinaryTree();
tree->setRoot(node[0]); // BinaryNode* 자료형을 setRoot()에 넣어야하므로 'A'라고 넣으면 에러 발생한다
tree->preorder(tree->getRoot());
cout << endl;
tree->inorder(tree->getRoot());
cout << endl;
tree->postorder(tree->getRoot());
return 0;
}
위의 코드로 해결했지만 굳이 tree를 포인터로 구현해야 했을까?라는 생각이 들어서 포인터를 사용하지 않고 구현하기로 결심하고 두 번째 코드를 구현했다
#include <iostream>
#include <vector>
using namespace std;
class BinaryNode {
public:
char data;
BinaryNode* left;
BinaryNode* right;
BinaryNode(char val=' ',BinaryNode* l=NULL,BinaryNode* r=NULL)
:data(val),right(r),left(l){}
~BinaryNode(){}
void setData(char n) {
data = n;
}
char getData() {
return data;
}
void setLeft(BinaryNode* l) {
left = l;
}
BinaryNode* getLeft() {
return left;
}
void setRight(BinaryNode* l) {
right = l;
}
BinaryNode* getRight() {
return right;
}
};
class BinaryTree {
BinaryNode* root;
public:
BinaryTree()
:root(NULL){}
~BinaryTree(){}
void setRoot(BinaryNode* n) {
root = n;
}
BinaryNode* getRoot() {
return root;
}
void inorder(BinaryNode* node) {
if (node != NULL) {
if (node->getLeft() != NULL) inorder(node->getLeft());
cout << node->getData();
if (node->getRight() != NULL) inorder(node->getRight());
}
}
void preorder(BinaryNode* node) {
if (node != NULL) {
cout << node->getData();
if (node->getLeft() != NULL) preorder(node->getLeft());
if (node->getRight() != NULL) preorder(node->getRight());
}
}
void postorder(BinaryNode* node) {
if (node != NULL) {
if (node->getLeft() != NULL) postorder(node->getLeft());
if (node->getRight() != NULL) postorder(node->getRight());
cout << node->getData();
}
}
};
int main() {
int N;
cin >> N;
vector<BinaryNode*> node;
node.resize(N);
// BinaryNode 생성
for (int i = 0;i < N;i++) {
node[i] = new BinaryNode('A' + i);
}
for (int i = 0;i < N;i++) {
char p, left, right;
cin >> p >> left >> right;
if (left != '.') node[p - 'A']->setLeft(node[left - 'A']);
if (right != '.') node[p - 'A']->setRight(node[right - 'A']);
}
BinaryTree tree;
tree.setRoot(node[0]);
tree.preorder(tree.getRoot());
cout << endl;
tree.inorder(tree.getRoot());
cout << endl;
tree.postorder(tree.getRoot());
return 0;
}