Filip_Markoski

[ADSx] - ValidityCheck

Jan 8th, 2018
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.30 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4.  
  5. class BNode<E extends Comparable<E>> {
  6.  
  7.     public E info;
  8.     public BNode<E> left;
  9.     public BNode<E> right;
  10.  
  11.     static int LEFT = 1;
  12.     static int RIGHT = 2;
  13.  
  14.     public BNode(E info) {
  15.         this.info = info;
  16.         left = null;
  17.         right = null;
  18.     }
  19.  
  20.     public BNode() {
  21.         this.info = null;
  22.         left = null;
  23.         right = null;
  24.     }
  25.  
  26.     public BNode(E info, BNode<E> left, BNode<E> right) {
  27.         this.info = info;
  28.         this.left = left;
  29.         this.right = right;
  30.     }
  31. }
  32.  
  33. class BTree<E extends Comparable<E>> {
  34.  
  35.     public BNode<E> root;
  36.  
  37.     public BTree() {
  38.         root = null;
  39.     }
  40.  
  41.     public BTree(E info) {
  42.         root = new BNode<E>(info);
  43.     }
  44.  
  45.     public void makeRoot(E elem) {
  46.         root = new BNode(elem);
  47.     }
  48.  
  49.     public void makeRootNode(BNode<E> node) {
  50.         root = node;
  51.     }
  52.  
  53.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  54.  
  55.         if (where == BNode.LEFT) {
  56.             if (node.left != null)  // veke postoi element
  57.                 return null;
  58.             node.left = tmp;
  59.         } else {
  60.             if (node.right != null) // veke postoi element
  61.                 return null;
  62.             node.right = tmp;
  63.         }
  64.  
  65.         return tmp;
  66.     }
  67.  
  68.     public boolean biggerThanChildren(BNode<E> node) {
  69.         if (node == null) return true;
  70.         if (node.left != null && !(node.info.compareTo(node.left.info) >= 0)) return false;
  71.         if (node.right != null && !(node.info.compareTo(node.right.info) >= 0)) return false;
  72.         return biggerThanChildren(node.left) && biggerThanChildren(node.right);
  73.     }
  74.  
  75. }
  76.  
  77. public class ValidityCheck {
  78.  
  79.     public static void main(String[] args) throws Exception {
  80.         int i, j, k;
  81.         int index;
  82.         String action;
  83.  
  84.         String line;
  85.  
  86.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  87.         StringTokenizer st;
  88.  
  89.         int N = Integer.parseInt(br.readLine());
  90.  
  91.         BNode<Integer> nodes[] = new BNode[N];
  92.         BTree<Integer> tree = new BTree<Integer>();
  93.  
  94.         for (i = 0; i < N; i++)
  95.             nodes[i] = new BNode<Integer>();
  96.  
  97.         for (i = 0; i < N; i++) {
  98.             line = br.readLine();
  99.             st = new StringTokenizer(line);
  100.             index = Integer.parseInt(st.nextToken());
  101.             nodes[index].info = Integer.parseInt(st.nextToken());
  102.             action = st.nextToken();
  103.             if (action.equals("LEFT")) {
  104.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  105.             } else if (action.equals("RIGHT")) {
  106.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);
  107.             } else {
  108.                 // this node is the root
  109.                 tree.makeRootNode(nodes[index]);
  110.             }
  111.         }
  112.  
  113.         br.close();
  114.  
  115.         System.out.println(tree.biggerThanChildren(tree.root));
  116.  
  117.     }
  118. }
  119. /**
  120.  * Write a function ValidityCheck which for a given tree,
  121.  * that will check if the info value of each node,
  122.  * is bigger than, or equal to, the info value of its children,
  123.  * and will print true or false, correspondingly.
  124.  */
Advertisement
Add Comment
Please, Sign In to add comment