public class BinaryTree {
private TreeNode root;
//순회 travers : (preOrder)
public void preOrder(TreeNode tempNode) {
if (tempNode != null) {
tempNode.showNode();
preOrder(tempNode.leftNode);
preOrder(tempNode.rightNode);
}
}
//순회 travers : (inorder)
public void inorder(TreeNode tempNode) {
if (tempNode != null) {
inorder(tempNode.leftNode);
tempNode.showNode();
inorder(tempNode.rightNode);
}
}
//순회 travers : (postorder)
public void postorder(TreeNode tempNode) {
if (tempNode != null) {
postorder(tempNode.leftNode);
postorder(tempNode.rightNode);
tempNode.showNode();
}
}
//탐색
public TreeNode search(int value) {
TreeNode current = root;
while (current.value != value) {
if (current == null)
return null;
if (value < current.value)
current = current.leftNode;
else
current = current.rightNode;
}
return current;
}
//최소 노드
public TreeNode getMin() {
TreeNode current = root;
if (current.leftNode != null)
current = current.leftNode;
return current;
}
//최대 노드
public TreeNode getMax() {
TreeNode current = root;
if (current.rightNode != null)
current = current.rightNode;
return current;
}
//삽입
public void insert(int value) {
TreeNode insertNode = new TreeNode();
insertNode.value = value;
if (root == null)
root = insertNode;
else {
TreeNode current = root;
TreeNode parent;
while (true) {
parent = current;
if (value < current.value) {
current = current.leftNode;
if (current == null) {
parent.leftNode = insertNode;
return;
}
} else {
current = current.rightNode;
if (current == null) {
parent.rightNode = insertNode;
return;
}
}
}
}
}
//삭제
public void delete() {
}
}
class TreeNode {
int value;
TreeNode leftNode;
TreeNode rightNode;
public void showNode() {
Log.i("kjw", String.format("[%d]", value));
}
}
private TreeNode root;
//순회 travers : (preOrder)
public void preOrder(TreeNode tempNode) {
if (tempNode != null) {
tempNode.showNode();
preOrder(tempNode.leftNode);
preOrder(tempNode.rightNode);
}
}
//순회 travers : (inorder)
public void inorder(TreeNode tempNode) {
if (tempNode != null) {
inorder(tempNode.leftNode);
tempNode.showNode();
inorder(tempNode.rightNode);
}
}
//순회 travers : (postorder)
public void postorder(TreeNode tempNode) {
if (tempNode != null) {
postorder(tempNode.leftNode);
postorder(tempNode.rightNode);
tempNode.showNode();
}
}
//탐색
public TreeNode search(int value) {
TreeNode current = root;
while (current.value != value) {
if (current == null)
return null;
if (value < current.value)
current = current.leftNode;
else
current = current.rightNode;
}
return current;
}
//최소 노드
public TreeNode getMin() {
TreeNode current = root;
if (current.leftNode != null)
current = current.leftNode;
return current;
}
//최대 노드
public TreeNode getMax() {
TreeNode current = root;
if (current.rightNode != null)
current = current.rightNode;
return current;
}
//삽입
public void insert(int value) {
TreeNode insertNode = new TreeNode();
insertNode.value = value;
if (root == null)
root = insertNode;
else {
TreeNode current = root;
TreeNode parent;
while (true) {
parent = current;
if (value < current.value) {
current = current.leftNode;
if (current == null) {
parent.leftNode = insertNode;
return;
}
} else {
current = current.rightNode;
if (current == null) {
parent.rightNode = insertNode;
return;
}
}
}
}
}
//삭제
public void delete() {
}
}
class TreeNode {
int value;
TreeNode leftNode;
TreeNode rightNode;
public void showNode() {
Log.i("kjw", String.format("[%d]", value));
}
}
'프로그래밍 > 알고리즘' 카테고리의 다른 글
다익스트라(Dijkstra Algorithm) (0) | 2017.02.24 |
---|---|
DFS(미로찾기) (0) | 2017.02.24 |
quickSort (0) | 2017.02.24 |
Queue (0) | 2017.02.24 |
binarySearch (0) | 2017.02.24 |