|
| 1 | +class Zipper { |
| 2 | + |
| 3 | + private int value; |
| 4 | + BinaryTree tree; // Only the root node will bind to the BinaryTree |
| 5 | + Zipper up; |
| 6 | + Zipper left; |
| 7 | + Zipper right; |
| 8 | + |
| 9 | + Zipper(int val) { |
| 10 | + value = val; |
| 11 | + tree = null; |
| 12 | + up = null; |
| 13 | + left = null; |
| 14 | + right = null; |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Get a zipper out of a rose tree, the focus is on the root node |
| 19 | + */ |
| 20 | + Zipper fromTree() { |
| 21 | + Zipper current = this; |
| 22 | + while (current.up != null) { |
| 23 | + current = current.up; |
| 24 | + } |
| 25 | + return current; |
| 26 | + } |
| 27 | + |
| 28 | + BinaryTree toTree() { |
| 29 | + Zipper current = this; |
| 30 | + while (current.up != null) { |
| 31 | + current = current.up; |
| 32 | + } |
| 33 | + return current.tree; |
| 34 | + } |
| 35 | + |
| 36 | + int getValue() { |
| 37 | + return value; |
| 38 | + } |
| 39 | + |
| 40 | + Zipper setLeft(Zipper leftChild) { |
| 41 | + left = leftChild; |
| 42 | + if (leftChild != null) { |
| 43 | + leftChild.up = this; |
| 44 | + leftChild.tree = null; |
| 45 | + } |
| 46 | + return this; |
| 47 | + } |
| 48 | + |
| 49 | + Zipper setRight(Zipper rightChild) { |
| 50 | + right = rightChild; |
| 51 | + if (rightChild != null) { |
| 52 | + rightChild.up = this; |
| 53 | + rightChild.tree = null; |
| 54 | + } |
| 55 | + return this; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Move the focus to the previous child of the same parent |
| 60 | + * @return a new Zipper |
| 61 | + * Conditions: 1. The current Zipper must have a parent, i.e. it is not the root node. |
| 62 | + * 2. The current Zipper must be the right child of its parent |
| 63 | + */ |
| 64 | + Zipper getPrev() { |
| 65 | + Zipper current = this; |
| 66 | + try { |
| 67 | + current = this.up; |
| 68 | + current = current.left; |
| 69 | + return current; |
| 70 | + } catch (Exception e) { |
| 71 | + System.out.println(e); |
| 72 | + } |
| 73 | + return null; |
| 74 | + } |
| 75 | + |
| 76 | + /** |
| 77 | + * Move the focus to the next child of the same parent |
| 78 | + * @return a new Zipper |
| 79 | + * Conditions: 1. The current Zipper must have a parent, i.e. it is not the root node. |
| 80 | + * 2. The current Zipper must be the left child of its parent |
| 81 | + */ |
| 82 | + Zipper getNext() { |
| 83 | + Zipper current = this; |
| 84 | + try { |
| 85 | + current = this.up; |
| 86 | + current = current.right; |
| 87 | + return current; |
| 88 | + } catch (Exception e) { |
| 89 | + System.out.println(e); |
| 90 | + } |
| 91 | + return null; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Move the focus to the parent |
| 96 | + * @return a new Zipper |
| 97 | + */ |
| 98 | + Zipper getUp() { |
| 99 | + return up; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * Set the value of the focus node, returns a new zipper |
| 104 | + * @param val |
| 105 | + */ |
| 106 | + void setValue(int val) { |
| 107 | + this.value = val; |
| 108 | + } |
| 109 | + |
| 110 | + /** |
| 111 | + * Insert a new subtree before the focus node, it becomes the prev of the focus node |
| 112 | + * @return a new Zipper |
| 113 | + */ |
| 114 | + Zipper insertBefore(Zipper toTheLeft) { |
| 115 | + this.left = toTheLeft; |
| 116 | + return this.left; |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * Insert a new subtree after the focus node, it becomes the next of the focus node |
| 121 | + * @return a new Zipper |
| 122 | + */ |
| 123 | + Zipper insertAfter(Zipper toTheRight) { |
| 124 | + this.right = toTheRight; |
| 125 | + return this.right; |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Removes the focus node and all subtrees |
| 130 | + * Focus moves to the next node if possible otherwise to the prev node if possible, otherwise to the parent node |
| 131 | + * @return a new zipper |
| 132 | + */ |
| 133 | + Zipper delete() { |
| 134 | + Zipper current = this; |
| 135 | + if (current.up == null) { |
| 136 | + current = null; |
| 137 | + } else { |
| 138 | + if (current.up.left == current) { |
| 139 | + current = current.up; |
| 140 | + current.left = null; |
| 141 | + if (current.right != null) { |
| 142 | + current = current.right; |
| 143 | + } |
| 144 | + } else { |
| 145 | + current = current.up; |
| 146 | + current.right = null; |
| 147 | + if (current.left != null) { |
| 148 | + current = current.left; |
| 149 | + } |
| 150 | + } |
| 151 | + } |
| 152 | + return current; |
| 153 | + } |
| 154 | + |
| 155 | +} |
| 156 | + |
| 157 | +class BinaryTree { |
| 158 | + |
| 159 | + private Zipper root; |
| 160 | + |
| 161 | + BinaryTree(int value) { |
| 162 | + root = new Zipper(value); |
| 163 | + root.tree = this; |
| 164 | + } |
| 165 | + |
| 166 | + BinaryTree(Zipper root) { |
| 167 | + this.root = root; |
| 168 | + this.root.tree = this; |
| 169 | + } |
| 170 | + |
| 171 | + Zipper getRoot() { |
| 172 | + return root; |
| 173 | + } |
| 174 | + |
| 175 | + void setRoot(Zipper zipper) { |
| 176 | + root = zipper; |
| 177 | + root.tree = this; |
| 178 | + } |
| 179 | + |
| 180 | + String printTree(Zipper zipper) { |
| 181 | + String str = ""; |
| 182 | + if (zipper.left == null && zipper.right == null) { |
| 183 | + str += "value: " + zipper.getValue() + ", left: " + null + ", right: " + null; |
| 184 | + } else if (zipper.left == null) { |
| 185 | + str += "value: " + zipper.getValue() + ", left: " + null + ", right: { " + printTree(zipper.right) + " }"; |
| 186 | + } else if (zipper.right == null) { |
| 187 | + str += "value: " + zipper.getValue() + ", left: { " + printTree(zipper.left) + " }, right: " + null; |
| 188 | + } else { |
| 189 | + str += "value: " + zipper.getValue() + |
| 190 | + ", left: { " + printTree(zipper.left) + " }, right: { " + printTree(zipper.right) + " }"; |
| 191 | + } |
| 192 | + return str; |
| 193 | + } |
| 194 | + |
| 195 | + String printTree() { |
| 196 | + return printTree(root); |
| 197 | + } |
| 198 | + |
| 199 | +} |
0 commit comments