일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | |||||
3 | 4 | 5 | 6 | 7 | 8 | 9 |
10 | 11 | 12 | 13 | 14 | 15 | 16 |
17 | 18 | 19 | 20 | 21 | 22 | 23 |
24 | 25 | 26 | 27 | 28 | 29 | 30 |
Tags
- String
- 다익스트라
- Brute Force
- Stored Procedure
- 그래프
- union find
- SQL
- binary search
- 스토어드 프로시저
- Dijkstra
- DP
- MYSQL
- Hash
- Trie
- Two Points
- 이진탐색
- two pointer
Archives
- Today
- Total
codingfarm
BST(Binary Search Tree) 본문
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | #include<iostream> using namespace std; class TreeNode { public: int value; TreeNode *left, *right, *parent; }; TreeNode treeNodePool[1500000]; int treeNodePoolIndex; TreeNode* NewTreeNode() { int tmp = treeNodePoolIndex; treeNodePoolIndex++; treeNodePool[tmp].value = -1; treeNodePool[tmp].left = 0; treeNodePool[tmp].right = 0; treeNodePool[tmp].parent = 0; return &treeNodePool[tmp]; } class Tree { public: TreeNode * root; void Init() { root = 0; } void Insert(int value) { TreeNode *parent = 0, *finder = root, **p = 0; while (1) { if (finder == 0) { TreeNode *newNode = NewTreeNode(); newNode->value = value; if (root == 0) { root = newNode; } else { *p = newNode; newNode->parent = parent; } return; } parent = finder; if (value >= finder->value) { p = &parent->right; finder = finder->right; } else { p = &parent->left; finder = finder->left; } } } TreeNode* Find(int value) { TreeNode *finder = root; while (1) { if (finder == 0) return 0; if (finder->value == value) return finder; if (value > finder->value) finder = finder->right; else finder = finder->left; } } TreeNode* Erase(int value) { TreeNode *eraser = Find(value); if (eraser == 0) return 0; TreeNode *parent = eraser->parent, **p = 0; if (eraser != root) { if (parent->right == eraser) p = &parent->right; else if (parent->left == eraser) p = &parent->left; } if (eraser->left == 0 && eraser->right == 0) { //자식이 하나도 없을 경우 if (eraser == root) root = 0; else *p = 0; } else if (eraser->left != 0 && eraser->right == 0) { //자식이 왼쪽에만 있을 경우 if (eraser == root) { root = eraser->left; eraser->left->parent = 0; } else { *p = eraser->left; eraser->left->parent = parent; } } else if (eraser->left == 0 && eraser->right != 0) { //자식이 오른쪽에만 있을 경우 if (eraser == root) { root = eraser->right; eraser->right->parent = 0; } else { *p = eraser->right; eraser->right->parent = parent; } } else if (eraser->left != 0 && eraser->right != 0) { //자식이 양쪽에 있을 경우 TreeNode *finder = eraser->left; while (1) { if (finder->right == 0) break; finder = finder->right; } Erase(finder->value); if (eraser == root) { root = finder; finder->parent = 0; } else { *p = finder; finder->parent = parent; } finder->left = eraser->left; finder->right = eraser->right; if (eraser->left != 0) finder->left->parent = finder; if (eraser->right != 0) finder->right->parent = finder; } } }; Tree tree; void Init() { treeNodePoolIndex = 0; tree.Init(); } int main(void) { Init(); int N; cin >> N; int input; for (int i = 0; i < N; i++) { cin >> input; tree.Insert(input); } for (int i = 0; i < N; i++) { cin >> input; tree.Erase(input); } return 0; } /* 7 100 150 50 175 125 25 75 100 150 50 175 125 25 75 */ | cs |
'Algorithm & Data structure > 이론' 카테고리의 다른 글
트라이 알고리즘 (trie) (0) | 2021.03.17 |
---|---|
구간 길이 구하기 (0) | 2021.01.06 |
Heap (0) | 2020.09.06 |
LCA(Lowest Common Ancestor) (0) | 2020.09.06 |
MST(Minimum Spanning Tree) & Kruskal (0) | 2020.09.06 |
Comments