Advertisement
Guest User

Margus Binary Tree Node

a guest
Oct 14th, 2010
611
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.16 KB | None | 0 0
  1.     // Margus Binary Tree Node
  2.     public static class MBTN<T extends Comparable<T>> implements Comparable<T>,
  3.             Iterable<MBTN<T>>, Cloneable, Serializable {
  4.         private static final long serialVersionUID = 1L;
  5.         private T _value = null;
  6.         private MBTN<T> _left = null;
  7.         private MBTN<T> _right = null;
  8.  
  9.         public MBTN(T value) {
  10.             synchronized (this) {
  11.                 this._value = value;
  12.             }
  13.         }
  14.  
  15.         public MBTN(T value, T left, T right) {
  16.             synchronized (this) {
  17.                 this._value = value;
  18.                 this.setLeft(left);
  19.                 this.setRight(right);
  20.             }
  21.         }
  22.  
  23.         public MBTN(T... args) {
  24.             if (args[0] == null)
  25.                 return;
  26.             synchronized (this) {
  27.                 int counter = 0;
  28.                 this._value = args[counter++];
  29.  
  30.                 Stack<MBTN<T>> nodes = new Stack<MBTN<T>>();
  31.                 nodes.push(this);
  32.                 MBTN<T> currentNode = null;
  33.  
  34.                 while (!nodes.isEmpty() && args.length > counter) {
  35.                     currentNode = nodes.pop();
  36.  
  37.                     if (args[counter] != null)
  38.                         nodes.add(0, currentNode.setLeft(args[counter++])
  39.                                 .getLeft());
  40.                     if (args.length > counter && args[counter] != null)
  41.                         nodes.add(0, currentNode.setRight(args[counter++])
  42.                                 .getRight());
  43.                 }
  44.             }
  45.             int r = 0;
  46.         }
  47.  
  48.         public MBTN<T> clone() {
  49.             try {
  50.                 @SuppressWarnings("unchecked")
  51.                 MBTN<T> cloned = (MBTN<T>) super.clone();
  52.                 cloned.setLeft(this._left);
  53.                 cloned.setRight(this._right);
  54.                 return cloned;
  55.             } catch (CloneNotSupportedException e) {
  56.                 e.printStackTrace();
  57.                 return null;
  58.             }
  59.         }
  60.  
  61.         public MBTN<T> get() {
  62.             return this;
  63.         }
  64.  
  65.         public MBTN<T> set(T value) {
  66.             synchronized (this) {
  67.                 this._value = value;
  68.             }
  69.             return this;
  70.         }
  71.  
  72.         public void removeAll() {
  73.             removeLeft();
  74.             removeRight();
  75.         }
  76.  
  77.         public void removeLeft() {
  78.             synchronized (this) {
  79.                 if (this._left != null) {
  80.                     this._left.removeAll();
  81.                     this._left = null;
  82.                 }
  83.             }
  84.         }
  85.  
  86.         public void removeRight() {
  87.             synchronized (this) {
  88.                 if (this._right != null) {
  89.                     this._right.removeAll();
  90.                     this._right = null;
  91.                 }
  92.             }
  93.         }
  94.  
  95.         public MBTN<T> getLeft() {
  96.             MBTN<T> result = null;
  97.             synchronized (this) {
  98.                 result = this._left;
  99.             }
  100.             return result;
  101.         }
  102.  
  103.         public MBTN<T> setLeft(T element) {
  104.             synchronized (this) {
  105.                 if (this._left != null)
  106.                     this._left.removeAll();
  107.  
  108.                 this._left = new MBTN<T>(element);
  109.             }
  110.             return this;
  111.         }
  112.  
  113.         public void setLeft(MBTN<T> node) {
  114.             synchronized (this) {
  115.                 if (this._left != null)
  116.                     this._left.removeAll();
  117.                 if (node == null)
  118.                     this._left = null;
  119.                 else
  120.                     this._left = node;
  121.             }
  122.         }
  123.  
  124.         public MBTN<T> getRight() {
  125.             MBTN<T> result = null;
  126.             synchronized (this) {
  127.                 result = this._right;
  128.             }
  129.             return result;
  130.         }
  131.  
  132.         public MBTN<T> setRight(T element) {
  133.             synchronized (this) {
  134.                 if (this._right != null)
  135.                     this._right.removeAll();
  136.  
  137.                 this._right = new MBTN<T>(element);
  138.             }
  139.             return this;
  140.         }
  141.  
  142.         public void setRight(MBTN<T> node) {
  143.             synchronized (this) {
  144.                 if (this._right != null)
  145.                     this._right.removeAll();
  146.                 if (node == null)
  147.                     this._right = null;
  148.                 else
  149.                     this._right = node;
  150.             }
  151.         }
  152.  
  153.         @Override
  154.         public int compareTo(T val) {
  155.             int result = 0;
  156.             synchronized (this) {
  157.                 result = this._value.compareTo(val);
  158.             }
  159.             return result;
  160.         }
  161.  
  162.         public String toString() {
  163.             StringBuilder sb = new StringBuilder();
  164.             synchronized (this) {
  165.                 if (this._left == null && this._left == null) {
  166.                     if (this._value != null)
  167.                         sb.append(this._value);
  168.                 } else {
  169.                     sb.append("(");
  170.                     if (this._value != null)
  171.                         sb.append(this._value);
  172.                     sb.append("|");
  173.                     if (this._left != null)
  174.                         sb.append(this._left);
  175.                     sb.append("-");
  176.                     if (this._right != null)
  177.                         sb.append(this._right);
  178.                     sb.append(")");
  179.                 }
  180.             }
  181.             return sb.toString();
  182.         }
  183.  
  184.         public T getValue() {
  185.             return _value;
  186.         }
  187.  
  188.         public void setValue(T _value) {
  189.             this._value = _value;
  190.         }
  191.  
  192.         public void printTreeInorder() {
  193.             System.out
  194.                     .println(printTreeInorder(new StringBuilder()).toString());
  195.         }
  196.  
  197.         private StringBuilder printTreeInorder(StringBuilder sb) {
  198.             synchronized (this) {
  199.                 if (this._left != null)
  200.                     this._left.printTreeInorder(sb).append(" ");
  201.                 if (this._value != null)
  202.                     sb.append(this._value).append(" ");
  203.                 if (this._right != null)
  204.                     this._right.printTreeInorder(sb).append(" ");
  205.             }
  206.             return sb;
  207.         }
  208.  
  209.         public void printTreePostorder() {
  210.             System.out.println(printTreePostorder(new StringBuilder())
  211.                     .toString());
  212.         }
  213.  
  214.         private StringBuilder printTreePostorder(StringBuilder sb) {
  215.             synchronized (this) {
  216.                 if (this._left != null)
  217.                     this._left.printTreeInorder(sb).append(" ");
  218.                 if (this._right != null)
  219.                     this._right.printTreeInorder(sb).append(" ");
  220.                 if (this._value != null)
  221.                     sb.append(this._value).append(" ");
  222.             }
  223.             return sb;
  224.         }
  225.  
  226.         public boolean lookup(T value) {
  227.             if (value == null)
  228.                 return false;
  229.             synchronized (this) {
  230.                 if (this._value != null)
  231.                     if (this._value.compareTo(value) == 0)
  232.                         return true;
  233.                 if (this._left != null)
  234.                     if (this._left.lookup(value))
  235.                         return true;
  236.                 if (this._right != null)
  237.                     if (this._right.lookup(value))
  238.                         return true;
  239.             }
  240.             return false;
  241.         }
  242.  
  243.         public int size() {
  244.             int result = 0;
  245.             synchronized (this) {
  246.                 if (this._value != null)
  247.                     result++;
  248.                 if (this._left != null)
  249.                     result += this._left.size();
  250.                 if (this._right != null)
  251.                     result += this._right.size();
  252.             }
  253.             return result;
  254.         }
  255.  
  256.         public int maxDepth() {
  257.             int result = 0, maxDepthLeft = 0, maxDepthRight = 0;
  258.             synchronized (this) {
  259.                 if (this._value != null)
  260.                     result++;
  261.                 if (this._left != null)
  262.                     maxDepthLeft = this._left.size();
  263.                 if (this._right != null)
  264.                     maxDepthRight = this._right.size();
  265.                 result += Math.max(maxDepthLeft, maxDepthRight);
  266.             }
  267.             return result;
  268.         }
  269.  
  270.         public void mirror() {
  271.             synchronized (this) {
  272.                 MBTN<T> tmp = this._left;
  273.                 this._left = (this._right != null ? this._right : null);
  274.                 this._right = (tmp != null ? tmp : null);
  275.             }
  276.         }
  277.  
  278.         public synchronized ArrayList<MBTN<T>> toArrayList() {
  279.             ArrayList<MBTN<T>> result = new ArrayList<MBTN<T>>();
  280.  
  281.             Stack<MBTN<T>> nodes = new Stack<MBTN<T>>();
  282.             nodes.push(this);
  283.             MBTN<T> currentNode = null, tmp = null;
  284.  
  285.             synchronized (this) {
  286.                 while (!nodes.isEmpty()) {
  287.                     result.add(currentNode = nodes.pop());
  288.                     if ((tmp = currentNode.getRight()) != null)
  289.                         nodes.push(tmp);
  290.                     if ((tmp = currentNode.getLeft()) != null)
  291.                         nodes.push(tmp);
  292.                 }
  293.             }
  294.  
  295.             result.add(this);
  296.             return result;
  297.         }
  298.  
  299.         public void mirrorAll() {
  300.             mirror();
  301.             synchronized (this) {
  302.                 if (this._left != null)
  303.                     this._left.mirrorAll();
  304.                 if (this._right != null)
  305.                     this._right.mirrorAll();
  306.             }
  307.         }
  308.  
  309.         @Override
  310.         public Iterator<MBTN<T>> iterator() {
  311.             return toArrayList().iterator();
  312.         }
  313.  
  314.         static class Cmp<T extends Comparable<T>> implements
  315.                 Comparator<MBTN<T>> {
  316.             @Override
  317.             public int compare(MBTN<T> paramT1, MBTN<T> paramT2) {
  318.                 return paramT1.getValue().compareTo(paramT2.getValue());
  319.             }
  320.         }
  321.  
  322.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement