-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
binarySearchTree.java
195 lines (178 loc) ยท 4.15 KB
/
binarySearchTree.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
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
public class binarySearchTree {
public 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;
}
}
public Node root;
public binarySearchTree() {
this.root = null;
}
//ํ์ ์ฐ์ฐ
public boolean find(int id){
Node current = root;
while(current!=null){
//ํ์ฌ ๋
ธ๋์ ์ฐพ๋ ๊ฐ์ด ๊ฐ์ผ๋ฉด
if(current.getData()==id){
return true;
//์ฐพ๋ ๊ฐ์ด ํ์ฌ ๋
ธ๋๋ณด๋ค ์์ผ๋ฉด
} else if(current.getData()>id){
current = current.getLeft();
//์ฐพ๋ ๊ฐ์ด ํ์ฌ ๋
ธ๋๋ณด๋ค ํฌ๋ฉด
} else{
current = current.getRight();
}
}
return false;
}
//์ญ์ ์ฐ์ฐ
public boolean delete(int id){
Node parent = root;
Node current = root;
boolean isLeftChild = false;
while(current.getData()!=id){
parent = current;
if(current.getData()>id){
isLeftChild = true;
current = current.getLeft();
}else{
isLeftChild = false;
current = current.getRight();
}
if(current==null){
return false;
}
}
//Case 1: ์์๋
ธ๋๊ฐ ์๋ ๊ฒฝ์ฐ
if(current.getLeft()==null && current.getRight()==null){
if(current==root){
root = null;
}
if(isLeftChild==true){
parent.setLeft(null);
}else{
parent.setRight(null);
}
}
//Case 2 : ํ๋์ ์์์ ๊ฐ๋ ๊ฒฝ์ฐ
else if(current.getRight()==null){
if(current==root){
root = current.getLeft();
}else if(isLeftChild){
parent.setLeft(current.getLeft());
}else{
parent.setRight(current.getLeft());
}
} else if(current.getLeft()==null){
if(current==root){
root = current.getRight();
}else if(isLeftChild){
parent.setLeft(current.getRight());
}else{
parent.setRight(current.getRight());
}
}
//Case 3 : ๋๊ฐ์ ์์์ ๊ฐ๋ ๊ฒฝ์ฐ
else if(current.getLeft()!=null && current.getRight()!=null){
// ์ค๋ฅธ์ชฝ ์๋ธํธ๋ฆฌ์ ์ต์๊ฐ์ ์ฐพ์
Node successor = getSuccessor(current);
if(current==root){
root = successor;
}else if(isLeftChild){
parent.setLeft(successor);
}else{
parent.setRight(successor);
}
successor.setLeft(current.getLeft());
}
return true;
}
public Node getSuccessor(Node deleleNode){
Node successsor =null;
Node successsorParent =null;
Node current = deleleNode.getRight();
while(current!=null){
successsorParent = successsor;
successsor = current;
current = current.getLeft();
}
if(successsor!=deleleNode.getRight()){
successsorParent.setLeft(successsor.getRight());
successsor.setRight(deleleNode.getRight());
}
return successsor;
}
//์ฝ์
์ฐ์ฐ
public void insert(int id){
Node newNode = new Node(id);
if(root==null){
root = newNode;
return;
}
Node current = root;
Node parent = null;
while(true){
parent = current;
if(id < current.getData()){
current = current.getLeft();
if(current==null){
parent.setLeft(newNode);
return;
}
}else{
current = current.getRight();
if(current==null){
parent.setRight(newNode);
return;
}
}
}
}
public void display(Node root){
if(root!=null){
display(root.getLeft());
System.out.print(" " + root.getData());
display(root.getRight());
}
}
public static void main(String[] args) {
binarySearchTree b = new binarySearchTree();
//ํธ๋ฆฌ์ ๋
ธ๋๋ฅผ ์ฝ์
b.insert(3);b.insert(8);
b.insert(1);b.insert(4);b.insert(6);b.insert(2);b.insert(10);b.insert(9);
b.insert(20);b.insert(25);b.insert(15);b.insert(16);
System.out.println("ํธ๋ฆฌ์ฝ์
๊ฒฐ๊ณผ : ");
b.display(b.root);
System.out.println("");
System.out.println("์ด์งํธ๋ฆฌ์์ 4๋ฅผ ํ์ : " + b.find(4));
System.out.println("์ด์งํธ๋ฆฌ์์ 2๋ฅผ ์ญ์ : " + b.delete(2));
b.display(b.root);
System.out.println("\n์ด์งํธ๋ฆฌ์์ 4๋ฅผ ์ญ์ : " + b.delete(4));
b.display(b.root);
System.out.println("\n์ด์งํธ๋ฆฌ์์ 10์ ์ญ์ : " + b.delete(10));
b.display(b.root);
}
}