From 6048270224cc57f5ca6dcd107b083692157f0a0b Mon Sep 17 00:00:00 2001 From: Abhinav Sarkar Date: Fri, 9 Aug 2019 15:31:44 +0530 Subject: [PATCH] Minor changes + reformatting --- .../abhinavsarkar/algorist/TreeIterators.java | 835 +++++++++--------- 1 file changed, 418 insertions(+), 417 deletions(-) diff --git a/src/main/java/net/abhinavsarkar/algorist/TreeIterators.java b/src/main/java/net/abhinavsarkar/algorist/TreeIterators.java index 8c78b32..18edc3a 100644 --- a/src/main/java/net/abhinavsarkar/algorist/TreeIterators.java +++ b/src/main/java/net/abhinavsarkar/algorist/TreeIterators.java @@ -8,101 +8,101 @@ import java.util.function.Consumer; import java.util.function.Supplier; class TreeIerators { - public static void main(String[] args) { - Tree tree; - do { - tree = Tree.generate(4, () -> Utils.generateRandomAlphaString(2), 0.1); - } while (tree == null); + public static void main(String[] args) { + Tree tree; + do { + tree = Tree.generate(4, () -> Utils.generateRandomAlphaString(2), 0.1); + } while (tree == null); - System.out.println("Tree:"); - System.out.println(tree); + System.out.println("Tree:"); + System.out.println(tree); - // InOrderIterator - InOrderIterator.printRecursive(tree); - System.out.println("\tInOrderIterator.printRecursive"); + // InOrderIterator + InOrderIterator.printRecursive(tree); + System.out.println("\tInOrderIterator.printRecursive"); - InOrderIterator.iterateRecursive(tree, Utils::printContent); - System.out.println("\tInOrderIterator.iterateRecursive"); + InOrderIterator.iterateRecursive(tree, Utils::printContent); + System.out.println("\tInOrderIterator.iterateRecursive"); - InOrderIterator.iterateCPS(tree, Utils::printContent, () -> {}); - System.out.println("\tInOrderIterator.iterateCPS"); + InOrderIterator.iterateCPS(tree, Utils::printContent, () -> {}); + System.out.println("\tInOrderIterator.iterateCPS"); - InOrderIterator.iterateDefCPS(tree, Utils::printContent, null); - System.out.println("\tInOrderIterator.iterateDefCPS"); + InOrderIterator.iterateDefCPS(tree, Utils::printContent, null); + System.out.println("\tInOrderIterator.iterateDefCPS"); - InOrderIterator.iterate(tree, Utils::printContent, null); - System.out.println("\tInOrderIterator.iterate"); + InOrderIterator.iterate(tree, Utils::printContent, null); + System.out.println("\tInOrderIterator.iterate"); - InOrderIterator inOrderIterator = new InOrderIterator<>(tree); - while (inOrderIterator.hasNext()) { - Utils.printContent(inOrderIterator.next()); - } - System.out.println("\tInOrderIterator"); - System.out.println(); - - // PreOrderIterator - PreOrderIterator.printRecursive(tree); - System.out.println("\tPreOrderIterator.printRecursive"); - - PreOrderIterator.iterateRecursive(tree, Utils::printContent); - System.out.println("\tPreOrderIterator.iterateRecursive"); - - PreOrderIterator.iterateCPS(tree, Utils::printContent, () -> {}); - System.out.println("\tPreOrderIterator.iterateCPS"); - - PreOrderIterator.iterateDefCPS(tree, Utils::printContent, null); - System.out.println("\tPreOrderIterator.iterateDefCPS"); - - PreOrderIterator.iterate(tree, Utils::printContent, null); - System.out.println("\tPreOrderIterator.iterate"); - - PreOrderIterator preOrderIterator = new PreOrderIterator<>(tree); - while (preOrderIterator.hasNext()) { - Utils.printContent(preOrderIterator.next()); - } - System.out.println("\tPreOrderIterator"); - System.out.println(); - - // PostOrderIterator - PostOrderIterator.printRecursive(tree); - System.out.println("\tPostOrderIterator.printRecursive"); - - PostOrderIterator.iterateRecursive(tree, Utils::printContent); - System.out.println("\tPostOrderIterator.iterateRecursive"); - - PostOrderIterator.iterateCPS(tree, Utils::printContent, () -> {}); - System.out.println("\tPostOrderIterator.iterateCPS"); - - PostOrderIterator.iterateDefCPS(tree, Utils::printContent, null); - System.out.println("\tPostOrderIterator.iterateDefCPS"); - - PostOrderIterator.iterate(tree, Utils::printContent, null); - System.out.println("\tPostOrderIterator.iterate"); - - PostOrderIterator postOrderIterator = new PostOrderIterator<>(tree); - while (postOrderIterator.hasNext()) { - Utils.printContent(postOrderIterator.next()); - } - System.out.println("\tPostOrderIterator"); + InOrderIterator inOrderIterator = new InOrderIterator<>(tree); + while (inOrderIterator.hasNext()) { + Utils.printContent(inOrderIterator.next()); } + System.out.println("\tInOrderIterator"); + System.out.println(); + + // PreOrderIterator + PreOrderIterator.printRecursive(tree); + System.out.println("\tPreOrderIterator.printRecursive"); + + PreOrderIterator.iterateRecursive(tree, Utils::printContent); + System.out.println("\tPreOrderIterator.iterateRecursive"); + + PreOrderIterator.iterateCPS(tree, Utils::printContent, () -> {}); + System.out.println("\tPreOrderIterator.iterateCPS"); + + PreOrderIterator.iterateDefCPS(tree, Utils::printContent, null); + System.out.println("\tPreOrderIterator.iterateDefCPS"); + + PreOrderIterator.iterate(tree, Utils::printContent, null); + System.out.println("\tPreOrderIterator.iterate"); + + PreOrderIterator preOrderIterator = new PreOrderIterator<>(tree); + while (preOrderIterator.hasNext()) { + Utils.printContent(preOrderIterator.next()); + } + System.out.println("\tPreOrderIterator"); + System.out.println(); + + // PostOrderIterator + PostOrderIterator.printRecursive(tree); + System.out.println("\tPostOrderIterator.printRecursive"); + + PostOrderIterator.iterateRecursive(tree, Utils::printContent); + System.out.println("\tPostOrderIterator.iterateRecursive"); + + PostOrderIterator.iterateCPS(tree, Utils::printContent, () -> {}); + System.out.println("\tPostOrderIterator.iterateCPS"); + + PostOrderIterator.iterateDefCPS(tree, Utils::printContent, null); + System.out.println("\tPostOrderIterator.iterateDefCPS"); + + PostOrderIterator.iterate(tree, Utils::printContent, null); + System.out.println("\tPostOrderIterator.iterate"); + + PostOrderIterator postOrderIterator = new PostOrderIterator<>(tree); + while (postOrderIterator.hasNext()) { + Utils.printContent(postOrderIterator.next()); + } + System.out.println("\tPostOrderIterator"); + } /* Run result: Tree: ├ r - │├ j - ││├ x - │││├ e - │││├ m - ││├ vz - │││├ g - ││├ - │├ l - ││├ b - │││├ qc - │││├ g - ││├ rp - │││├ d - │││├ o + │ ├ j + │ │ ├ x + │ │ │ ├ e + │ │ │ ├ m + │ │ ├ vz + │ │ │ ├ g + │ │ │ ├ + │ ├ l + │ │ ├ b + │ │ │ ├ qc + │ │ │ ├ g + │ │ ├ rp + │ │ │ ├ d + │ │ │ ├ o e x m j g vz r qc b g l d rp o InOrderIterator.printRecursive e x m j g vz r qc b g l d rp o InOrderIterator.iterateRecursive @@ -128,414 +128,415 @@ class TreeIerators { } class Tree { - Tree left; - T content; - Tree right; + Tree left; + T content; + Tree right; - Tree(Tree left, T content, Tree right) { - this.left = left; - this.content = content; - this.right = right; + Tree(Tree left, T content, Tree right) { + this.left = left; + this.content = content; + this.right = right; + } + + public static Tree generate(int maxDepth, Supplier gen, double nullProbability) { + if (nullProbability < 0 || nullProbability > 1) { + throw new IllegalArgumentException("nullProbability must be between 0 and 1"); } - public static Tree generate(int maxDepth, Supplier gen, double nullProbability) { - if (nullProbability < 0 || nullProbability > 1) { - throw new IllegalArgumentException("nullProbability must be between 0 and 1"); - } - - if (maxDepth == 0) { - return null; - } - - double rand = ThreadLocalRandom.current().nextDouble(); - if (rand < nullProbability) { - return null; - } - - Tree left = generate(maxDepth - 1, gen, nullProbability); - Tree right = generate(maxDepth - 1, gen, nullProbability); - return new Tree<>(left, gen.get(), right); + if (maxDepth == 0) { + return null; } - @Override - public String toString() { - return toStringLayout(0).toString(); + double rand = ThreadLocalRandom.current().nextDouble(); + if (rand < nullProbability) { + return null; } - private StringBuilder toStringLayout(int level) { - StringBuilder guidelines = Utils.makeGuidelines(level); - StringBuilder sb = new StringBuilder().append(guidelines).append(content).append("\n"); - if (this.left == null && this.right == null) { - return sb; - } + Tree left = generate(maxDepth - 1, gen, nullProbability); + Tree right = generate(maxDepth - 1, gen, nullProbability); + return new Tree<>(left, gen.get(), right); + } - if (this.left != null) { - sb.append(this.left.toStringLayout(level + 1)); - } else { - sb.append(guidelines).append("\n"); - } + @Override + public String toString() { + return toStringLayout(0).toString(); + } - if (this.right != null) { - sb.append(this.right.toStringLayout(level + 1)); - } else { - sb.append(guidelines).append("\n"); - } - - return sb; + private StringBuilder toStringLayout(int level) { + StringBuilder sb = new StringBuilder().append(Utils.makeGuidelines(level)).append(content).append("\n"); + if (this.left == null && this.right == null) { + return sb; } + + StringBuilder nullChildGuidelines = Utils.makeGuidelines(level + 1); + if (this.left != null) { + sb.append(this.left.toStringLayout(level + 1)); + } else { + sb.append(nullChildGuidelines).append("\n"); + } + + if (this.right != null) { + sb.append(this.right.toStringLayout(level + 1)); + } else { + sb.append(nullChildGuidelines).append("\n"); + } + + return sb; + } } class InOrderIterator implements Iterator { - private static class Cont { - final Tree tree; - final Cont next; + private static class Cont { + final Tree tree; + final Cont next; - Cont(Tree tree, Cont next) { - this.tree = tree; - this.next = next; - } + Cont(Tree tree, Cont next) { + this.tree = tree; + this.next = next; } + } - static void printRecursive(Tree tree) { - if (tree != null) { - printRecursive(tree.left); - Utils.printContent(tree.content); - printRecursive(tree.right); - } + static void printRecursive(Tree tree) { + if (tree != null) { + printRecursive(tree.left); + Utils.printContent(tree.content); + printRecursive(tree.right); } + } - static void iterateRecursive(Tree tree, Consumer action) { - if (tree != null) { - iterateRecursive(tree.left, action); - action.accept(tree.content); - iterateRecursive(tree.right, action); - } + static void iterateRecursive(Tree tree, Consumer action) { + if (tree != null) { + iterateRecursive(tree.left, action); + action.accept(tree.content); + iterateRecursive(tree.right, action); } + } - static void iterateCPS(Tree tree, Consumer action, Runnable cont) { - if (tree != null) { - iterateCPS(tree.left, action, () -> { - action.accept(tree.content); - iterateCPS(tree.right, action, cont); - }); + static void iterateCPS(Tree tree, Consumer action, Runnable cont) { + if (tree != null) { + iterateCPS(tree.left, action, () -> { + action.accept(tree.content); + iterateCPS(tree.right, action, cont); + }); + } else { + cont.run(); + } + } + + static void iterateDefCPS(Tree tree, Consumer action, Cont cont) { + if (tree != null) { + iterateDefCPS(tree.left, action, new Cont<>(tree, cont)); + } else { + if (cont != null) { + action.accept(cont.tree.content); + iterateDefCPS(cont.tree.right, action, cont.next); + } else { + return; + } + } + } + + static void iterate(Tree tree, Consumer action, Cont cont) { + while (true) { + if (tree != null) { + cont = new Cont<>(tree, cont); + tree = tree.left; + } else { + if (cont != null) { + action.accept(cont.tree.content); + tree = cont.tree.right; + cont = cont.next; } else { - cont.run(); + return; } + } } + } - static void iterateDefCPS(Tree tree, Consumer action, Cont cont) { - if (tree != null) { - iterateDefCPS(tree.left, action, new Cont<>(tree, cont)); - } else { - if (cont != null) { - action.accept(cont.tree.content); - iterateDefCPS(cont.tree.right, action, cont.next); - } else { - return; - } + private Tree tree; + private Stack> stack = new Stack<>(); + + InOrderIterator(Tree tree) { + this.tree = tree; + } + + @Override + public boolean hasNext() { + return tree != null || !stack.isEmpty(); + } + + @Override + public T next() { + while (hasNext()) { + if (tree != null) { + stack.push(tree); + tree = tree.left; + } else { + if (!stack.isEmpty()) { + Tree t = stack.pop(); + T content = t.content; + tree = t.right; + return content; } + } } - - static void iterate(Tree tree, Consumer action, Cont cont) { - while (true) { - if (tree != null) { - cont = new Cont<>(tree, cont); - tree = tree.left; - } else { - if (cont != null) { - action.accept(cont.tree.content); - tree = cont.tree.right; - cont = cont.next; - } else { - return; - } - } - } - } - - private Tree tree; - private Stack> stack = new Stack<>(); - - InOrderIterator(Tree tree) { - this.tree = tree; - } - - @Override - public boolean hasNext() { - return tree != null || !stack.isEmpty(); - } - - @Override - public T next() { - while (hasNext()) { - if (tree != null) { - stack.push(tree); - tree = tree.left; - } else { - if (!stack.isEmpty()) { - Tree t = stack.pop(); - T content = t.content; - tree = t.right; - return content; - } - } - } - throw new NoSuchElementException(); - } + throw new NoSuchElementException(); + } } class PreOrderIterator implements Iterator { - private static class Cont { - final Tree tree; - final Cont next; + private static class Cont { + final Tree tree; + final Cont next; - Cont(Tree tree, Cont next) { - this.tree = tree; - this.next = next; - } + Cont(Tree tree, Cont next) { + this.tree = tree; + this.next = next; } + } - static void printRecursive(Tree tree) { - if (tree != null) { - Utils.printContent(tree.content); - printRecursive(tree.left); - printRecursive(tree.right); - } + static void printRecursive(Tree tree) { + if (tree != null) { + Utils.printContent(tree.content); + printRecursive(tree.left); + printRecursive(tree.right); } + } - static void iterateRecursive(Tree tree, Consumer action) { - if (tree != null) { - action.accept(tree.content); - iterateRecursive(tree.left, action); - iterateRecursive(tree.right, action); - } + static void iterateRecursive(Tree tree, Consumer action) { + if (tree != null) { + action.accept(tree.content); + iterateRecursive(tree.left, action); + iterateRecursive(tree.right, action); } + } - static void iterateCPS(Tree tree, Consumer action, Runnable cont) { - if (tree != null) { - action.accept(tree.content); - iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, cont)); + static void iterateCPS(Tree tree, Consumer action, Runnable cont) { + if (tree != null) { + action.accept(tree.content); + iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, cont)); + } else { + cont.run(); + } + } + + static void iterateDefCPS(Tree tree, Consumer action, Cont cont) { + if (tree != null) { + action.accept(tree.content); + iterateDefCPS(tree.left, action, new Cont<>(tree.right, cont)); + } else { + if (cont != null) { + iterateDefCPS(cont.tree, action, cont.next); + } else { + return; + } + } + } + + static void iterate(Tree tree, Consumer action, Cont cont) { + while (true) { + if (tree != null) { + action.accept(tree.content); + cont = new Cont<>(tree.right, cont); + tree = tree.left; + } else { + if (cont != null) { + tree = cont.tree; + cont = cont.next; } else { - cont.run(); + return; } + } } + } - static void iterateDefCPS(Tree tree, Consumer action, Cont cont) { - if (tree != null) { - action.accept(tree.content); - iterateDefCPS(tree.left, action, tree.right != null ? new Cont<>(tree.right, cont) : cont); - } else { - if (cont != null) { - iterateDefCPS(cont.tree, action, cont.next); - } else { - return; - } + private Tree tree; + private Stack> stack = new Stack<>(); + + PreOrderIterator(Tree tree) { + this.tree = tree; + } + + @Override + public boolean hasNext() { + return tree != null || !stack.isEmpty(); + } + + @Override + public T next() { + while (hasNext()) { + if (tree != null) { + T content = tree.content; + if (tree.right != null) { + stack.push(tree.right); } - } - - static void iterate(Tree tree, Consumer action, Cont cont) { - while (true) { - if (tree != null) { - action.accept(tree.content); - if (tree.right != null) { - cont = new Cont<>(tree.right, cont); - } - tree = tree.left; - } else { - if (cont != null) { - tree = cont.tree; - cont = cont.next; - } else { - return; - } - } + tree = tree.left; + return content; + } else { + if (!stack.isEmpty()) { + tree = stack.pop(); } + } } - - private Tree tree; - private Stack> stack = new Stack<>(); - - PreOrderIterator(Tree tree) { - this.tree = tree; - } - - @Override - public boolean hasNext() { - return tree != null || !stack.isEmpty(); - } - - @Override - public T next() { - while (hasNext()) { - if (tree != null) { - T content = tree.content; - if (tree.right != null) { - stack.push(tree.right); - } - tree = tree.left; - return content; - } else { - if (!stack.isEmpty()) { - tree = stack.pop(); - } - } - } - throw new NoSuchElementException(); - } + throw new NoSuchElementException(); + } } class PostOrderIterator implements Iterator { - static class Cont { - final Tree tree; - final boolean isRight; - final Cont next; + static class Cont { + final Tree tree; + final boolean isLeft; + final Cont next; - Cont(Tree tree, boolean isRight, Cont next) { - this.tree = tree; - this.next = next; - this.isRight = isRight; - } + Cont(Tree tree, boolean isLeft, Cont next) { + this.tree = tree; + this.next = next; + this.isLeft = isLeft; } + } - static void printRecursive(Tree tree) { - if (tree != null) { - printRecursive(tree.left); - printRecursive(tree.right); - Utils.printContent(tree.content); - } + static void printRecursive(Tree tree) { + if (tree != null) { + printRecursive(tree.left); + printRecursive(tree.right); + Utils.printContent(tree.content); } + } - static void iterateRecursive(Tree tree, Consumer action) { - if (tree != null) { - iterateRecursive(tree.left, action); - iterateRecursive(tree.right, action); - action.accept(tree.content); - } + static void iterateRecursive(Tree tree, Consumer action) { + if (tree != null) { + iterateRecursive(tree.left, action); + iterateRecursive(tree.right, action); + action.accept(tree.content); } + } - static void iterateCPS(Tree tree, Consumer action, Runnable cont) { - if (tree != null) { - iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, () -> { - action.accept(tree.content); - cont.run(); - })); + static void iterateCPS(Tree tree, Consumer action, Runnable cont) { + if (tree != null) { + iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, () -> { + action.accept(tree.content); + cont.run(); + })); + } else { + cont.run(); + } + } + + + static void iterateDefCPS(Tree tree, Consumer action, Cont cont) { + if (tree != null) { + Cont rCont = new Cont<>(tree, false, cont); + Cont lCont = new Cont<>(tree.right, true, rCont); + iterateDefCPS(tree.left, action, lCont); + } else { + if (cont != null) { + if (cont.isLeft) { + iterateDefCPS(cont.tree, action, cont.next); } else { - cont.run(); + action.accept(cont.tree.content); + iterateDefCPS(null, action, cont.next); } + } else { + return; + } } + } - - static void iterateDefCPS(Tree tree, Consumer action, Cont cont) { - if (tree != null) { - cont = new Cont<>(tree, false, cont); - iterateDefCPS(tree.left, action, tree.right != null ? new Cont<>(tree.right, true, cont) : cont); + static void iterate(Tree tree, Consumer action, Cont cont) { + while (true) { + if (tree != null) { + cont = new Cont<>(tree, false, cont); + cont = new Cont<>(tree.right, true, cont); + tree = tree.left; + } else { + if (cont != null) { + if (cont.isLeft) { + tree = cont.tree; + } else { + action.accept(cont.tree.content); + tree = null; + } + cont = cont.next; } else { - if (cont != null) { - if (!cont.isRight) { - action.accept(cont.tree.content); - } - iterateDefCPS(cont.isRight ? cont.tree : null, action, cont.next); - } else { - return; - } + return; } + } } + } - static void iterate(Tree tree, Consumer action, Cont cont) { - while (true) { - if (tree != null) { - cont = new Cont<>(tree, false, cont); - cont = tree.right != null ? new Cont<>(tree.right, true, cont) : cont; - tree = tree.left; - } else { - if (cont != null) { - if (!cont.isRight) { - action.accept(cont.tree.content); - tree = null; - } else { - tree = cont.tree; - } - cont = cont.next; - } else { - return; - } - } + private static class TreeTup { + final Tree tree; + final boolean isLeft; + + public TreeTup(Tree tree, boolean isLeft) { + this.tree = tree; + this.isLeft = isLeft; + } + } + + private Tree tree; + private Stack> stack = new Stack<>(); + + PostOrderIterator(Tree tree) { + this.tree = tree; + } + + @Override + public boolean hasNext() { + return tree != null || !stack.isEmpty(); + } + + @Override + public T next() { + while (hasNext()) { + if (tree != null) { + stack.push(new TreeTup<>(tree, false)); + if (tree.right != null) { + stack.push(new TreeTup<>(tree.right, true)); } - } - - private static class TreeTup { - final Tree tree; - final boolean isRight; - - public TreeTup(Tree tree, boolean isRight) { - this.tree = tree; - this.isRight = isRight; + tree = tree.left; + } else { + if (!stack.isEmpty()) { + TreeTup tup = stack.pop(); + if (tup.isLeft) { + tree = tup.tree; + } else { + T content = tup.tree.content; + tree = null; + return content; + } } + } } - - private Tree tree; - private Stack> stack = new Stack<>(); - - PostOrderIterator(Tree tree) { - this.tree = tree; - } - - @Override - public boolean hasNext() { - return tree != null || !stack.isEmpty(); - } - - @Override - public T next() { - while (hasNext()) { - if (tree != null) { - stack.push(new TreeTup<>(tree, false)); - if (tree.right != null) { - stack.push(new TreeTup<>(tree.right, true)); - } - tree = tree.left; - } else { - if (!stack.isEmpty()) { - TreeTup tup = stack.pop(); - if (!tup.isRight) { - T content = tup.tree.content; - tree = null; - return content; - } else { - tree = tup.tree; - } - } - } - } - throw new NoSuchElementException(); - } + throw new NoSuchElementException(); + } } class Utils { - static void printContent(T t) { - System.out.printf(t + " "); - } + static void printContent(T t) { + System.out.printf(t + " "); + } - static String generateRandomAlphaString(int maxLength) { - ThreadLocalRandom random = ThreadLocalRandom.current(); - int targetLength = random.nextInt(maxLength) + 1; - StringBuilder sb = new StringBuilder(targetLength); - for (int i = 0; i < targetLength; i++) { - sb.append((char) random.nextInt('a', 'z' + 1)); - } - return sb.toString(); + static String generateRandomAlphaString(int maxLength) { + ThreadLocalRandom random = ThreadLocalRandom.current(); + int targetLength = random.nextInt(maxLength) + 1; + StringBuilder sb = new StringBuilder(targetLength); + for (int i = 0; i < targetLength; i++) { + sb.append((char) random.nextInt('a', 'z' + 1)); } + return sb.toString(); + } - static StringBuilder makeGuidelines(int times) { - StringBuilder sb = new StringBuilder(); - for (int i = 0; i < times; i++) { - sb.append('│'); - } - return sb.append("├ "); + static StringBuilder makeGuidelines(int times) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < times; i++) { + sb.append("│ "); } + return sb.append("├ "); + } } \ No newline at end of file