๋ฐ์ํ
์๋์ ์์์ ๋ฐํ์ผ๋ก ์์ฑ๋์์ต๋๋ค. ์ข์์์ ๊ฐ์ฌํฉ๋๋ค.
[์๋ฃ๊ตฌ์กฐ ์๊ณ ๋ฆฌ์ฆ] Tree์ Balance ํ์ธํ๊ธฐ
https://youtu.be/-m154rqFQng
(1) .h
์ฝ๋1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <iostream> | |
#include <algorithm> | |
#include <vector> | |
#include <queue> | |
using namespace std; | |
class Node | |
{ | |
public: | |
int data; | |
Node* left, * right; | |
Node(int data) { | |
this->data = data; | |
this->left = this->right = NULL; | |
} | |
}; | |
class Level | |
{ | |
public: | |
int min = INT_MAX; | |
int max = INT_MIN; | |
}; | |
class Tree | |
{ | |
private: | |
Node* root = NULL; | |
int min = INT_MIN; | |
vector<vector<Node*>> levels; | |
public: | |
Tree(vector<int> a) | |
{ | |
root = bst(a, 0, a.size() - 1); | |
} | |
Node* bst(vector<int> a, int start, int end) | |
{ | |
if (start <= end) | |
{ | |
int mid = (start + end) / 2; | |
Node* newNode = new Node(a[mid]); | |
newNode->left = bst(a, start, mid - 1); | |
newNode->right = bst(a, mid + 1, end); | |
return newNode; | |
} | |
return NULL; | |
} | |
//๋ฐธ๋ฐ์ค์ฒดํฌ1 : ์๊ฐ๋ณต์ก๋ O(nlogn) | |
bool isBalanced() | |
{ | |
return isBalanced(root); | |
} | |
bool isBalanced(Node* node) | |
{ | |
//์ฌ๊ท ๋๋ฆฌ๋๋ฐ, ๊ฐ ์๋ธํธ๋ฆฌ์ ๊ธธ์ด๋ฅผ ์ฐ๋ค | |
if (node == NULL) return true; | |
int heightDiff = getHeight(node->left) - getHeight(node->right); | |
if (abs(heightDiff) > 1) return false; | |
else { | |
return isBalanced(node->left) && isBalanced(node->right); | |
} | |
} | |
int getHeight(Node* node) | |
{ | |
if (node == NULL) return -1; | |
return max(getHeight(node->left), getHeight(node->right)) + 1; | |
} | |
//๋ฐธ๋ฐ์ค์ฒดํฌ2 : ์๊ฐ๋ณต์ก๋ O(n) | |
bool isBalanced2() | |
{ | |
//checkHeight๋ฆฌํด๊ฐ์ด min์๋๋ฉด true๋ฐํ | |
return checkHeight(root) != min; | |
} | |
int checkHeight(Node* node) | |
{ | |
if (node == NULL) return -1; | |
int left = checkHeight(node->left); | |
int right = checkHeight(node->right); | |
if (left == min || right == min) return min; | |
int heightDiff = left - right; | |
if (abs(heightDiff) > 1) return min; | |
else { | |
return max(left, right) + 1; | |
} | |
} | |
//์ข๋ ์๊ฒฉํ ๋ฐธ๋ฐ์ค์ฒดํฌ(์ ์๋ธํธ๋ฆฌ์ ๊ธธ์ด ์ฐจ๊ฐ 2์ด์์ด๋ฉด ์ธ๋ฐธ๋ฐ์ค) | |
bool isBalanced3() | |
{ | |
return isBalanced3(root); | |
} | |
bool isBalanced3(Node* node) | |
{ | |
Level* obj = new Level; | |
checkBalanced(node, obj, 0); | |
if (abs(obj->min - obj->max) > 1) return false; | |
else return true; | |
} | |
void checkBalanced(Node* node, Level* obj, int level) | |
{ | |
if (node == NULL) | |
{ | |
if (level < obj->min) obj->min = level; | |
else if (level > obj->max) obj->max = level; | |
return; | |
} | |
checkBalanced(node->left, obj, level + 1); | |
checkBalanced(node->right, obj, level + 1); | |
} | |
void bstToList() | |
{ | |
levels = bstToList(root); | |
} | |
vector<vector<Node*>> bstToList(Node* node) | |
{ | |
vector<vector<Node*>> lists; | |
vector<Node*> current; | |
if (node != NULL) current.push_back(node); | |
lists.push_back(current); | |
while (!current.empty()) | |
{ | |
vector<Node*> parent = current; | |
current.clear(); | |
for (int i = 0; i < parent.size(); i++) | |
{ | |
if (parent[i]->left) current.push_back(parent[i]->left); | |
if (parent[i]->right) current.push_back(parent[i]->right); | |
} | |
if (!current.empty()) lists.push_back(current); | |
} | |
return lists; | |
} | |
void print()//๋ ๋ฒจ๋ณ ๋ฐ์ดํฐ๊ฐ ์ถ๋ ฅ | |
{ | |
for (int i = 0; i < levels.size(); i++) | |
{ | |
for (int j = 0; j < levels[i].size(); j++) | |
cout << levels[i][j]->data << " "; | |
cout << endl; | |
} | |
} | |
}; |
(2) .cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "header.h" | |
int main() { | |
//์ ๋ ฌ ๋์ด์๋ ๋ฐฐ์ด์ ๋ฃ๋๋ค | |
Tree* t = new Tree({ 0,1,2,3,4,5,6,7,8,9}); | |
t->bstToList(); | |
t->print(); | |
(t->isBalanced() == true) ? puts("balanced") : puts("unbalanced"); | |
(t->isBalanced2() == true) ? puts("balanced") : puts("unbalanced"); | |
(t->isBalanced3()==true) ? puts("balanced") : puts("unbalanced"); | |
return 0; | |
} |
๋ฐ์ํ