Minor changes + reformatting

This commit is contained in:
Abhinav Sarkar 2019-08-09 15:31:44 +05:30
parent a68a6522c2
commit 6048270224

View File

@ -8,101 +8,101 @@ import java.util.function.Consumer;
import java.util.function.Supplier;
class TreeIerators {
public static void main(String[] args) {
Tree<String> tree;
do {
tree = Tree.generate(4, () -> Utils.generateRandomAlphaString(2), 0.1);
} while (tree == null);
public static void main(String[] args) {
Tree<String> 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<String> 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<String> 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<String> postOrderIterator = new PostOrderIterator<>(tree);
while (postOrderIterator.hasNext()) {
Utils.printContent(postOrderIterator.next());
}
System.out.println("\tPostOrderIterator");
InOrderIterator<String> 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<String> 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<String> 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
<NULL>
l
b
qc
g
rp
d
o
j
x
e
m
vz
g
<NULL>
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<T> {
Tree<T> left;
T content;
Tree<T> right;
Tree<T> left;
T content;
Tree<T> right;
Tree(Tree<T> left, T content, Tree<T> right) {
this.left = left;
this.content = content;
this.right = right;
Tree(Tree<T> left, T content, Tree<T> right) {
this.left = left;
this.content = content;
this.right = right;
}
public static <T> Tree<T> generate(int maxDepth, Supplier<T> gen, double nullProbability) {
if (nullProbability < 0 || nullProbability > 1) {
throw new IllegalArgumentException("nullProbability must be between 0 and 1");
}
public static <T> Tree<T> generate(int maxDepth, Supplier<T> 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<T> left = generate(maxDepth - 1, gen, nullProbability);
Tree<T> 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<T> left = generate(maxDepth - 1, gen, nullProbability);
Tree<T> 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("<NULL>\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("<NULL>\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("<NULL>\n");
}
if (this.right != null) {
sb.append(this.right.toStringLayout(level + 1));
} else {
sb.append(nullChildGuidelines).append("<NULL>\n");
}
return sb;
}
}
class InOrderIterator<T> implements Iterator<T> {
private static class Cont<T> {
final Tree<T> tree;
final Cont<T> next;
private static class Cont<T> {
final Tree<T> tree;
final Cont<T> next;
Cont(Tree<T> tree, Cont<T> next) {
this.tree = tree;
this.next = next;
}
Cont(Tree<T> tree, Cont<T> next) {
this.tree = tree;
this.next = next;
}
}
static <T> void printRecursive(Tree<T> tree) {
if (tree != null) {
printRecursive(tree.left);
Utils.printContent(tree.content);
printRecursive(tree.right);
}
static <T> void printRecursive(Tree<T> tree) {
if (tree != null) {
printRecursive(tree.left);
Utils.printContent(tree.content);
printRecursive(tree.right);
}
}
static <T> void iterateRecursive(Tree<T> tree, Consumer<T> action) {
if (tree != null) {
iterateRecursive(tree.left, action);
action.accept(tree.content);
iterateRecursive(tree.right, action);
}
static <T> void iterateRecursive(Tree<T> tree, Consumer<T> action) {
if (tree != null) {
iterateRecursive(tree.left, action);
action.accept(tree.content);
iterateRecursive(tree.right, action);
}
}
static <T> void iterateCPS(Tree<T> tree, Consumer<T> action, Runnable cont) {
if (tree != null) {
iterateCPS(tree.left, action, () -> {
action.accept(tree.content);
iterateCPS(tree.right, action, cont);
});
static <T> void iterateCPS(Tree<T> tree, Consumer<T> action, Runnable cont) {
if (tree != null) {
iterateCPS(tree.left, action, () -> {
action.accept(tree.content);
iterateCPS(tree.right, action, cont);
});
} else {
cont.run();
}
}
static <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> 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 <T> void iterate(Tree<T> tree, Consumer<T> action, Cont<T> 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 <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> 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<T> tree;
private Stack<Tree<T>> stack = new Stack<>();
InOrderIterator(Tree<T> 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> t = stack.pop();
T content = t.content;
tree = t.right;
return content;
}
}
}
static <T> void iterate(Tree<T> tree, Consumer<T> action, Cont<T> 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<T> tree;
private Stack<Tree<T>> stack = new Stack<>();
InOrderIterator(Tree<T> 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> t = stack.pop();
T content = t.content;
tree = t.right;
return content;
}
}
}
throw new NoSuchElementException();
}
throw new NoSuchElementException();
}
}
class PreOrderIterator<T> implements Iterator<T> {
private static class Cont<T> {
final Tree<T> tree;
final Cont<T> next;
private static class Cont<T> {
final Tree<T> tree;
final Cont<T> next;
Cont(Tree<T> tree, Cont<T> next) {
this.tree = tree;
this.next = next;
}
Cont(Tree<T> tree, Cont<T> next) {
this.tree = tree;
this.next = next;
}
}
static <T> void printRecursive(Tree<T> tree) {
if (tree != null) {
Utils.printContent(tree.content);
printRecursive(tree.left);
printRecursive(tree.right);
}
static <T> void printRecursive(Tree<T> tree) {
if (tree != null) {
Utils.printContent(tree.content);
printRecursive(tree.left);
printRecursive(tree.right);
}
}
static <T> void iterateRecursive(Tree<T> tree, Consumer<T> action) {
if (tree != null) {
action.accept(tree.content);
iterateRecursive(tree.left, action);
iterateRecursive(tree.right, action);
}
static <T> void iterateRecursive(Tree<T> tree, Consumer<T> action) {
if (tree != null) {
action.accept(tree.content);
iterateRecursive(tree.left, action);
iterateRecursive(tree.right, action);
}
}
static <T> void iterateCPS(Tree<T> tree, Consumer<T> action, Runnable cont) {
if (tree != null) {
action.accept(tree.content);
iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, cont));
static <T> void iterateCPS(Tree<T> tree, Consumer<T> action, Runnable cont) {
if (tree != null) {
action.accept(tree.content);
iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, cont));
} else {
cont.run();
}
}
static <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> 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 <T> void iterate(Tree<T> tree, Consumer<T> action, Cont<T> 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 <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> 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<T> tree;
private Stack<Tree<T>> stack = new Stack<>();
PreOrderIterator(Tree<T> 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 <T> void iterate(Tree<T> tree, Consumer<T> action, Cont<T> 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<T> tree;
private Stack<Tree<T>> stack = new Stack<>();
PreOrderIterator(Tree<T> 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<T> implements Iterator<T> {
static class Cont<T> {
final Tree<T> tree;
final boolean isRight;
final Cont<T> next;
static class Cont<T> {
final Tree<T> tree;
final boolean isLeft;
final Cont<T> next;
Cont(Tree<T> tree, boolean isRight, Cont<T> next) {
this.tree = tree;
this.next = next;
this.isRight = isRight;
}
Cont(Tree<T> tree, boolean isLeft, Cont<T> next) {
this.tree = tree;
this.next = next;
this.isLeft = isLeft;
}
}
static <T> void printRecursive(Tree<T> tree) {
if (tree != null) {
printRecursive(tree.left);
printRecursive(tree.right);
Utils.printContent(tree.content);
}
static <T> void printRecursive(Tree<T> tree) {
if (tree != null) {
printRecursive(tree.left);
printRecursive(tree.right);
Utils.printContent(tree.content);
}
}
static <T> void iterateRecursive(Tree<T> tree, Consumer<T> action) {
if (tree != null) {
iterateRecursive(tree.left, action);
iterateRecursive(tree.right, action);
action.accept(tree.content);
}
static <T> void iterateRecursive(Tree<T> tree, Consumer<T> action) {
if (tree != null) {
iterateRecursive(tree.left, action);
iterateRecursive(tree.right, action);
action.accept(tree.content);
}
}
static <T> void iterateCPS(Tree<T> tree, Consumer<T> action, Runnable cont) {
if (tree != null) {
iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, () -> {
action.accept(tree.content);
cont.run();
}));
static <T> void iterateCPS(Tree<T> tree, Consumer<T> action, Runnable cont) {
if (tree != null) {
iterateCPS(tree.left, action, () -> iterateCPS(tree.right, action, () -> {
action.accept(tree.content);
cont.run();
}));
} else {
cont.run();
}
}
static <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> cont) {
if (tree != null) {
Cont<T> rCont = new Cont<>(tree, false, cont);
Cont<T> 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 <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> 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 <T> void iterate(Tree<T> tree, Consumer<T> action, Cont<T> 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 <T> void iterate(Tree<T> tree, Consumer<T> action, Cont<T> 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<T> {
final Tree<T> tree;
final boolean isLeft;
public TreeTup(Tree<T> tree, boolean isLeft) {
this.tree = tree;
this.isLeft = isLeft;
}
}
private Tree<T> tree;
private Stack<TreeTup<T>> stack = new Stack<>();
PostOrderIterator(Tree<T> 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<T> {
final Tree<T> tree;
final boolean isRight;
public TreeTup(Tree<T> tree, boolean isRight) {
this.tree = tree;
this.isRight = isRight;
tree = tree.left;
} else {
if (!stack.isEmpty()) {
TreeTup<T> tup = stack.pop();
if (tup.isLeft) {
tree = tup.tree;
} else {
T content = tup.tree.content;
tree = null;
return content;
}
}
}
}
private Tree<T> tree;
private Stack<TreeTup<T>> stack = new Stack<>();
PostOrderIterator(Tree<T> 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<T> 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 <T> void printContent(T t) {
System.out.printf(t + " ");
}
static <T> 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("");
}
}