-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBST.java
151 lines (135 loc) · 4.18 KB
/
BST.java
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
package bst.basic;
public class BST {
public Node root;
public BST(){
this.root = null;
}
// 탐색 연산
public boolean find(int key){
Node cur = root;
while(cur != null){
if(cur.getData()==key){
return true; // 해당 key가 이미 존재하면 true
}else if(cur.getData()>key){
cur = cur.getLeft();
}else{
cur = cur.getRight();
}
}
return false;
}
// 삭제 연산
public boolean delete(int key){
Node pa = root;
Node cur = root;
boolean isLeft = false;
while(cur.getData() != key){
pa = cur;
if(cur.getData() >key){
isLeft = true;
cur = cur.getLeft();
}else{
isLeft = false;
cur = cur.getRight();
}
if(cur==null) return false;
}
// 1. 삭제할 노드가 단말 노드인 경우
if(cur.getLeft()==null && cur.getRight()==null){
if(cur==root) root = null;
if(isLeft == true){
pa.setLeft(null);
}else{
pa.setRight(null);
}
}
// 2. 삭제할 노드의 한 개의 자식노드를 가진 경우 (차수가 1인 경우)
else if(cur.getRight() == null || cur.getLeft() ==null){
// true : 오른쪽, false : 왼쪽
boolean flag = cur.getRight() == null ? true : false;
if(cur==root){
root = flag ? cur.getLeft() : cur.getRight();
}else if(isLeft){
if(flag) pa.setLeft(cur.getLeft());
else pa.setLeft(cur.getRight());
}else{
if(flag) pa.setRight(cur.getLeft());
else pa.setRight(cur.getRight());
}
}
// 3. 삭제할 노드가 두 개의 자식노드를 가진 경우 (차수가 2인 경우)
else if(cur.getRight() != null && cur.getLeft() !=null){
Node successor = getSuccessor(cur);
if(cur == root){
root = successor;
}else if(isLeft){
pa.setLeft(successor);
}else {
pa.setRight(successor);
}
successor.setLeft(cur.getLeft());
}
return true;
}
public void insert(int key){
Node insertNode = new Node(key);
if(root == null){
root = insertNode;
return;
}
Node cur = root, pa =null;
while(true){
pa = cur;
if(cur.getData() == key) return; // 이미 데이터가 존재
if(cur.getData() > key){
cur = cur.getLeft();
if(cur==null){
pa.setLeft(insertNode);
return;
}
}else{
cur = cur.getRight();
if(cur==null){
pa.setRight(insertNode);
return;
}
}
}
}
public void print(Node root){
if(root != null){
print(root.getLeft());
System.out.print(root.getData()+" ");
print(root.getRight());
}
}
private Node getSuccessor(Node deleteNode) {
Node insertNode = null, pa =null;
Node cur = deleteNode.getRight();
while(cur!=null){
pa = insertNode;
insertNode = cur;
cur = cur.getLeft();
}
if(insertNode != deleteNode.getRight()){
pa.setLeft(insertNode.getRight());
pa.setRight(deleteNode.getRight());
}
return insertNode;
}
}
class Node{
private int data;
private Node left;
private Node right;
public Node(int data){
this.setData(data);
setLeft(null); setRight(null);
}
public int getData() { return data; }
public void setData(int data) { this.data = data; }
public Node getLeft() { return left; }
public void setLeft(Node left) { this.left = left; }
public Node getRight() { return right; }
public void setRight(Node right) { this.right = right; }
}