#include "..\CookHeader.h"

typedef struct _TreeNode {
	struct _TreeNode* left = NULL;
	string data;
	struct _TreeNode* right = NULL;
} TreeNode;

Array <TreeNode*> memory;
TreeNode* root;
Array <string> nameAry = { "블랙핑크", "레드벨벳", "마마무", "에이핑크", "걸스데이", "트와이스", "잇지", "여자친구" };

void freeMemory() {
	for (int i = 0; i < len(memory); i++)
		delete memory[i];
}

int main() {
	TreeNode* node;
	TreeNode* current;

	node = new TreeNode;
	node->data = nameAry[0];
	root = node;
	memory.push_back(node);

	for (int i = 1; i < (int)nameAry.size(); i++) {
		string name = nameAry[i];
		node = new TreeNode;
		node->data = name;

		current = root;
		while (true) {
			if (name < current->data) {
				if (current->left == NULL) {
					current->left = node;
					break;
				}
				current = current->left;
			}
			else {
				if (current->right == NULL) {
					current->right = node;
					break;
				}
				current = current->right;
			}
		}
		memory.push_back(node);
	}

	string findName;
	input(findName, "찾을 그룹이름 --> ");
	current = root;
	while (true) {
		if (findName == current->data) {
			println(findName + " 찾음.");
			break;
		}
		else if (findName < current->data) {
			if (current->left == NULL) {
				println(findName + " 트리에 없음.");
				break;
			}
			current = current->left;
		}
		else {
			if (current->right == NULL) {
				println(findName + " 트리에 없음.");
				break;
			}
			current = current->right;
		}
	}
	freeMemory();
}