forked from JaeYeopHan/algorithm_basic_java
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
153 lines (120 loc) · 3.55 KB
/
BinaryTree.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
package datastructure.binaryTree;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertThat;
public class BinaryTree {
@Test
public void test() {
Node node1 = new Node();
Node node2 = new Node();
Node node3 = new Node();
Node node4 = new Node();
node1.data = 1;
node2.data = 2;
node3.data = 3;
node4.data = 4;
node1.left = node2;
node1.right = node3;
node2.left = node4;
assertThat(getMax(node1), is(4));
assertThat(isBalanced(node1), is(true));
int[] arr = {1,5,7,9,12,26,56,78};
assertThat(isBST(buildBST(arr)), is(true));
}
/*
TASK
바이너리 트리에서 최대값을 구한다.
*/
public int getMax(Node root) {
int result = Integer.MIN_VALUE;
if (root == null) {
return result;
}
return getMaxRec(root, result);
}
public int getMaxRec(Node head, int result) {
if (head == null) {
return result;
}
if (head.data > result) {
result = head.data;
}
result = getMaxRec(head.left, result);
result = getMaxRec(head.right, result);
return result;
}
/*
TASK
주어진 바이너리 트리가 균형 잡힌 트리인지 판별한다.
*/
public boolean isBalanced(Node root) {
return getNodeHeight(root) != -1;
}
private int getNodeHeight(Node node) {
if (node == null) {
return 0;
}
int leftHeight = getNodeHeight(node.left);
if (leftHeight == -1) {
return -1;
}
int rightHeight = getNodeHeight(node.right);
if (rightHeight == -1) {
return -1;
}
if (Math.abs(rightHeight - leftHeight) > 1) {
return -1;
}
return Math.max(leftHeight, rightHeight) + 1;
}
/*
TASK
오름차순으로 정렬된 배열을 Binary Search Tree로 변환한다.
트리의 높이를 최소화 하면서 (즉 complete binary tree로) 변환한다.
*/
public Node buildBST(int[] arr) {
return buildBSTRec(arr, 0, arr.length - 1);
}
private Node buildBSTRec(int[] arr, int start, int end) {
if (start > end) {
return null;
}
int middle = start + (end - start)/2;
Node leftNode = buildBSTRec(arr, start, middle - 1);
Node rightNode = buildBSTRec(arr, middle + 1, end);
return new Node(arr[middle], leftNode, rightNode);
}
/*
TASK
주어진 트리가 BST인지 확인한다.
Hint: 범위 탐색
*/
public boolean isBST(Node root) {
return isBSTRec(root, Integer.MIN_VALUE, Integer.MAX_VALUE);
}
private boolean isBSTRec(Node node, int min, int max) {
if (node == null) {
return true;
}
if (node.data <= min || node.data > max) {
return false;
}
boolean isBSTOfLeft = isBSTRec(node.left, min, node.data);
boolean isBSTOfRight = isBSTRec(node.right, node.data, max);
return isBSTOfLeft && isBSTOfRight;
}
public class Node {
int data;
Node left;
Node right;
public Node() {}
public Node(int data) {
this.data = data;
}
public Node(int data, Node left, Node right) {
this.data = data;
this.left = left;
this.right = right;
}
}
}