Minor changes + reformatting

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

View File

@ -89,20 +89,20 @@ class TreeIerators {
/* Run result: /* Run result:
Tree: Tree:
r r
j j
x x
e e
m m
vz vz
g g
<NULL> <NULL>
l l
b b
qc qc
g g
rp rp
d d
o 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.printRecursive
e x m j g vz r qc b g l d rp o InOrderIterator.iterateRecursive 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) { private StringBuilder toStringLayout(int level) {
StringBuilder guidelines = Utils.makeGuidelines(level); StringBuilder sb = new StringBuilder().append(Utils.makeGuidelines(level)).append(content).append("\n");
StringBuilder sb = new StringBuilder().append(guidelines).append(content).append("\n");
if (this.left == null && this.right == null) { if (this.left == null && this.right == null) {
return sb; return sb;
} }
StringBuilder nullChildGuidelines = Utils.makeGuidelines(level + 1);
if (this.left != null) { if (this.left != null) {
sb.append(this.left.toStringLayout(level + 1)); sb.append(this.left.toStringLayout(level + 1));
} else { } else {
sb.append(guidelines).append("<NULL>\n"); sb.append(nullChildGuidelines).append("<NULL>\n");
} }
if (this.right != null) { if (this.right != null) {
sb.append(this.right.toStringLayout(level + 1)); sb.append(this.right.toStringLayout(level + 1));
} else { } else {
sb.append(guidelines).append("<NULL>\n"); sb.append(nullChildGuidelines).append("<NULL>\n");
} }
return sb; 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) { static <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> cont) {
if (tree != null) { if (tree != null) {
action.accept(tree.content); 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 { } else {
if (cont != null) { if (cont != null) {
iterateDefCPS(cont.tree, action, cont.next); iterateDefCPS(cont.tree, action, cont.next);
@ -339,9 +339,7 @@ class PreOrderIterator<T> implements Iterator<T> {
while (true) { while (true) {
if (tree != null) { if (tree != null) {
action.accept(tree.content); action.accept(tree.content);
if (tree.right != null) {
cont = new Cont<>(tree.right, cont); cont = new Cont<>(tree.right, cont);
}
tree = tree.left; tree = tree.left;
} else { } else {
if (cont != null) { if (cont != null) {
@ -391,13 +389,13 @@ class PostOrderIterator<T> implements Iterator<T> {
static class Cont<T> { static class Cont<T> {
final Tree<T> tree; final Tree<T> tree;
final boolean isRight; final boolean isLeft;
final Cont<T> next; 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.tree = tree;
this.next = next; 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) { static <T> void iterateDefCPS(Tree<T> tree, Consumer<T> action, Cont<T> cont) {
if (tree != null) { if (tree != null) {
cont = new Cont<>(tree, false, cont); Cont<T> rCont = new Cont<>(tree, false, cont);
iterateDefCPS(tree.left, action, tree.right != null ? new Cont<>(tree.right, true, cont) : cont); Cont<T> lCont = new Cont<>(tree.right, true, rCont);
iterateDefCPS(tree.left, action, lCont);
} else { } else {
if (cont != null) { if (cont != null) {
if (!cont.isRight) { if (cont.isLeft) {
iterateDefCPS(cont.tree, action, cont.next);
} else {
action.accept(cont.tree.content); action.accept(cont.tree.content);
iterateDefCPS(null, action, cont.next);
} }
iterateDefCPS(cont.isRight ? cont.tree : null, action, cont.next);
} else { } else {
return; return;
} }
@ -449,15 +450,15 @@ class PostOrderIterator<T> implements Iterator<T> {
while (true) { while (true) {
if (tree != null) { if (tree != null) {
cont = new Cont<>(tree, false, cont); 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; tree = tree.left;
} else { } else {
if (cont != null) { if (cont != null) {
if (!cont.isRight) { if (cont.isLeft) {
tree = cont.tree;
} else {
action.accept(cont.tree.content); action.accept(cont.tree.content);
tree = null; tree = null;
} else {
tree = cont.tree;
} }
cont = cont.next; cont = cont.next;
} else { } else {
@ -469,11 +470,11 @@ class PostOrderIterator<T> implements Iterator<T> {
private static class TreeTup<T> { private static class TreeTup<T> {
final Tree<T> tree; 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.tree = tree;
this.isRight = isRight; this.isLeft = isLeft;
} }
} }
@ -501,12 +502,12 @@ class PostOrderIterator<T> implements Iterator<T> {
} else { } else {
if (!stack.isEmpty()) { if (!stack.isEmpty()) {
TreeTup<T> tup = stack.pop(); TreeTup<T> tup = stack.pop();
if (!tup.isRight) { if (tup.isLeft) {
tree = tup.tree;
} else {
T content = tup.tree.content; T content = tup.tree.content;
tree = null; tree = null;
return content; return content;
} else {
tree = tup.tree;
} }
} }
} }
@ -534,7 +535,7 @@ class Utils {
static StringBuilder makeGuidelines(int times) { static StringBuilder makeGuidelines(int times) {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < times; i++) { for (int i = 0; i < times; i++) {
sb.append('│'); sb.append("");
} }
return sb.append(""); return sb.append("");
} }