Skip to content

Commit

Permalink
live-study: binary-tree
Browse files Browse the repository at this point in the history
  • Loading branch information
soongjamm committed Jan 1, 2021
1 parent 4eea213 commit f02dd27
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 2 deletions.
51 changes: 51 additions & 0 deletions src/live_study/src/main/java/week5/BinaryTree.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package week5;

import java.util.LinkedList;
import java.util.Objects;
import java.util.Queue;

public class BinaryTree {

static Queue<Node> q;

public static String bfs(Node root) {
q = new LinkedList<>();
String result = "";
LinkedList<Node> visited = new LinkedList<>();
q.add(root);

while (!q.isEmpty()) {
Node node = q.poll();
if(visited.contains(node)) {
continue;
}
result += node.getValue();
if (!Objects.isNull(node.getLeft())) {
q.add(node.getLeft());
}
if (!Objects.isNull(node.getRight())) {
q.add(node.getRight());
}
}

return result;
}

public static String dfs(Node root) {
if (Objects.isNull(root)) {
throw new NullPointerException("root가 null입니다.");
}
return dfs(root, "");
}

private static String dfs(Node root, String result) {
if (Objects.isNull(root)) {
return result;
}
result = dfs(root.getLeft(), result);
result += root.getValue();
result = dfs(root.getRight(), result);

return result;
}
}
30 changes: 30 additions & 0 deletions src/live_study/src/main/java/week5/Node.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package week5;

public class Node {
private int value;
private Node left;
private Node right;

public Node(int value, Node left, Node right) {
this.value = value;
this.left = left;
this.right = right;
}

public Node(int value) {
this.value = value;
}

public int getValue() {
return value;
}

public Node getLeft() {
return left;
}

public Node getRight() {
return right;
}

}
40 changes: 40 additions & 0 deletions src/live_study/src/test/java/week5/BinaryTreeTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package week5;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.util.Objects;

import static org.junit.jupiter.api.Assertions.*;

class BinaryTreeTest {

Node root;

@BeforeEach
void initilize() {
Node node1 = new Node(1);
Node node2 = new Node(2);
Node node5 = new Node(5, node1, node2);
Node node3 = new Node(3);
Node node4 = new Node(4);
Node node6 = new Node(6, node3, node4);
Node node7 = new Node(7, node5, node6);
Node node8 = new Node(8);
root = new Node(9, node7, node8);
}

@Test
void bfs() {
assertTrue(!Objects.isNull(root));
assertEquals("978561234", BinaryTree.bfs(root));

}

@Test
void dfs() {
assertTrue(!Objects.isNull(root));
assertEquals("152736498", BinaryTree.dfs(root));
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
> 정리를 하고도 왜 어댑터 패턴을 사용하는지 와닿지 않았는데, 다른 예제를 찾아보고 좀 더 와닿았다.
> https://jusungpark.tistory.com/22
# Adapter Pattern
> `어댑터 패턴은 합성, 즉 객체를 속성으로 만들어서 참조하는 디자인 패턴이다.`
> `호출당하는 쪽의 메서드를 호출하는 쪽의 코드에 대응하도록 중간에 변환기를 통해 호출하는 패턴`
Expand All @@ -12,7 +15,7 @@
>
> JDBC와 JRE가 어댑터의 역할을 하는 것
객체지향의 5원칙 SOLID중 개방 폐쇄 원칙(Open Closed Principle)을 활용한 설계 패턴이라 할 수 있다.
객체지향의 5원칙 SOLID중 `개방 폐쇄 원칙(Open Closed Principle)`을 활용한 설계 패턴이라 할 수 있다.

## 어댑터 패턴을 사용하면 나타나는 차이 (결과)
- 다음 예제는 어댑터 패턴을 적용하지 않았을 때와 적용했을 때의 차이를 보여준다.
Expand Down Expand Up @@ -79,4 +82,8 @@ public class AdapterServiceA {
}
}
```
- 어댑터 합성, 즉 객체를 속성으로 만들어서 참조하고 있다.
- 어댑터 합성, 즉 객체를 속성으로 만들어서 참조하고 있다.
- 속성으로 만들어서 그 속성이 가지고 있는 메소드를 수행하는 메소드를 가진다. (`runService()`)


### 결론

0 comments on commit f02dd27

Please sign in to comment.