forked from rangken/AlgorithmStudy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTree.java
More file actions
91 lines (81 loc) · 2.09 KB
/
Copy pathBinaryTree.java
File metadata and controls
91 lines (81 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class BinaryTree<E>{
protected Node<E> root;
protected int size = 0;
protected INodeCreator creator = null;
public BinaryTree(){
root = null;
}
final public void insertLeft(E data, Node<E> node){
if(node.left != null){
System.out.println("왼쪽 노드가 이미 차있음");
return ;
}
if(root == null){
root = node;
}
Node<E> newNode = new Node<E>(data);
node.left = newNode;
}
final public void insertRight(E data, Node<E> node){
if(node.right != null){
System.out.println("오른쪽 노드가 이미 차있음");
return ;
}
if(root == null){
root = node;
}
Node<E> newNode = new Node<E>(data);
node.right = newNode;
}
protected int getMaxLevel(Node<E> node){
if(node == null)
return 0;
return java.lang.Math.max(getMaxLevel(node.left), getMaxLevel(node.right)) + 1;
}
public void printPreOrder(){
BinaryTreePrinter.printPreOrder(root);
}
public void printInOrder(){
BinaryTreePrinter.printInOrder(root);
}
public void printPostOrder(){
BinaryTreePrinter.printPostOrder(root);
}
public void printLevelOrder(){
BinaryTreePrinter.printLevelOrder(root);
}
public void printBFS(){
BinaryTreePrinter.printBFS(root);
}
public void print(){
BinaryTreePrinter.print(root);
}
public static class Node<E>{
// Inner class 에서는 private 라도 멤버 변수 접근 가능함!
public E element;
public Node<E> parent;
public Node<E> left;
public Node<E> right;
public Node(){
this.left = null;
this.right = null;
}
public Node(E element){
this.element = element;
this.left = null;
this.right = null;
}
public Node(E element,Node<E> parent){
this.parent = parent;
this.element = element;
this.left = null;
this.right = null;
}
public void visit(){
System.out.print(element);
}
}
protected static interface INodeCreator<E extends Comparable<E>> {
public Node<E> createNewNode(E id,Node<E> parent);
}
}