Advertisement
Guest User

Untitled

a guest
Jun 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.44 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3.  
  4. public class TNode {
  5.  
  6.     private int data;
  7.     private int height = 0;
  8.  
  9.     private ArrayList<TNode> children;
  10.  
  11.     public TNode(int data, ArrayList<TNode> children) {
  12.         this.data = data;
  13.         this.children = children;
  14.     }
  15.  
  16.     public int getHeight() {
  17.         if (children == null)
  18.             return 0;
  19.  
  20.         if (height != 0)
  21.             return height;
  22.  
  23.         int max = -1;
  24.         for (TNode t : children)
  25.             if (t != null)
  26.                 if (t.getHeight() > max)
  27.                     max = t.getHeight();
  28.  
  29.         return height = max + 1;
  30.     }
  31.  
  32.     public void listAll() {
  33.         listAll(0);
  34.     }
  35.  
  36.     public void listAll(int depth) {
  37.         for (int i = 0; i < depth; i++)
  38.             System.out.print(" ");
  39.        
  40.         System.out.println(data + ":" + getHeight());
  41.         if (children != null)
  42.             for (TNode t : children)
  43.                 if (t != null)
  44.                     t.listAll(depth + 1);
  45.     }
  46.  
  47.     public ArrayList<Integer> makeList() {
  48.         ArrayList<Integer> list = new ArrayList<Integer>();
  49.         listAll(list);
  50.         return list;
  51.     }
  52.  
  53.     public void listAll(ArrayList<Integer> list) {
  54.         list.add(data);
  55.         if (children != null)
  56.             for (TNode t : children)
  57.                 if (t != null)
  58.                     t.listAll(list);
  59.     }
  60.  
  61.     public boolean contains(int x) {
  62.         if (data == x)
  63.             return true;
  64.  
  65.         if (children != null)
  66.             for (TNode t : children)
  67.                 if (t != null)
  68.                     if (t.contains(x))
  69.                         return true;
  70.  
  71.         return false;
  72.     }
  73.  
  74.     public static void main(String ... args) {
  75.        ArrayList<TNode> tl = new ArrayList<TNode>();
  76.        tl.add(null); tl.add(null); tl.add(null);
  77.        TNode tA = new TNode(3, tl);
  78.  
  79.        TNode tB = new TNode(1, null);
  80.  
  81.  
  82.        tl = new ArrayList<TNode>();
  83.        tl.add(tA); tl.add(tB); tl.add(null);
  84.        TNode tC = new TNode(4, tl);
  85.  
  86.        TNode tD = new TNode(1,null);
  87.  
  88.        tl = new ArrayList<TNode>();
  89.        tl.add(tD);
  90.        TNode tE = new TNode(5,tl);
  91.  
  92.        tl = new ArrayList<TNode>();
  93.        tl.add(tE); tl.add(tC);
  94.        TNode tRoot = new TNode(9,tl);
  95.  
  96.        tRoot.listAll();
  97.  
  98.         System.out.println(tRoot.contains(3));
  99.         System.out.println(tRoot.contains(6));
  100.  
  101.         System.out.println(tRoot.makeList());
  102.     }
  103.  
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement