Skip to content

Commit 57a4ac6

Browse files
authored
Merge pull request #39 from Data-Structure-Study/yoonexample
기수 정렬 오류 수정 및 보간 탐색 구현, 이진 탐색 트리 구현
2 parents 8a80063 + 82da0eb commit 57a4ac6

File tree

5 files changed

+178
-1
lines changed

5 files changed

+178
-1
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package search;
2+
3+
public class InterpolSearch {
4+
5+
public static void main(String[] args) {
6+
int[] arr = {1, 3, 5, 7, 9};
7+
8+
int idx = iSearch(arr, 7);
9+
printSearchInfo(idx);
10+
11+
idx = iSearch(arr, 4);
12+
printSearchInfo(idx);
13+
14+
idx = iSearch(arr, 2);
15+
printSearchInfo(idx);
16+
}
17+
18+
public static int iSearch(int[] arr, int target) {
19+
int first = 0; // 탐색 대상의 시작 인덱스 값
20+
int last = arr.length - 1; // 탐색 대상의 마지막 인덱스 값
21+
int mid;
22+
23+
while (!(arr[first] > target || arr[last] < target)) {
24+
mid = (int) ((double) (target - arr[first]) / (arr[last] - arr[first]) * (last - first))
25+
+ first;
26+
27+
if (arr[mid] == target) {
28+
return mid;
29+
} else if (target < arr[mid]) { // 탐색 대상이 아니라면 반으로 줄입니다.
30+
last = mid - 1; // 왜 mid에서 1을 뺄까요?
31+
} else {
32+
first = mid + 1; // 왜 mid에서 1을 더할까요?
33+
}
34+
}
35+
return -1;
36+
}
37+
38+
public static void printSearchInfo(int idx) {
39+
String info = "탐색 실패";
40+
if (idx != -1) {
41+
info = "타겟 저장 인덱스: " + idx;
42+
}
43+
System.out.println(info);
44+
}
45+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package search.binarysearchtree;
2+
3+
import tree.binarytree.BinaryTreeNode;
4+
5+
/**
6+
* 이진 탐색 트리 자료구조의 ADT, 인터페이스이자 Node의 인터페이스
7+
*
8+
* @param <E> 데이터의 파라미터 타입
9+
* @author dion
10+
*/
11+
public interface BinarySearchTreeNode<E> {
12+
13+
/**
14+
* 해당 데이터를 가지고 있는 노드를 찾습니다.
15+
*
16+
* @param target 찾으려고 하는 데이터
17+
* @return 해당 데이터를 가지고 있는 노드
18+
*/
19+
BinaryTreeNode<E> search(E target);
20+
21+
/**
22+
* 해당 데이터를 BST에 추가합니다.
23+
*
24+
* @param data BST에 저장할 데이터
25+
*/
26+
void insert(E data);
27+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package search.binarysearchtree;
2+
3+
import java.util.Comparator;
4+
import tree.binarytree.BinaryTreeNode;
5+
import tree.binarytree.LinkedBinaryTreeNode;
6+
7+
public class LinkedBinarySearchTreeNode<E> implements BinarySearchTreeNode<E> {
8+
9+
private final BinaryTreeNode<E> node;
10+
private final Comparator<E> comparator;
11+
12+
public LinkedBinarySearchTreeNode(E data, Comparator<E> comparator) {
13+
this.node = new LinkedBinaryTreeNode<>(data);
14+
this.comparator = comparator;
15+
}
16+
17+
@Override
18+
public BinaryTreeNode<E> search(E target) {
19+
BinaryTreeNode<E> curNode = this.node;
20+
21+
while (curNode != null) {
22+
E curNodeData = curNode.getData();
23+
if (curNodeData.equals(target)) {
24+
return curNode;
25+
} else if (comparator.compare(curNodeData, target) > 0) {
26+
curNode = curNode.getLeftSubTree();
27+
} else {
28+
curNode = curNode.getRightSubTree();
29+
}
30+
}
31+
32+
return null;
33+
}
34+
35+
@Override
36+
public void insert(E data) {
37+
if (data == null) {
38+
return;
39+
}
40+
41+
BinaryTreeNode<E> curNode = this.node;
42+
BinaryTreeNode<E> parentNode = null;
43+
44+
while (curNode != null) { // 저장할 위치를 찾는다.
45+
E curNodeData = curNode.getData();
46+
if (data == curNodeData) { // 중복저장 허용 안함
47+
return;
48+
}
49+
50+
parentNode = curNode;
51+
if (comparator.compare(curNodeData, data) > 0) {
52+
curNode = curNode.getLeftSubTree();
53+
} else {
54+
curNode = curNode.getRightSubTree();
55+
}
56+
}
57+
58+
BinaryTreeNode<E> newNode = new LinkedBinaryTreeNode<>(data);
59+
if (comparator.compare(parentNode.getData(), data) > 0) { // 크기를 비교해서 원하는 위치에 저장한다.
60+
parentNode.setLeftSubTree(newNode);
61+
} else {
62+
parentNode.setRightSubTree(newNode);
63+
}
64+
}
65+
}

yoonexample/src/main/java/sort/RadixSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@ public void sort(int[] array, int maxLength) {
2828
}
2929

3030
// 버킷 수 만큼 반복
31+
int arrayIndex = 0;
3132
for (int j = 0; j < BUCKET_NUM; j++) {
3233
// 버킷에 저장되어 있는 것을 순서대로 꺼내서 다시 array에 저장
33-
int arrayIndex = 0;
3434
while (!buckets[j].isEmpty()) {
3535
array[arrayIndex++] = buckets[j].dequeue();
3636
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package search.binarysearchtree;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.util.Comparator;
6+
import org.junit.jupiter.api.DisplayName;
7+
import org.junit.jupiter.api.Test;
8+
import tree.binarytree.BinaryTreeNode;
9+
10+
class BinarySearchTreeNodeTest {
11+
12+
@Test
13+
@DisplayName("이진_탐색_트리_테스트")
14+
void 이진_탐색_트리_테스트() {
15+
BinarySearchTreeNode<Integer> bstRoot = new LinkedBinarySearchTreeNode<>(5, (Comparator
16+
.comparingInt(o -> o)));
17+
BinaryTreeNode<Integer> searchNode;
18+
19+
bstRoot.insert(1);
20+
bstRoot.insert(6);
21+
bstRoot.insert(2);
22+
bstRoot.insert(8);
23+
bstRoot.insert(3);
24+
bstRoot.insert(9);
25+
26+
searchNode = bstRoot.search(1);
27+
assertThat(searchNode).isNotNull();
28+
assertThat(searchNode.getData()).isEqualTo(1);
29+
30+
searchNode = bstRoot.search(4);
31+
assertThat(searchNode).isNull();
32+
33+
searchNode = bstRoot.search(6);
34+
assertThat(searchNode).isNotNull();
35+
assertThat(searchNode.getData()).isEqualTo(6);
36+
37+
searchNode = bstRoot.search(7);
38+
assertThat(searchNode).isNull();
39+
}
40+
}

0 commit comments

Comments
 (0)