-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbinary_tree.java
More file actions
116 lines (113 loc) · 4.21 KB
/
binary_tree.java
File metadata and controls
116 lines (113 loc) · 4.21 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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// 이진 탐색트리 구현
public class binary_tree {
Integer Data;
binary_tree left; // 좌측 서브트리
binary_tree right; // 우측 서브트리
public binary_tree(){ // 생성자
this.Data = null;
this.left = null;
this.right = null;
}
// 탐색 트리 구현
public void search(binary_tree tree, int Data){
// Data가 없을 때
if(tree.Data == null){
System.out.println("데이터가 없습니다.");
} // 해당 되는 경로가 있을 때
else if(tree.Data == Data){
System.out.println(tree.Data + "가 경로에 있습니다.");
} // 찾으려는 경로보다 작은 경우
else if(tree.Data < Data){
tree.search(tree.right,Data);
} // 찾으려는 경로보다 큰 경우
else {
tree.search(tree.left,Data);
}
}// 탐색 트리 삽입
public void Insert(binary_tree tree, int treevalue){
if(tree.Data == null){ // 해당트리에 데이터가 없으면 삽입 ( rootData)
tree.Data = treevalue;
}
else if(treevalue == tree.Data){
System.out.println("이미 데이터가 있습니다.");
} // Data가 삽입하려는 위치보다 클때
else if(treevalue > tree.Data){
// tree가 존재안할 경우
if(tree.right == null){
tree.right = new binary_tree();
tree.right.Data = treevalue;
}
// 이미 트리가 존재할 경우
else{
tree.right.Insert(tree.right,treevalue);
}
} // Data가 삽입하려는 위치보다 작을때
else{
// 해당 트리가 존재 하지 않을 경우
if(tree.left == null){
tree.left = new binary_tree();
tree.left.Data = treevalue;
}// 이미 트리가 존재하는 경우
else{
tree.left.Insert(tree.left,treevalue);
}
}
}
// 탐색 트리 삭제
public void Delete(binary_tree tree,int treevalue){
// 삭제할 자료가 없을 때
if(tree.Data == null){
System.out.println("삭제할 자료가 없습니다.");
}else if(tree.Data > treevalue){ // 값이 작은 경우
tree.Delete(tree.left,treevalue);
}else if(tree.Data < treevalue){ // 값이 큰 경우
tree.Delete(tree.right,treevalue);
}else{ // 삭제할 값을 찾았을 때
if(tree.left == null && tree.right == null){
tree = null;
} // 자식 노드가 없는 경우
//자식 노드 하나인 경우
else if(tree.left == null){
tree = tree.right;
}else if(tree.right == null){
tree = tree.left;
}else{
tree.Double_Delete(tree,tree.right,treevalue);
}
}
}
public void Double_Delete(binary_tree pretree,binary_tree tree,int treevalue){
/* 추가 구축 예정 */
if(tree.right== null){
}else{
pretree = tree.right;
}
}
public static void main(String[] args){
binary_tree Binary = new binary_tree();
while(true) {
java.util.Scanner scanner = new java.util.Scanner(System.in);
int Choice;
System.out.print("트리 기능 ( 1 - 탐색 ) / ( 2 - 삽입 ) / ( 3 - 삭제 ) :");
Choice = scanner.nextInt();
int Data;
switch(Choice){
case 1:
System.out.print("탐색 할 값을 넣어주세요 : ");
Data = scanner.nextInt();
Binary.search(Binary,Data);
break;
case 2:
System.out.print("삽입 할 값을 넣어주세요 : ");
Data = scanner.nextInt();
Binary.Insert(Binary,Data);
break;
case 3:
System.out.print("삭제 할 값을 넣어주세요 : ");
Data = scanner.nextInt();
Binary.Delete(Binary,Data);
break;
}
}
}
}