|
| 1 | +package datastructure.binaryTree; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import static org.hamcrest.CoreMatchers.is; |
| 6 | +import static org.junit.Assert.assertThat; |
| 7 | + |
| 8 | +public class BinaryTree { |
| 9 | + |
| 10 | + @Test |
| 11 | + public void test() { |
| 12 | + Node node1 = new Node(); |
| 13 | + Node node2 = new Node(); |
| 14 | + Node node3 = new Node(); |
| 15 | + Node node4 = new Node(); |
| 16 | + node1.data = 1; |
| 17 | + node2.data = 2; |
| 18 | + node3.data = 3; |
| 19 | + node4.data = 4; |
| 20 | + node1.left = node2; |
| 21 | + node1.right = node3; |
| 22 | + node2.left = node4; |
| 23 | + |
| 24 | + assertThat(getMax(node1), is(4)); |
| 25 | + } |
| 26 | + |
| 27 | + |
| 28 | + /* |
| 29 | + TASK |
| 30 | + 바이너리 트리에서 최대값을 구한다. |
| 31 | + */ |
| 32 | + |
| 33 | + public int getMax(Node root) { |
| 34 | + int result = Integer.MAX_VALUE; |
| 35 | + if (root == null) { |
| 36 | + return result; |
| 37 | + } |
| 38 | + return getMaxRec(root, result); |
| 39 | + } |
| 40 | + |
| 41 | + public int getMaxRec(Node head, int result) { |
| 42 | + if (head == null) { |
| 43 | + return result; |
| 44 | + } |
| 45 | + if (head.data > result) { |
| 46 | + result = head.data; |
| 47 | + } |
| 48 | + result = getMaxRec(head.left, result); |
| 49 | + result = getMaxRec(head.right, result); |
| 50 | + return result; |
| 51 | + } |
| 52 | + |
| 53 | + |
| 54 | + /* |
| 55 | + TASK |
| 56 | + 주어진 바이너리 트리가 균형 잡힌 트리인지 판별한다. |
| 57 | + */ |
| 58 | + |
| 59 | + public boolean isBalanced(Node root) { |
| 60 | + return getNodeHeight(root) != -1; |
| 61 | + } |
| 62 | + |
| 63 | + private int getNodeHeight(Node node) { |
| 64 | + if (node == null) { |
| 65 | + return 0; |
| 66 | + } |
| 67 | + int leftHeight = getNodeHeight(node.left); |
| 68 | + if (leftHeight == -1) { |
| 69 | + return -1; |
| 70 | + } |
| 71 | + |
| 72 | + int rightHeight = getNodeHeight(node.right); |
| 73 | + if (rightHeight == -1) { |
| 74 | + return -1; |
| 75 | + } |
| 76 | + |
| 77 | + if (Math.abs(rightHeight - leftHeight) > 1) { |
| 78 | + return -1; |
| 79 | + } |
| 80 | + |
| 81 | + return Math.max(leftHeight, rightHeight) + 1; |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | + /* |
| 86 | + TASK |
| 87 | + 오름차순으로 정렬된 배열을 Binary Search Tree로 변환한다. |
| 88 | + 트리의 높이를 최소화 하면서 (즉 complete binary tree로) 변환한다. |
| 89 | + */ |
| 90 | + |
| 91 | + public Node buildBST(int[] arr) { |
| 92 | + return buildBSTRec(arr, 0, arr.length - 1); |
| 93 | + } |
| 94 | + |
| 95 | + private Node buildBSTRec(int[] arr, int start, int end) { |
| 96 | + if (start > end) { |
| 97 | + return null; |
| 98 | + } |
| 99 | + int middle = start + (end - start)/2; |
| 100 | + Node leftNode = buildBSTRec(arr, start, middle - 1); |
| 101 | + Node rightNode = buildBSTRec(arr, middle + 1, end); |
| 102 | + |
| 103 | + return new Node(arr[middle], leftNode, rightNode); |
| 104 | + } |
| 105 | + |
| 106 | + |
| 107 | + /* |
| 108 | + TASK |
| 109 | + 주어진 트리가 BST인지 확인한다. |
| 110 | + Hint: 범위 탐색 |
| 111 | + */ |
| 112 | + |
| 113 | + public boolean isBST(Node root) { |
| 114 | + return isBSTRec(root, Integer.MIN_VALUE, Integer.MAX_VALUE); |
| 115 | + } |
| 116 | + |
| 117 | + private boolean isBSTRec(Node node, int min, int max) { |
| 118 | + if (node == null) { |
| 119 | + return true; |
| 120 | + } |
| 121 | + |
| 122 | + if (node.data <= min || node.data > max) { |
| 123 | + return false; |
| 124 | + } |
| 125 | + |
| 126 | + boolean isBSTOfLeft = isBSTRec(node.left, min, node.data); |
| 127 | + boolean isBSTOfRight = isBSTRec(node.right, node.data, max); |
| 128 | + |
| 129 | + return isBSTOfLeft && isBSTOfRight; |
| 130 | + } |
| 131 | + |
| 132 | + |
| 133 | + public class Node { |
| 134 | + int data; |
| 135 | + Node left; |
| 136 | + Node right; |
| 137 | + |
| 138 | + public Node() {} |
| 139 | + public Node(int data) { |
| 140 | + this.data = data; |
| 141 | + } |
| 142 | + |
| 143 | + public Node(int data, Node left, Node right) { |
| 144 | + this.data = data; |
| 145 | + this.left = left; |
| 146 | + this.right = right; |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments