Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 111 additions & 1 deletion src/Traversal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,115 @@
import java.util.LinkedList;
import java.util.Stack;

public class Traversal {
public static void main(String[] args) {

TreeNode<Integer> root = new TreeNode<>(10);
root.left = new TreeNode<>(9);
root.left.left = new TreeNode<>(5);
root.left.right = new TreeNode<>(2);

root.right = new TreeNode<>(15);
root.right.left = new TreeNode<>(-3);
root.right.right = new TreeNode<>(5);
root.right.right.right = new TreeNode<>(22);

TreeNode<String> stringRoot = new TreeNode<>("Hello");
stringRoot.left = new TreeNode<>("hi");
stringRoot.left.left = new TreeNode<>("what");
stringRoot.left.right = new TreeNode<>("jessica");

stringRoot.right = new TreeNode<>("yes");
stringRoot.right.left = new TreeNode<>("blue");
stringRoot.right.right = new TreeNode<>("yellow");
stringRoot.right.right.right = new TreeNode<>("pink");

TreeNode<Integer> megaRoot = new TreeNode<Integer> (1);
TreeNode<Integer> current = megaRoot;

for(int i=2; i<100_001; i++){
current.right = new TreeNode<Integer>(i);
current= current.right;
}
preOrder(megaRoot);
public static void preOrderIterative(TreeNode<?> root){

Stack<TreeNode<?>> stack = new Stack<>();
stack.push(root);

while (!stack.isEmpty()){
TreeNode<?> current = stack.pop();
if(current == null) continue;
//do something
System.out.println(current.value);
stack.push(current.right);
stack.push(current.left);
}
}

public static void levelOrder(TreeNode<?> root){
Queue<TreeNode<?>> queue = new LinkedList<>();
queue.offer(root);

while(!queue.isEmpty()){
TreeNode<?> current = queue.remove();
if(current == null) continue;
System.out.println(current.value);
}
}
//printGreaterThan(root, 4);
preOrder(stringRoot);

int results = sum(root);
System.out.println(results);
System.err.println(oddSum(root));
}

public static void printGreaterThan(TreeNode<Integer> current, int threshold) {
if (current == null) return;

if (current.value > threshold) {
System.out.println(current.value);
}

printGreaterThan(current.left, threshold);
printGreaterThan(current.right, threshold);
}
public static int countNodes(TreeNode<?> current){
if (current == null) return 0;
int leftCount = countNodes(current.left);
int rightCount = countNodes(current.right);
int totalCount = 1+ leftCount + rightCount;

return totalCount;




}

public static void preOrder(TreeNode<?> current) {
if (current == null) return;

System.out.println(current.data);
preOrder(current.left);
preOrder(current.right);
}

public static <E> void postOrder(TreeNode<E> current) {
if (current == null) return;

postOrder(current.left);
postOrder(current.right);
E myValue = current.value;
System.out.println(myValue);
}

public static void inOrder(TreeNode current) {
if (current == null) return;

inOrder(current.left);
System.out.println(current.data);
inOrder(current.right);
}
}
}
21 changes: 19 additions & 2 deletions src/TreeNode.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
public class TreeNode {



public class TreeNode <T> {
public T data;
public TreeNode <T> left;
public TreeNode <T> right;

public TreeNode (T data){
this.data = data;
}
public TreeNode (T data, TreeNode <T> left){
this.data = data;
this.left = left;

}
public TreeNode (T data, TreeNode <T>left, TreeNode <T> right){
this.data = data;
this.left = left;
this.right = right;
}
}