Minor changes + reformatting

master
Abhinav Sarkar 2019-08-09 15:31:44 +05:30
parent a68a6522c2
commit 6048270224
1 changed files with 418 additions and 417 deletions

View File

@ -89,20 +89,20 @@ class TreeIerators {
/* 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
@ -163,22 +163,22 @@ class Tree<T> {
}
private StringBuilder toStringLayout(int level) {
StringBuilder guidelines = Utils.makeGuidelines(level);
StringBuilder sb = new StringBuilder().append(guidelines).append(content).append("\n");
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(guidelines).append("<NULL>\n");
sb.append(nullChildGuidelines).append("<NULL>\n");
}
if (this.right != null) {
sb.append(this.right.toStringLayout(level + 1));
} else {
sb.append(guidelines).append("<NULL>\n");
sb.append(nullChildGuidelines).append("<NULL>\n");
}
return sb;
@ -325,7 +325,7 @@ class PreOrderIterator<T> implements Iterator<T> {
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);
iterateDefCPS(tree.left, action, new Cont<>(tree.right, cont));
} else {
if (cont != null) {
iterateDefCPS(cont.tree, action, cont.next);
@ -339,9 +339,7 @@ class PreOrderIterator<T> implements Iterator<T> {
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) {
@ -391,13 +389,13 @@ class PostOrderIterator<T> implements Iterator<T> {
static class Cont<T> {
final Tree<T> tree;
final boolean isRight;
final boolean isLeft;
final Cont<T> next;
Cont(Tree<T> tree, boolean isRight, Cont<T> next) {
Cont(Tree<T> tree, boolean isLeft, Cont<T> next) {
this.tree = tree;
this.next = next;
this.isRight = isRight;
this.isLeft = isLeft;
}
}
@ -431,14 +429,17 @@ class PostOrderIterator<T> implements Iterator<T> {
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);
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.isRight) {
if (cont.isLeft) {
iterateDefCPS(cont.tree, action, cont.next);
} else {
action.accept(cont.tree.content);
iterateDefCPS(null, action, cont.next);
}
iterateDefCPS(cont.isRight ? cont.tree : null, action, cont.next);
} else {
return;
}
@ -449,15 +450,15 @@ class PostOrderIterator<T> implements Iterator<T> {
while (true) {
if (tree != null) {
cont = new Cont<>(tree, false, cont);
cont = tree.right != null ? new Cont<>(tree.right, true, cont) : cont;
cont = new Cont<>(tree.right, true, cont);
tree = tree.left;
} else {
if (cont != null) {
if (!cont.isRight) {
if (cont.isLeft) {
tree = cont.tree;
} else {
action.accept(cont.tree.content);
tree = null;
} else {
tree = cont.tree;
}
cont = cont.next;
} else {
@ -469,11 +470,11 @@ class PostOrderIterator<T> implements Iterator<T> {
private static class TreeTup<T> {
final Tree<T> tree;
final boolean isRight;
final boolean isLeft;
public TreeTup(Tree<T> tree, boolean isRight) {
public TreeTup(Tree<T> tree, boolean isLeft) {
this.tree = tree;
this.isRight = isRight;
this.isLeft = isLeft;
}
}
@ -501,12 +502,12 @@ class PostOrderIterator<T> implements Iterator<T> {
} else {
if (!stack.isEmpty()) {
TreeTup<T> tup = stack.pop();
if (!tup.isRight) {
if (tup.isLeft) {
tree = tup.tree;
} else {
T content = tup.tree.content;
tree = null;
return content;
} else {
tree = tup.tree;
}
}
}
@ -534,7 +535,7 @@ class Utils {
static StringBuilder makeGuidelines(int times) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++) {
sb.append('│');
sb.append("│ ");
}
return sb.append("├ ");
}