#include "..\CookHeader.h"

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

Array <TreeNode*> memory;
TreeNode* root;
Array <Array <string>> movieAry = { {"범죄도시3", "범죄도시3.mp4"}, {"플래시", " 플래시.mp4"}, 
									{"인어공주", "인어공주.mp4"}, {"슈퍼마리오", "슈퍼마리오.mp4"}, 
									{"드림팰리스", "드림팰리스.mp4"}, {"존윅4", " 존윅4.mp4"} };

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

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

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

	for (int i = 1; i < len(movieAry); i++) {
		Array<string> movie = movieAry[i];
		node = new TreeNode;
		node->data = movie;

		current = root;
		while (true) {
			if (movie[0] < current->data[0]) {
				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);
	}

	println("------- 넷플릭스 영화 목록 ------");
	printArray2(movieAry);
	println("");

	string findName;
	input(findName, "보고 싶은 영화 --> ");
	current = root;
	while (true) {
		if (findName == current->data[0]) {
			println(findName + " 찾음.");
			println(current->data[1] + " 영화가 Play 됩니다.");
			break;
		}
		else if (findName < current->data[0]) {
			if (current->left == NULL) {
				println(findName + " 트리에 없음.");
				break;
			}
			current = current->left;
		}
		else {
			if (current->right == NULL) {
				println(findName + " 영화는 없습니다.");
				break;
			}
			current = current->right;
		}
	}
	freeMemory();
}