-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
130 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters