-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree.java
More file actions
executable file
·213 lines (171 loc) · 6.34 KB
/
Copy pathTree.java
File metadata and controls
executable file
·213 lines (171 loc) · 6.34 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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package com.codeWithArsalon.NonLinearDS;
import java.util.ArrayList;
public class Tree {
private class Node{
private int value;
private Node leftChild;
private Node rightChild;
public Node(int value) {
this.value = value;
}
@Override
public String toString() {
return "Node=" + value;
}
}
private Node root;
public void addItem(int value){
var node = new Node(value);
if(root == null){
root = node;
return;
}
var current = root;
while(true){
if(value < current.value){
if(current.leftChild == null){ //leaf node
current.leftChild = node;
break;
}
current = current.leftChild; //else, continue to next left child
}
else{
if(current.rightChild == null){ //leaf node
current.rightChild = node;
break;
}
current = current.rightChild; //else, continue to next right child
}
}
}
public boolean lookup(int value){
var current = root;
while (current != null){
if(value < current.value) //left subtree
current = current.leftChild;
else if (value > current.value) //right tree
current = current.rightChild;
else
return true;
}
return false; //if node isn't in tree, current will become null eventually and break out of loop
}
public void traversePreOrder(){
traversePreOrder(root);// kick off recursion
}
private void traversePreOrder(Node root){
if(root == null) //base condition
return;
System.out.println(root.value);
traversePreOrder(root.leftChild);
traversePreOrder(root.rightChild);
}
public void traversePostOrder(){
traversePostOrder(root); //kick off recursion
}
private void traversePostOrder(Node root){
if(root == null) //base condition
return;
traversePostOrder(root.leftChild);
traversePostOrder(root.rightChild);
System.out.println(root.value);
}
public void traverseInOrder(){
traverseInOrder(root); //kick off recursion
}
private void traverseInOrder(Node root){
if(root == null) //base condition
return;
traversePreOrder(root.leftChild);
System.out.println(root.value);
traversePreOrder(root.rightChild);
}
public void traverseLevelOrder(){
for(var i = 0; i <= height(); i++) { //longest path to root node
for (var value : getNodesAtDistance(i)) //returns an ArrayList of nodes at index i
System.out.println(value);
}
}
public ArrayList<Integer> getNodesAtDistance(int distance){
var list = new ArrayList<Integer>(); //stores values in array list
getNodesAtDistance(root, distance, list);
return list;
}
private void getNodesAtDistance(Node root, int distance, ArrayList list){
if (root == null) //if tree is empty, return
return;
if(distance == 0) { //base condition, terminate recursion, print value
list.add(root.value); //add value to list
return;
}
getNodesAtDistance(root.leftChild, distance - 1, list); //look at right & left children, decrement distance by 1
getNodesAtDistance(root.rightChild, distance - 1, list);
}
public int height(){
return height(root);
}
private int height(Node root){
if(root == null)
return -1;
if(isLeafNode(root)) //base condition
return 0; //height of leaf nodes is zero
var count = 1 + Math.max(
height(root.leftChild), //breaking problem into smaller parts
height(root.rightChild));
return count;
}
public boolean isBinarySearchTree(){
return isBinarySearchTree(root,
Integer.MIN_VALUE,
Integer.MAX_VALUE);
}
private boolean isBinarySearchTree(Node root, int min, int max){
if (root == null) //empty tree is a binary tree
return true;
if(isLeafNode(root))
return true;
if(root.value < min || root.value > max) //means node is out of range
return false;
return
isBinarySearchTree(root.leftChild, min, root.value - 1) //tells is left child is in right range
&& isBinarySearchTree(root.rightChild, root.value + 1, max); //tells is right child is in right range
}
public boolean equals(Tree other){
return equals(root, other.root); //pass root of current, root of other tree
}
private boolean equals(Node first, Node second){
if(first == null && second == null) //both nodes are null (scenario #1)
return true;
if(first != null && second != null) //both nodes are not null (scenario #2)
return (first.value == second.value) //check root for equality
&& equals(first.leftChild, second.leftChild) //check left subtree for equality
&& equals(first.rightChild, second.rightChild); //check right subtree for equality
return false; //one node is null, other is not (scenario #3)
}
public int minBinaryTree(){
//O (log n) time complexity
if(root == null)
throw new IllegalStateException(); //empty tree
var current = root;
var last = current;
while(current != null){
last = current; //stores last value
current = current.leftChild;
}
return last.value; //references leaf-most node
}
public int min(){
return min(root); //non binary search
}
private int min(Node root){
//O(n) complexity
if(isLeafNode(root)) //base condition
return root.value;
var left = min(root.leftChild); //min in left tree
var right = min(root.rightChild); //min in right
return Math.min(Math.min(left, right), root.value); //find min of three values (left, right, root)
}
private boolean isLeafNode(Node root) {
return root.leftChild == null & root.rightChild == null;
}
}