Advertisement
NEGI_RUS

TreeConstant

Dec 26th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.95 KB | None | 0 0
  1. public static class Node {
  2.     public Node left, right;
  3.     public Node parent;
  4.     public String data;
  5.    
  6.     public Node(String data, Node parent) {
  7.         this.data = data;
  8.         this.parent = parent;
  9.     }
  10.    
  11.     public Node addLeft(String data) {
  12.         left = new Node(data, this);
  13.         return left;
  14.     }
  15.    
  16.     public Node addRight(String data) {
  17.         right = new Node(data, this);
  18.         return right;
  19.     }
  20. }
  21.  
  22. public static void main(String[] args) {
  23. /*  0   1   2   3   */
  24.     Node root = new Node("root", null);
  25.     {
  26.         Node n10 = root.addLeft("1,0");
  27.         {
  28.             Node n20 = n10.addLeft("2,0");
  29.             {
  30.                 Node n30 = n20.addLeft("3,0");
  31.                 Node n31 = n20.addRight("3,1");
  32.             }
  33.             Node n21 = n10.addRight("2,1");
  34.             {
  35.                 Node n32 = n21.addLeft("3,2");
  36.                 Node n33 = n21.addRight("3,3");
  37.             }
  38.         }
  39.         Node n11 = root.addRight("1,1");
  40.         {
  41.             Node n22 = n11.addLeft("2,2");
  42.             Node n23 = n11.addRight("2,3");
  43.         }
  44.     }
  45.     /* 11 nodes
  46.      *             *
  47.      *           /   \
  48.      *          *     *
  49.      *         / \   / \
  50.      *        /\ /\
  51.      */
  52.    
  53.     int deep = 0;
  54.     int lastDeep = 0;
  55.     boolean leftRight = false; // false = left, true = right
  56.     boolean upDown = true; // false = up, true = down
  57.    
  58.     Node current = root;
  59.     do {
  60.         if (upDown == true) {
  61.             System.out.println(current.data);
  62.         } else if (lastDeep == deep) {
  63.             if (leftRight == false) {
  64.                 current = current.right;
  65.                 System.out.println(current.data);
  66.                 deep++;
  67.                 upDown = true;
  68.                 leftRight = true;
  69.             } else {
  70.                 leftRight = (current.parent.right == current);
  71.                 current = current.parent;
  72.                 lastDeep--;
  73.                 deep--;
  74.                 upDown = false;
  75.             }
  76.         }
  77.        
  78.         if (current.left != null && current.right != null)  {
  79.             lastDeep = deep;
  80.             if (upDown == true) {
  81.                 current = current.left;
  82.                 deep++;
  83.                 upDown = true;
  84.                 leftRight = false;
  85.             }
  86.         } else {
  87.             leftRight = (current.parent.right == current);
  88.             current = current.parent;
  89.             deep--;
  90.             upDown = false;            
  91.         }
  92.     } while (leftRight != true || deep != 0);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement