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
140 changes: 140 additions & 0 deletions src/Traversal.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,145 @@
import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.Queue;
import java.util.Stack;

public class Traversal {
public static void main(String[] args) {
TreeNode<Integer> root = new TreeNode<>(10, null, null);

root.left = new TreeNode<>(9, null, null);
root.right = new TreeNode<>(15, null, null);

root.left.left = new TreeNode<>(5, null, null);
root.left.right = new TreeNode<>(2, null, null);

root.right.left = new TreeNode<>(-3, null, null);
root.right.right = new TreeNode<>(5, null, null);

root.right.right.right = new TreeNode<>(22, null, null);

TreeNode<String> stringRoot = new TreeNode<String>("hello", null, null);
stringRoot.left = new TreeNode<String>("how", null, null);
stringRoot.right = new TreeNode<String>("are", null, null);

stringRoot.left.left = new TreeNode<String>("you", null, null);
stringRoot.left.right = new TreeNode<String>("I", null, null);

stringRoot.right.left = new TreeNode<String>("am", null, null);
stringRoot.right.right = new TreeNode<String>("so", null, null);

stringRoot.right.right.right = new TreeNode<String>("good", null, null);

TreeNode<Integer> megaRoot = new TreeNode<>(1, null, null);

TreeNode<Integer> current = megaRoot;
for (int i = 2; i < 50001; i++){
current.right = new TreeNode<Integer>(i, null, null);
current = current.right;
}

levelOrder(root);
//preorder(megaRoot);

// System.out.println("Preorder recursive");
// preorder(root);
// System.out.println("Preorder iterative");
// preOrderIter(megaRoot);

// preorder(root);
//postorder(root);
// inorder(stringRoot);

//allows it to fail at compile time rather than runtime
// printGreater(root, 100);

// System.out.println(toMap(stringRoot));
}

public static <T> void preOrderIter(TreeNode<T> node){
Stack<TreeNode<T>> stack = new Stack<>();
stack.push(node);

while (!stack.empty()){
TreeNode<T> current = stack.pop();
if (current == null) continue;
System.out.println(current.value);
stack.push(current.right);
stack.push(current.left);
}
}

public static <T> void levelOrder(TreeNode<T> node){
Queue<TreeNode<T>> queue = new LinkedList<>();
queue.add(node);

while (!queue.isEmpty()){
TreeNode<T> current = queue.poll();
if (current == null){
continue;
}

System.out.println(current.value);
queue.add(current.left);
queue.add(current.right);
}
}

public static <E> void preorder(TreeNode<E> current1){

if (current1 == null) return;

System.out.println(current1.value);

if (current1.left != null) preorder(current1.left);

if (current1.right != null) preorder(current1.right);
}

public static <T> Map<T, Integer> toMap(TreeNode<T> node){
Map<T, Integer> counts = new HashMap<>();
toMapHelper(node, counts);
return counts;
}

private static <T> void toMapHelper(TreeNode<T> node, Map<T, Integer> counts){
if (node == null) return;

counts.put(node.value, counts.getOrDefault(node.value, 0) + 1);
toMapHelper(node.left, counts);
toMapHelper(node.right, counts);
}

public static int countNodes(TreeNode<?> node){
if (node == null) return 0;
// int leftCount = countNodes(node.left);
// int rightCount = countNodes(node.right);
// int overallCount = leftCount + rightCount + 1;
// return overallCount;

return countNodes(node.left) + countNodes(node.right) + 1;
}


public static void printGreater(TreeNode<Integer> node, int threshold){
if (node == null) return;
if (node.value > threshold) System.out.println(node.value);
printGreater(node.left, threshold);
printGreater(node.right, threshold);
}

public static <T> void postorder(TreeNode<T> node){
if (node == null) return;
postorder(node.left);
postorder(node.right);
System.out.println(node.value);
}

public static <T> void inorder(TreeNode<T> node){
if (node == null) return;
inorder(node.left);
System.out.println(node.value);
inorder(node.right);
}
}
8 changes: 4 additions & 4 deletions src/TraversalTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import org.junit.jupiter.api.Test;
// import org.junit.jupiter.api.Test;

public class TraversalTest {

}
// public class TraversalTest {
// }
13 changes: 10 additions & 3 deletions src/TreeNode.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
public class TreeNode {


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

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