■ B 트리를 사용하는 방법을 보여준다.
▶ BTreeNode.cs
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// B 트리 노드 /// </summary> public class BTreeNode<T> { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Public #region Field /// <summary> /// 사용 키 카운트 /// </summary> public int KeyCountUsed = 0; /// <summary> /// 키 배열 /// </summary> public string[] KeyArray = new string[KEY_COUNT_PER_NODE]; /// <summary> /// 값 배열 /// </summary> public T[] ValueArray = new T[KEY_COUNT_PER_NODE]; /// <summary> /// 자식 배열 /// </summary> public BTreeNode<T>[] ChildArray = new BTreeNode<T>[CHILD_COUNT_PER_NODE]; #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 키 1/2 카운트 /// </summary> private const int KEY_HALF_COUNT = 2; /// <summary> /// 노드당 키 카운트 /// </summary> private const int KEY_COUNT_PER_NODE = 2 * KEY_HALF_COUNT; /// <summary> /// 노드당 자식 카운트 /// </summary> private const int CHILD_COUNT_PER_NODE = KEY_COUNT_PER_NODE + 1; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 추가하기 - AddItem(key, value, parentKey, parentValue, parentNode) /// <summary> /// 항목 추가하기 /// </summary> /// <param name="key">키</param> /// <param name="value">값</param> /// <param name="parentKey">부모 키</param> /// <param name="parentValue">부모 값</param> /// <param name="parentNode">부모 노드</param> public void AddItem(string key, T value, ref string parentKey, ref T parentValue, ref BTreeNode<T> parentNode) { int spot = 0; while(spot < KeyCountUsed) { if(string.Compare(KeyArray[spot], key) >= 0) { break; } spot++; } if((spot < KeyCountUsed) && (KeyArray[spot] == key)) { throw new Exception($"Insert error. Cannot insert item with duplicate key value '{key}'"); } if(ChildArray[0] == null) { AddItemToNode(spot, key, value, null, ref parentKey, ref parentValue, ref parentNode); } else { ChildArray[spot].AddItem(key, value, ref parentKey, ref parentValue, ref parentNode); if(parentNode != null) { AddItemToNode(spot, parentKey, parentValue, parentNode, ref parentKey, ref parentValue, ref parentNode); } } } #endregion #region 항목 제거하기 - RemoveItem(key) /// <summary> /// 항목 제거하기 /// </summary> /// <param name="key">키</param> public void RemoveItem(string key) { int spot = 0; while(spot < KeyCountUsed) { if(string.Compare(KeyArray[spot], key) >= 0) { break; } spot++; } if((spot < KeyCountUsed) && (KeyArray[spot] == key)) { if(ChildArray[0] == null) { RemoveItemFromNode(spot); } else { string rightMostKey = string.Empty; T rightMostValue = default(T); ChildArray[spot].SwapRightMost(key, ref rightMostKey, ref rightMostValue); KeyArray [spot] = rightMostKey; ValueArray[spot] = rightMostValue; ChildArray[spot].RemoveItem(key); } } else { if(ChildArray[0] == null) { throw new Exception($"Delete error. Cannot find item with key value '{key}'"); } ChildArray[spot].RemoveItem(key); } if(ChildArray[0] != null) { if(ChildArray[spot].KeyCountUsed < KEY_HALF_COUNT) { if((spot > 0) && (ChildArray[spot - 1].KeyCountUsed > KEY_HALF_COUNT)) { RebalanceSiblings(ChildArray[spot - 1], ChildArray[spot], ref KeyArray[spot - 1], ref ValueArray[spot - 1]); } else if((spot < KEY_HALF_COUNT - 1) && (ChildArray[spot + 1].KeyCountUsed > KEY_HALF_COUNT)) { RebalanceSiblings(ChildArray[spot], ChildArray[spot + 1], ref KeyArray[spot], ref ValueArray[spot]); } else { if(spot > 0) { MergeSiblings(spot - 1, spot, KeyArray[spot - 1], ValueArray[spot - 1]); } else { MergeSiblings(spot, spot + 1, KeyArray[spot], ValueArray[spot]); } } } } } #endregion #region 항목 찾기 - FindItem(key) /// <summary> /// 항목 찾기 /// </summary> /// <param name="key">키</param> /// <returns>항목</returns> public T FindItem(string key) { int spot = 0; while(spot < KeyCountUsed) { if(string.Compare(KeyArray[spot], key) >= 0) { break; } spot++; } if((spot < KeyCountUsed) && (KeyArray[spot] == key)) { return ValueArray[spot]; } if(ChildArray[spot] == null) { return default(T); } return ChildArray[spot].FindItem(key); } #endregion #region 리스트 박스에 추가하기 - AddToListBox(listBox, int indent) /// <summary> /// 리스트 박스에 추가하기 /// </summary> /// <param name="listBox">리스트 박스</param> /// <param name="indent"></param> public void AddToListBox(ListBox listBox, int indent) { for(int i = 0; i < KeyCountUsed; i++) { if(ChildArray[i] != null) { ChildArray[i].AddToListBox(listBox, indent + 4); } listBox.Items.Add(new string(' ', indent) + ValueArray[i].ToString()); } if(KeyCountUsed > 0) { if(ChildArray[KeyCountUsed] != null) { ChildArray[KeyCountUsed].AddToListBox(listBox, indent + 4); } } } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 공간을 갖는 노드에서 항목 추가하기 - AddItemInNodeWithRoom(spot, key, value, node) /// <summary> /// 공간을 갖는 노드에서 항목 추가하기 /// </summary> /// <param name="spot">스팟</param> /// <param name="key">키</param> /// <param name="value">값</param> /// <param name="node">노드</param> private void AddItemInNodeWithRoom(int spot, string key, T value, BTreeNode<T> node) { Array.Copy(KeyArray , spot , KeyArray , spot + 1, KeyCountUsed - spot); Array.Copy(ValueArray, spot , ValueArray, spot + 1, KeyCountUsed - spot); Array.Copy(ChildArray, spot + 1, ChildArray, spot + 2, KeyCountUsed - spot); KeyArray[spot] = key; ValueArray[spot] = value; ChildArray[spot + 1] = node; KeyCountUsed++; } #endregion #region 노드 분리하기 - SplitNode(spot, key, value, node, praentKey, parentValue, parentNode) /// <summary> /// 노드 분리하기 /// </summary> /// <param name="spot">스팟</param> /// <param name="key">키</param> /// <param name="value">값</param> /// <param name="node">노드</param> /// <param name="praentKey">부모 키</param> /// <param name="parentValue">부모 값</param> /// <param name="parentNode">부모 노드</param> private void SplitNode(int spot, string key, T value, BTreeNode<T> node, ref string praentKey, ref T parentValue, ref BTreeNode<T> parentNode) { string[] keyArray = new string[KEY_COUNT_PER_NODE + 1]; Array.Copy(KeyArray, 0, keyArray, 0, spot); keyArray[spot] = key; Array.Copy(KeyArray, spot, keyArray, spot + 1, KEY_COUNT_PER_NODE - spot); T[] valueArray = new T[KEY_COUNT_PER_NODE + 1]; Array.Copy(ValueArray, 0, valueArray, 0, spot); valueArray[spot] = value; Array.Copy(ValueArray, spot, valueArray, spot + 1, KEY_COUNT_PER_NODE - spot); BTreeNode<T>[] nodeArray = new BTreeNode<T>[CHILD_COUNT_PER_NODE + 1]; Array.Copy(ChildArray, 0, nodeArray, 0, spot + 1); nodeArray[spot + 1] = node; Array.Copy(ChildArray, spot + 1, nodeArray, spot + 2, KEY_COUNT_PER_NODE - spot); Array.Copy(keyArray , 0, KeyArray , 0, KEY_HALF_COUNT ); Array.Copy(valueArray, 0, ValueArray, 0, KEY_HALF_COUNT ); Array.Copy(nodeArray , 0, ChildArray, 0, KEY_HALF_COUNT + 1); Array.Clear(KeyArray , KEY_HALF_COUNT , KEY_HALF_COUNT); Array.Clear(ValueArray, KEY_HALF_COUNT , KEY_HALF_COUNT); Array.Clear(ChildArray, KEY_HALF_COUNT + 1, KEY_HALF_COUNT); KeyCountUsed = KEY_HALF_COUNT; praentKey = keyArray[KEY_HALF_COUNT]; parentValue = valueArray[KEY_HALF_COUNT]; parentNode = new BTreeNode<T>(); Array.Copy(keyArray , KEY_HALF_COUNT + 1, parentNode.KeyArray , 0, KEY_HALF_COUNT ); Array.Copy(valueArray, KEY_HALF_COUNT + 1, parentNode.ValueArray, 0, KEY_HALF_COUNT ); Array.Copy(nodeArray , KEY_HALF_COUNT + 1, parentNode.ChildArray, 0, KEY_HALF_COUNT + 1); parentNode.KeyCountUsed = KEY_HALF_COUNT; } #endregion #region 노드에 항목 추가하기 - AddItemToNode(spot, key, value, node, parentkey, parentValue, parentNode) /// <summary> /// 노드에 항목 추가하기 /// </summary> /// <param name="spot">스팟</param> /// <param name="key">키</param> /// <param name="value">값</param> /// <param name="node">노드</param> /// <param name="parentkey">부모 키</param> /// <param name="parentValue">부모 값</param> /// <param name="parentNode">부모 노드</param> private void AddItemToNode(int spot, string key, T value, BTreeNode<T> node, ref string parentkey, ref T parentValue, ref BTreeNode<T> parentNode) { if(KeyCountUsed < KEY_COUNT_PER_NODE) { AddItemInNodeWithRoom(spot, key, value, node); parentkey = null; parentValue = default(T); parentNode = null; } else { SplitNode(spot, key, value, node, ref parentkey, ref parentValue, ref parentNode); } } #endregion #region 노드에서 항목 제거하기 - RemoveItemFromNode(spot) /// <summary> /// 노드에서 항목 제거하기 /// </summary> /// <param name="spot">스팟</param> private void RemoveItemFromNode(int spot) { Array.Copy(KeyArray , spot + 1, KeyArray , spot , KeyCountUsed - spot - 1); Array.Copy(ValueArray, spot + 1, ValueArray, spot , KeyCountUsed - spot - 1); Array.Copy(ChildArray, spot + 2, ChildArray, spot + 1, KeyCountUsed - spot - 1); KeyCountUsed--; KeyArray [KeyCountUsed ] = null; ValueArray[KeyCountUsed ] = default(T); ChildArray[KeyCountUsed + 1] = null; } #endregion #region 가장 오른쪽 노드와 교환하기 - SwapRightMost(key, rightMostKey, rightMostValue) /// <summary> /// 가장 오른쪽 노드와 교환하기 /// </summary> /// <param name="key">키</param> /// <param name="rightMostKey">가장 오른쪽 키</param> /// <param name="rightMostValue">가장 오른쪽 값</param> private void SwapRightMost(string key, ref string rightMostKey, ref T rightMostValue) { if(ChildArray[0] == null) { rightMostKey = KeyArray [KeyCountUsed - 1]; rightMostValue = ValueArray[KeyCountUsed - 1]; KeyArray[KeyCountUsed - 1] = key; } else { ChildArray[KeyCountUsed].SwapRightMost(key, ref rightMostKey, ref rightMostValue); } } #endregion #region 이웃과 재조정하기 - RebalanceSiblings(leftNode, rightNode, middleKey, middleValue) /// <summary> /// 이웃과 재조정하기 /// </summary> /// <param name="leftNode">왼쪽 노드</param> /// <param name="rightNode">오른쪽 노드</param> /// <param name="middleKey">중간 키</param> /// <param name="middleValue">중간 값</param> private static void RebalanceSiblings(BTreeNode<T> leftNode, BTreeNode<T> rightNode, ref string middleKey, ref T middleValue) { int keyCount = leftNode.KeyCountUsed + rightNode.KeyCountUsed + 1; int middleIndex = leftNode.KeyCountUsed; string[] keyArray = new string[keyCount]; Array.Copy(leftNode.KeyArray, 0, keyArray, 0, leftNode.KeyCountUsed); keyArray[middleIndex] = middleKey; Array.Copy(rightNode.KeyArray, 0, keyArray, middleIndex + 1, rightNode.KeyCountUsed); T[] valueArray = new T[keyCount]; Array.Copy(leftNode.ValueArray, 0, valueArray, 0, leftNode.KeyCountUsed); valueArray[middleIndex] = middleValue; Array.Copy(rightNode.ValueArray, 0, valueArray, middleIndex + 1, rightNode.KeyCountUsed); BTreeNode<T>[] childArray = new BTreeNode<T>[keyCount + 1]; Array.Copy(leftNode.ChildArray , 0, childArray, 0 , leftNode.KeyCountUsed + 1); Array.Copy(rightNode.ChildArray, 0, childArray, middleIndex + 1, rightNode.KeyCountUsed + 1); int leftCount = (int)((keyCount - 1) / 2); Array.Copy(keyArray , 0, leftNode.KeyArray , 0, leftCount ); Array.Copy(valueArray, 0, leftNode.ValueArray, 0, leftCount ); Array.Copy(childArray, 0, leftNode.ChildArray, 0, leftCount + 1); Array.Clear(leftNode.KeyArray , leftCount , KEY_COUNT_PER_NODE - leftCount); Array.Clear(leftNode.ValueArray, leftCount , KEY_COUNT_PER_NODE - leftCount); Array.Clear(leftNode.ChildArray, leftCount + 1, KEY_COUNT_PER_NODE - leftCount); leftNode.KeyCountUsed = leftCount; middleKey = keyArray [leftCount]; middleValue = valueArray[leftCount]; int rightCount = keyCount - 1 - leftCount; Array.Copy(keyArray , leftCount + 1, rightNode.KeyArray , 0, rightCount ); Array.Copy(valueArray, leftCount + 1, rightNode.ValueArray, 0, rightCount ); Array.Copy(childArray, leftCount + 1, rightNode.ChildArray, 0, rightCount + 1); rightNode.KeyCountUsed = rightCount; } #endregion #region 이웃과 병합하기 - MergeSiblings(leftSpot, rightSpot, middleKey, middleValue) /// <summary> /// 이웃과 병합하기 /// </summary> /// <param name="leftSpot">왼쪽 스팟</param> /// <param name="rightSpot">오른쪽 스팟</param> /// <param name="middleKey">중간 키</param> /// <param name="middleValue">중간 값</param> private void MergeSiblings(int leftSpot, int rightSpot, string middleKey, T middleValue) { int middleIndex = ChildArray[leftSpot].KeyCountUsed; ChildArray[leftSpot].KeyArray [middleIndex] = middleKey; ChildArray[leftSpot].ValueArray[middleIndex] = middleValue; Array.Copy(ChildArray[rightSpot].KeyArray , 0, ChildArray[leftSpot].KeyArray , middleIndex + 1, ChildArray[rightSpot].KeyCountUsed ); Array.Copy(ChildArray[rightSpot].ValueArray, 0, ChildArray[leftSpot].ValueArray, middleIndex + 1, ChildArray[rightSpot].KeyCountUsed ); Array.Copy(ChildArray[rightSpot].ChildArray, 0, ChildArray[leftSpot].ChildArray, middleIndex + 1, ChildArray[rightSpot].KeyCountUsed + 1); ChildArray[leftSpot].KeyCountUsed += ChildArray[rightSpot].KeyCountUsed + 1; Array.Copy(KeyArray , leftSpot + 1, KeyArray , leftSpot , KeyCountUsed - leftSpot - 1); Array.Copy(ValueArray, leftSpot + 1, ValueArray, leftSpot , KeyCountUsed - leftSpot - 1); Array.Copy(ChildArray, rightSpot + 1, ChildArray, rightSpot, KeyCountUsed - rightSpot ); KeyCountUsed--; KeyArray [KeyCountUsed ] = null; ValueArray[KeyCountUsed ] = default(T); ChildArray[KeyCountUsed + 1] = null; } #endregion } } |
▶ BTree.cs
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 |
using System.Windows.Forms; namespace TestProject { /// <summary> /// B 트리 /// </summary> /// <typeparam name="T"></typeparam> public class BTree<T> { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 루트 노드 /// </summary> private BTreeNode<T> rootNode = new BTreeNode<T>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 항목 추가하기 - AddItem(key, value) /// <summary> /// 항목 추가하기 /// </summary> /// <param name="key">키</param> /// <param name="value">값</param> public void AddItem(string key, T value) { string parentKey = string.Empty; T parentValue = default(T); BTreeNode<T> parentNode = null; this.rootNode.AddItem(key, value, ref parentKey, ref parentValue, ref parentNode); if(parentNode != null) { BTreeNode<T> rootNode = new BTreeNode<T>(); rootNode.KeyArray [0] = parentKey; rootNode.ValueArray[0] = parentValue; rootNode.ChildArray[0] = this.rootNode; rootNode.ChildArray[1] = parentNode; rootNode.KeyCountUsed = 1; this.rootNode = rootNode; } } #endregion #region 항목 제거하기 - RemoveItem(key) /// <summary> /// 항목 제거하기 /// </summary> /// <param name="key">키</param> public void RemoveItem(string key) { this.rootNode.RemoveItem(key); if(this.rootNode.KeyCountUsed < 1) { if(this.rootNode.ChildArray[0] != null) { this.rootNode = this.rootNode.ChildArray[0]; } } } #endregion #region 항목 찾기 - FindItem(key) /// <summary> /// 항목 찾기 /// </summary> /// <param name="key">키</param> /// <returns>항목</returns> public T FindItem(string key) { return this.rootNode.FindItem(key); } #endregion #region 리스트 박스에 추가하기 - AddToListBox(listBox) /// <summary> /// 리스트 박스에 추가하기 /// </summary> /// <param name="listBox">리스트 박스</param> public void AddToListBox(ListBox listBox) { this.rootNode.AddToListBox(listBox, 0); } #endregion #region 문자열 구하기 - ToString() /// <summary> /// 문자열 구하기 /// </summary> /// <returns>문자열</returns> public override string ToString() { return this.rootNode.ToString(); } #endregion } } |
▶ MainForm.cs
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
using Microsoft.VisualBasic; using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 /// </summary> public partial class MainForm : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// B 트리 /// </summary> private BTree<Person> bTree = new BTree<Person>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm() /// <summary> /// 생성자 /// </summary> public MainForm() { InitializeComponent(); this.addButton.Click += addButton_Click; this.removeButtton.Click += removeButton_Click; this.addRandomButton.Click += addRandomButton_Click; this.findButton.Click += findButton_Click; this.listBox.SelectedIndexChanged += listBox_SelectedIndexChanged; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 추가 버튼 클릭시 처리하기 - addButton_Click(sender, e) /// <summary> /// 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addButton_Click(object sender, EventArgs e) { try { Person person = new Person(this.ssnTextBox.Text, this.firstNameTextBox.Text, this.lastNameTextBox.Text); if(person.FirstName.Length == 0) { person.FirstName = "first" + person.SSN; } if(person.LastName.Length == 0) { person.LastName = "last" + person.SSN; } this.bTree.AddItem(person.SSN, person); this.listBox.Items.Clear(); this.bTree.AddToListBox(this.listBox); } catch(Exception exception) { MessageBox.Show ( exception.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation ); } this.ssnTextBox.Clear(); this.firstNameTextBox.Clear(); this.lastNameTextBox.Clear(); this.ssnTextBox.Focus(); } #endregion #region 제거 버튼 클릭시 처리하기 - removeButton_Click(sender, e) /// <summary> /// 제거 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void removeButton_Click(object sender, EventArgs e) { try { this.bTree.RemoveItem(this.ssnTextBox.Text); } catch(Exception exception) { MessageBox.Show ( exception.Message, "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Exclamation ); } this.ssnTextBox.Clear(); this.listBox.Items.Clear(); this.bTree.AddToListBox(this.listBox); this.ssnTextBox.Focus(); } #endregion #region 랜덤 추가 버튼 클릭시 처리하기 - addRandomButton_Click(sender, e) /// <summary> /// 랜덤 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addRandomButton_Click(object sender, EventArgs e) { string count = Interaction.InputBox("사람 수를 입력해 주시기 바랍니다.", "CONFIRMATION", "", -1, -1); if(count.Length == 0) { return; } for(int i = 1; i <= int.Parse(count); i++) { Person person = new Person(); this.bTree.AddItem(person.SSN, person); } this.listBox.Items.Clear(); this.bTree.AddToListBox(this.listBox); this.ssnTextBox.Focus(); } #endregion #region 찾기 버튼 클릭시 처리하기 - findButton_Click(sender, e) /// <summary> /// 찾기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void findButton_Click(object sender, EventArgs e) { Person person = this.bTree.FindItem(this.ssnTextBox.Text); if(person == null) { MessageBox.Show ( "트리에 이 사람이 없습니다.", "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information ); } else { MessageBox.Show ( person.SSN + "\n" + person.FirstName + "\n" + person.LastName, "INFORMATION", MessageBoxButtons.OK, MessageBoxIcon.Information ); } this.ssnTextBox.Focus(); } #endregion #region 리스트 박스 선택 인덱스 변경시 처리하기 - listBox_SelectedIndexChanged(sender, e) /// <summary> /// 리스트 박스 선택 인덱스 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void listBox_SelectedIndexChanged(object sender, EventArgs e) { this.ssnTextBox.Clear(); this.firstNameTextBox.Clear(); this.lastNameTextBox.Clear(); if(this.listBox.SelectedIndex < 0) { return; } string text = this.listBox.SelectedItem.ToString().Trim(); this.ssnTextBox.Text = text.Split(new char[]{':'})[0].Trim(); } #endregion } } |