-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.cpp
More file actions
100 lines (94 loc) · 2.88 KB
/
BinaryTree.cpp
File metadata and controls
100 lines (94 loc) · 2.88 KB
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
#include "BinaryTree.h"
#include <iostream>
using namespace std;
BinaryTree::BinaryTree(int _data, Node* rootNode)
{
rootNode->data = _data;
cout << "루트 노드의 값:" << rootNode->data << endl;
}
BinaryTree::~BinaryTree()
{
cout << "소멸자" << endl;
}
void BinaryTree::AddNode(int _data, Node* rootNode)
{
Node* newNode = new Node;
Node* tmpRoot = rootNode;
Node* Root = tmpRoot;
while (tmpRoot != nullptr) {
Root = tmpRoot;
if (_data <= tmpRoot->data) {
tmpRoot = tmpRoot->left;
}
else {
tmpRoot = tmpRoot->right;
}
}
if (_data <= Root->data) {
Root->left = newNode;
newNode->data = _data;
}
else {
Root->right = newNode;
newNode->data = _data;
}
cout << "추가된 값:" << newNode->data << endl;
}
void BinaryTree::SearchNode(int _data, Node* rootNode)
{
int numDown = 0;
Node* currentNode = rootNode;
while (currentNode != nullptr) {
if (_data == currentNode->data){
cout << "입력된 값 "<<_data<<" 은(는) 존재하는 값이고 루트 노드로부터 찾는데 " << numDown << "회 걸렸습니다." << endl;
break;
}
else if (_data < currentNode->data)
currentNode = currentNode->left;
else
currentNode = currentNode->right;
numDown++;
}
if (currentNode == nullptr)
cout << "입력된 값 "<<_data<<" 은(는) 없는 값입니다." << endl;
return;
}
void BinaryTree::DeleteNode(int _data, Node* rootNode)
{
Node* currentNode = rootNode; // 삭제할 노드
Node* currentRoot = currentNode; // 삭제할 노드의 윗 노드
while (currentNode->data != _data) { // 삭제할 노드의 값이 입력된 값이 될 때까지 while
currentRoot = currentNode;
if (_data < currentNode->data)
currentNode = currentNode->left;
else
currentNode = currentNode->right;
}
if (currentNode->left == nullptr && currentNode->right == nullptr) { // 삭제할 노드의 좌,우가 null일 때
if (currentNode->data < currentRoot->data) // 삭제할 노드가 윗 노드의 좌측인지, 우측인지 판별
currentRoot->left = nullptr;
else
currentRoot->right = nullptr;
}
else if (currentNode->left == nullptr && currentNode->right != nullptr) { // 삭제할 노드의 좌 노드만 null일 때
if (currentNode->data < currentRoot->data) // 삭제할 노드가 윗 노드의 좌측인지, 우측인지 판별
currentRoot->left = currentNode->right;
else
currentRoot->right = currentNode->right;
}
else if (currentNode->right == nullptr && currentNode->left != nullptr) { // 삭제할 노드의 우 노드만 null일 때
if (currentNode->data < currentRoot->data) // 삭제할 노드가 윗 노드의 좌측인지, 우측인지 판별
currentRoot->left = currentNode->left;
else
currentRoot->right = currentNode->left;
}
else {
Node* minNode = currentNode->right;
while (minNode->left != nullptr) { // 삭제할 노드의 우측에서 가장 작은노드 찾기(좌측으로 내려가기)
minNode = minNode->left;
}
currentNode->data = minNode->data;
DeleteNode(minNode->data, currentNode->right);
}
return;
}