Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Margus Binary Tree Node
- public static class MBTN<T extends Comparable<T>> implements Comparable<T>,
- Iterable<MBTN<T>>, Cloneable, Serializable {
- private static final long serialVersionUID = 1L;
- private T _value = null;
- private MBTN<T> _left = null;
- private MBTN<T> _right = null;
- public MBTN(T value) {
- synchronized (this) {
- this._value = value;
- }
- }
- public MBTN(T value, T left, T right) {
- synchronized (this) {
- this._value = value;
- this.setLeft(left);
- this.setRight(right);
- }
- }
- public MBTN(T... args) {
- if (args[0] == null)
- return;
- synchronized (this) {
- int counter = 0;
- this._value = args[counter++];
- Stack<MBTN<T>> nodes = new Stack<MBTN<T>>();
- nodes.push(this);
- MBTN<T> currentNode = null;
- while (!nodes.isEmpty() && args.length > counter) {
- currentNode = nodes.pop();
- if (args[counter] != null)
- nodes.add(0, currentNode.setLeft(args[counter++])
- .getLeft());
- if (args.length > counter && args[counter] != null)
- nodes.add(0, currentNode.setRight(args[counter++])
- .getRight());
- }
- }
- int r = 0;
- }
- public MBTN<T> clone() {
- try {
- @SuppressWarnings("unchecked")
- MBTN<T> cloned = (MBTN<T>) super.clone();
- cloned.setLeft(this._left);
- cloned.setRight(this._right);
- return cloned;
- } catch (CloneNotSupportedException e) {
- e.printStackTrace();
- return null;
- }
- }
- public MBTN<T> get() {
- return this;
- }
- public MBTN<T> set(T value) {
- synchronized (this) {
- this._value = value;
- }
- return this;
- }
- public void removeAll() {
- removeLeft();
- removeRight();
- }
- public void removeLeft() {
- synchronized (this) {
- if (this._left != null) {
- this._left.removeAll();
- this._left = null;
- }
- }
- }
- public void removeRight() {
- synchronized (this) {
- if (this._right != null) {
- this._right.removeAll();
- this._right = null;
- }
- }
- }
- public MBTN<T> getLeft() {
- MBTN<T> result = null;
- synchronized (this) {
- result = this._left;
- }
- return result;
- }
- public MBTN<T> setLeft(T element) {
- synchronized (this) {
- if (this._left != null)
- this._left.removeAll();
- this._left = new MBTN<T>(element);
- }
- return this;
- }
- public void setLeft(MBTN<T> node) {
- synchronized (this) {
- if (this._left != null)
- this._left.removeAll();
- if (node == null)
- this._left = null;
- else
- this._left = node;
- }
- }
- public MBTN<T> getRight() {
- MBTN<T> result = null;
- synchronized (this) {
- result = this._right;
- }
- return result;
- }
- public MBTN<T> setRight(T element) {
- synchronized (this) {
- if (this._right != null)
- this._right.removeAll();
- this._right = new MBTN<T>(element);
- }
- return this;
- }
- public void setRight(MBTN<T> node) {
- synchronized (this) {
- if (this._right != null)
- this._right.removeAll();
- if (node == null)
- this._right = null;
- else
- this._right = node;
- }
- }
- @Override
- public int compareTo(T val) {
- int result = 0;
- synchronized (this) {
- result = this._value.compareTo(val);
- }
- return result;
- }
- public String toString() {
- StringBuilder sb = new StringBuilder();
- synchronized (this) {
- if (this._left == null && this._left == null) {
- if (this._value != null)
- sb.append(this._value);
- } else {
- sb.append("(");
- if (this._value != null)
- sb.append(this._value);
- sb.append("|");
- if (this._left != null)
- sb.append(this._left);
- sb.append("-");
- if (this._right != null)
- sb.append(this._right);
- sb.append(")");
- }
- }
- return sb.toString();
- }
- public T getValue() {
- return _value;
- }
- public void setValue(T _value) {
- this._value = _value;
- }
- public void printTreeInorder() {
- System.out
- .println(printTreeInorder(new StringBuilder()).toString());
- }
- private StringBuilder printTreeInorder(StringBuilder sb) {
- synchronized (this) {
- if (this._left != null)
- this._left.printTreeInorder(sb).append(" ");
- if (this._value != null)
- sb.append(this._value).append(" ");
- if (this._right != null)
- this._right.printTreeInorder(sb).append(" ");
- }
- return sb;
- }
- public void printTreePostorder() {
- System.out.println(printTreePostorder(new StringBuilder())
- .toString());
- }
- private StringBuilder printTreePostorder(StringBuilder sb) {
- synchronized (this) {
- if (this._left != null)
- this._left.printTreeInorder(sb).append(" ");
- if (this._right != null)
- this._right.printTreeInorder(sb).append(" ");
- if (this._value != null)
- sb.append(this._value).append(" ");
- }
- return sb;
- }
- public boolean lookup(T value) {
- if (value == null)
- return false;
- synchronized (this) {
- if (this._value != null)
- if (this._value.compareTo(value) == 0)
- return true;
- if (this._left != null)
- if (this._left.lookup(value))
- return true;
- if (this._right != null)
- if (this._right.lookup(value))
- return true;
- }
- return false;
- }
- public int size() {
- int result = 0;
- synchronized (this) {
- if (this._value != null)
- result++;
- if (this._left != null)
- result += this._left.size();
- if (this._right != null)
- result += this._right.size();
- }
- return result;
- }
- public int maxDepth() {
- int result = 0, maxDepthLeft = 0, maxDepthRight = 0;
- synchronized (this) {
- if (this._value != null)
- result++;
- if (this._left != null)
- maxDepthLeft = this._left.size();
- if (this._right != null)
- maxDepthRight = this._right.size();
- result += Math.max(maxDepthLeft, maxDepthRight);
- }
- return result;
- }
- public void mirror() {
- synchronized (this) {
- MBTN<T> tmp = this._left;
- this._left = (this._right != null ? this._right : null);
- this._right = (tmp != null ? tmp : null);
- }
- }
- public synchronized ArrayList<MBTN<T>> toArrayList() {
- ArrayList<MBTN<T>> result = new ArrayList<MBTN<T>>();
- Stack<MBTN<T>> nodes = new Stack<MBTN<T>>();
- nodes.push(this);
- MBTN<T> currentNode = null, tmp = null;
- synchronized (this) {
- while (!nodes.isEmpty()) {
- result.add(currentNode = nodes.pop());
- if ((tmp = currentNode.getRight()) != null)
- nodes.push(tmp);
- if ((tmp = currentNode.getLeft()) != null)
- nodes.push(tmp);
- }
- }
- result.add(this);
- return result;
- }
- public void mirrorAll() {
- mirror();
- synchronized (this) {
- if (this._left != null)
- this._left.mirrorAll();
- if (this._right != null)
- this._right.mirrorAll();
- }
- }
- @Override
- public Iterator<MBTN<T>> iterator() {
- return toArrayList().iterator();
- }
- static class Cmp<T extends Comparable<T>> implements
- Comparator<MBTN<T>> {
- @Override
- public int compare(MBTN<T> paramT1, MBTN<T> paramT2) {
- return paramT1.getValue().compareTo(paramT2.getValue());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement