-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinaryTree.java
119 lines (108 loc) · 3.1 KB
/
BinaryTree.java
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package techQuestions;
import java.util.Iterator;
import java.util.LinkedList;
//Erich's binary tree implementation!
class BinaryTree {
BinaryTree left, right;
int data;
public BinaryTree(int data) {
this.data = data;
}
// insert value into binary tree by the rules of binary tree
public void insert(int value) {
//if the value to be inserted is less than the current node data
if (value <= data) {
// if left node doesn't exist yet, just place new value there
if (left == null) {
left = new BinaryTree(value);
// else, look to insert new value underneath left node
} else {
left.insert(value);
}
}
// if value is bigger than current node
if (value > data) {
if (right == null) {
// if no right node, then value is new right node
right = new BinaryTree(value);
} else {
right.insert(value);
}
}
}
public int maxDepth(BinaryTree root) {
if (root == null) {
return 0;
} else {
int leftMax = maxDepth(root.left);
int rightMax = maxDepth(root.right);
return Math.max(leftMax, rightMax) + 1;
}
}
public int maxDepthIterative(BinaryTree root) {
LinkedList<BinaryTree> stack = new LinkedList<>();
LinkedList<Integer> depths = new LinkedList<>();
if (root == null ) {
return 0;
}
stack.add(root);
depths.add(1);
int i = 0;
int depth = 0, currentDepth = 0;
while(!stack.isEmpty()) {
// System.out.println("while loop number: " + i);
// new root node is the most recent element to be added to this stack
root = stack.pollLast();
// new depth is most recent element to be added to depths stack
currentDepth = depths.pollLast();
if (root != null) {
// System.out.println("current root is: " + root.data);
// System.out.println("currentDepth is: " + currentDepth);
depth = Math.max(currentDepth, depth);
stack.add(root.left);
stack.add(root.right);
depths.add(currentDepth + 1);
depths.add(currentDepth + 1);
// System.out.println("depths: " + depths.toString());
Iterator<BinaryTree> stackIterator = stack.iterator();
// System.out.println("here comes stack elements!!!");
while (stackIterator.hasNext()) {
BinaryTree current = stackIterator.next();
if (current != null) {
// System.out.print(current.data + ", ");
} else {
// System.out.print("NULL!, ");
}
}
// System.out.println();
} else {
// System.out.println("ROOT WAS NULL!");
// System.out.println("depths: " + depths.toString());
Iterator<BinaryTree> stackIterator = stack.iterator();
// System.out.println("here comes stack elements!!!");
while (stackIterator.hasNext()) {
BinaryTree current = stackIterator.next();
if (current != null) {
// System.out.print(current.data + ", ");
} else {
// System.out.print("NULL!, ");
}
}
}
i++;
}
return depth;
}
public void printInOrder() {
// print left first by recursing down left side
if (left != null) {
left.printInOrder();
}
// then print root node
System.out.println(data);
// finally, recurse down right side
if (right != null) {
right.printInOrder();
}
}
}