* 이진 탐색 트리
package algo.bstree; import java.util.ArrayList; import java.util.List; public class BSTree { public enum DIR { LEFT, RIGHT } public class Node { public int data; public Node left; public Node right; public Node(int data) { this.data = data; left = null; right = null; } } public Node root; public void insert(int data) { Node newNode = new Node(data); if (root == null) { root = newNode; return; } Node current = root; Node parent = null; while (true) { parent = current; if (current.data == data) { System.out.println("이미 있는 데이터 입니다 : " + data); return; } else if (current.data < data) { current = current.right; if (current == null) { parent.right = newNode; System.out.println("노드 삽입 : " + data); return; } } else { current = current.left; if (current == null) { parent.left = newNode; System.out.println("노드 삽입 : " + data); return; } } } } public void find(int data) { find(root, data); } private Node find(Node node, int data) { if (node == null) { System.out.println("찾는 데이터가 없습니다 : " + data); return null; } if (node.data == data) { System.out.println("찾았습니다 : " + data); return node; } if (node.data < data) return find(node.right, data); else return find(node.left, data); } public void delete(int data) { Node current = root; Node parent = root; DIR dir = null; if (current == null) { System.out.println("삭제할 데이터가 없습니다 : " + data); return; } while (current.data != data) { parent = current; if (current.data < data) { current = current.right; dir = DIR.RIGHT; } else { current = current.left; dir = DIR.LEFT; } if (current == null) { System.out.println("삭제할 데이터가 없습니다 : " + data); return; } } if (current.left == null && current.right == null) { // 삭제할 노드가 자식을 갖지 않는 경우 if (current == root) { // 루트 노드밖에 없는 경우 current = null; root = null; } else { if (dir == DIR.LEFT) parent.left = null; else parent.right = null; } } else if (current.right == null || current.left == null) { // 삭제할 노드가 자식을 한개만 갖는경우 if (current == root) { // 루트 노드 인 경우 root = current.right == null ? current.left : current.right; } else { if (dir == DIR.LEFT) { parent.left = current.right == null ? current.left : current.right; } else { parent.right = current.right == null ? current.left : current.right; } } } else { // 자식 노드가 둘다 있는 경우 // current 노드가 자식을 둘다 갖는경우, Successor를 current에 Set해줘야한다. Node successor = getSuccessor(current); if (current == root) { root = successor; } else { if (dir == DIR.LEFT) parent.left = successor; else parent.right = successor; } successor.left = current.left; } if (root == null || root == current) System.out.println("루트 노드 삭제 : " + data); else System.out.println("노드 삭제 : " + data); } public Node getSuccessor(Node delNode) { // successor는 Right Sub Tree 중에서 가장 작은 값이다. Node successor = delNode.right; Node successorParent = delNode; while (successor.left != null) { successorParent = successor; successor = successor.left; } // successor가 Del Node의 오른쪽 자식과 같다면 // sucessor의 right 트리는 그대로 따라 올라간다. if (delNode.right != successor) { successorParent.left = successor.right; successor.right = delNode.right; } return successor; } public void inOrder(Node node) { if (node != null) { inOrder(node.left); System.out.print(" " + node.data); inOrder(node.right); } } public void printPreety() { List<Node> list = new ArrayList<Node>(); list.add(root); printTree(list, getHeight(root)); } private int getHeight(Node head) { if (head == null) { return 0; } else { return 1 + Math.max(getHeight(head.left), getHeight(head.right)); } } private void printTree(List<Node> levelNodes, int level) { List<Node> nodes = new ArrayList<Node>(); // indentation for first node in given level printIndentForLevel(level); for (Node Node : levelNodes) { // print node data System.out.print(Node == null ? " " : Node.data); // spacing between nodes printSpacingBetweenNodes(level); // if its not a leaf node if (level > 1) { nodes.add(Node == null ? null : Node.left); nodes.add(Node == null ? null : Node.right); } } System.out.println(); if (level > 1) { printTree(nodes, level - 1); } } private void printIndentForLevel(int level) { for (int i = (int) (Math.pow(2, level - 1)); i > 0; i--) { System.out.print(" "); } } private void printSpacingBetweenNodes(int level) { // spacing between nodes for (int i = (int) ((Math.pow(2, level - 1)) * 2) - 1; i > 0; i--) { System.out.print(" "); } } }*테스트
import java.util.Arrays; import java.util.Collections; import java.util.List; import algo.binarytree.MyBinaryTree; import algo.binarytree.MyBinaryTree.DIR; import algo.binarytree.MyBinaryTree.MyBTNode; import algo.bstree.BSTree; import algo.radix.MyRadixSort; public class Main { public static void main(String[] args) { BSTree tree = new BSTree(); makeBinarySearchTree(tree); prettyPrintTree(tree); tree.find(4); tree.delete(2); prettyPrintTree(tree); tree.delete(4); prettyPrintTree(tree); tree.delete(10); prettyPrintTree(tree); tree.delete(1); prettyPrintTree(tree); tree.insert(9); prettyPrintTree(tree); tree.insert(1); prettyPrintTree(tree); tree.find(100); prettyPrintTree(tree); tree.insert(100); prettyPrintTree(tree); tree.delete(15); prettyPrintTree(tree); tree.delete(16); prettyPrintTree(tree); tree.delete(9); prettyPrintTree(tree); tree.delete(8); prettyPrintTree(tree); tree.delete(3); prettyPrintTree(tree); } public static void prettyPrintTree(BSTree tree) { // tree.inOrder(tree.root); // System.out.println(""); System.out.println("---- 트리 출력 ----"); tree.printPreety(); System.out.println(""); } public static void makeBinarySearchTree(BSTree tree) { tree.insert(3); tree.insert(8); tree.insert(1); tree.insert(4); tree.insert(6); tree.insert(2); tree.insert(10); tree.insert(9); tree.insert(20); tree.insert(25); tree.insert(15); tree.insert(16); } } **** 결과 **** 노드 삽입 : 8 노드 삽입 : 1 노드 삽입 : 4 노드 삽입 : 6 노드 삽입 : 2 노드 삽입 : 10 노드 삽입 : 9 노드 삽입 : 20 노드 삽입 : 25 노드 삽입 : 15 노드 삽입 : 16 ---- 트리 출력 ---- 3 1 8 2 4 10 6 9 20 15 25 16 찾았습니다 : 4 노드 삭제 : 2 ---- 트리 출력 ---- 3 1 8 4 10 6 9 20 15 25 16 노드 삭제 : 4 ---- 트리 출력 ---- 3 1 8 6 10 9 20 15 25 16 노드 삭제 : 10 ---- 트리 출력 ---- 3 1 8 6 15 9 20 16 25 노드 삭제 : 1 ---- 트리 출력 ---- 3 8 6 15 9 20 16 25 이미 있는 데이터 입니다 : 9 ---- 트리 출력 ---- 3 8 6 15 9 20 16 25 노드 삽입 : 1 ---- 트리 출력 ---- 3 1 8 6 15 9 20 16 25 찾는 데이터가 없습니다 : 100 ---- 트리 출력 ---- 3 1 8 6 15 9 20 16 25 노드 삽입 : 100 ---- 트리 출력 ---- 3 1 8 6 15 9 20 16 25 100 노드 삭제 : 15 ---- 트리 출력 ---- 3 1 8 6 16 9 20 25 100 노드 삭제 : 16 ---- 트리 출력 ---- 3 1 8 6 20 9 25 100 노드 삭제 : 9 ---- 트리 출력 ---- 3 1 8 6 20 25 100 노드 삭제 : 8 ---- 트리 출력 ---- 3 1 20 6 25 100 노드 삭제 : 3 ---- 트리 출력 ---- 6 1 20 25 100
'Algorithm - Using java' 카테고리의 다른 글
Binary Tree (0) | 2018.06.07 |
---|---|
Radix Sort (0) | 2018.06.07 |
Counting Sort (0) | 2018.05.29 |
Priority Queue (0) | 2018.05.29 |
Heap Sort (0) | 2018.05.28 |