package algo.bst.ch01;
class Node {
int key;
Node left;
Node right;
public Node(int key) {
this.key = key;
}
}
// BST -> 균형이 맞지 않는 트리
// B-tree -> 균형이 맞는 트리, 한 노드 당 3개, 노드의 차수가 2개면 자식 3개, 노드의 차수가 3개면 자식 4개
public class BinarySearchTree01 {
public static void main(String[] args) {
// 50 -> 30 -> 70 -> 20 -> 40 -> 60 -> 80 -> 10 -> 25 -> 35 -> 45
Node root = new Node(50);
root.left = new Node(30);
root.right = new Node(70);
root.left.left = new Node(20);
root.left.right = new Node(40);
root.right.left = new Node(60);
root.right.right = new Node(80);
root.left.left.left = new Node(10);
root.left.left.right = new Node(25);
root.left.right.left = new Node(35);
root.left.right.right = new Node(45);
// 출력 (전위)
System.out.println(root.key);
System.out.println(root.left.key + ",");
System.out.println(root.left.left.key + ",");
System.out.println(root.left.left.left.key + ",");
System.out.println(root.left.left.right.key + ",");
System.out.println(root.left.right.key + ",");
System.out.println(root.left.right.left.key + ",");
System.out.println(root.left.right.right.key + ",");
System.out.println(root.right.key + ",");
System.out.println(root.right.left.key + ",");
System.out.println(root.right.right.key);
}
}

Share article