HristijanBosheski

Проверка на валидност [АПС Вежби за втор колоквиум]

Jan 20th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.72 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.InputStreamReader;
  3. import java.util.StringTokenizer;
  4.  
  5. class BNode<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.  
  34. class BTree<E> {
  35.    
  36.     public BNode<E> root;
  37.    
  38.     public BTree() {
  39.         root = null;
  40.     }
  41.    
  42.     public BTree(E info) {
  43.         root = new BNode<E>(info);
  44.     }
  45.    
  46.     public void makeRoot(E elem) {
  47.         root = new BNode(elem);
  48.     }
  49.    
  50.     public void makeRootNode(BNode<E> node) {
  51.         root = node;
  52.     }
  53.    
  54.     public BNode<E> addChild(BNode<E> node, int where, E elem) {
  55.        
  56.         BNode<E> tmp = new BNode<E>(elem);
  57.        
  58.         if (where == BNode.LEFT) {
  59.             if (node.left != null)  // veke postoi element
  60.                 return null;
  61.             node.left = tmp;
  62.         } else {
  63.             if (node.right != null) // veke postoi element
  64.                 return null;
  65.             node.right = tmp;
  66.         }
  67.        
  68.         return tmp;
  69.     }
  70.    
  71.     public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  72.        
  73.         if (where == BNode.LEFT) {
  74.             if (node.left != null)  // veke postoi element
  75.                 return null;
  76.             node.left = tmp;
  77.         } else {
  78.             if (node.right != null) // veke postoi element
  79.                 return null;
  80.             node.right = tmp;
  81.         }
  82.        
  83.         return tmp;
  84.     }
  85.    
  86. }
  87.  
  88. public class ValidityCheck {
  89.    
  90.     public static void main(String[] args) throws Exception {
  91.         int i, j, k;
  92.         int index;
  93.         String action;
  94.        
  95.         String line;
  96.  
  97.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  98.         StringTokenizer st;
  99.  
  100.         int N = Integer.parseInt(br.readLine());
  101.  
  102.         BNode<Integer> nodes[] = new BNode[N];
  103.         BTree<Integer> tree = new BTree<Integer>();
  104.        
  105.         for (i=0;i<N;i++)
  106.             nodes[i] = new BNode<Integer>();
  107.        
  108.         for (i = 0; i < N; i++) {
  109.             line = br.readLine();
  110.             st = new StringTokenizer(line);
  111.             index = Integer.parseInt(st.nextToken());
  112.             nodes[index].info = Integer.parseInt(st.nextToken());
  113.             action = st.nextToken();
  114.             if (action.equals("LEFT")) {
  115.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  116.             } else if (action.equals("RIGHT")) {
  117.                 tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);    
  118.             } else {
  119.                 // this node is the root
  120.                 tree.makeRootNode(nodes[index]);
  121.             }
  122.         }
  123.  
  124.         br.close();
  125.        
  126.         if(validityCheck(tree.root))
  127.             System.out.println("true");
  128.         else
  129.             System.out.println("false");
  130.         // vasiot kod ovde
  131.        
  132.        
  133.     }
  134.     public static boolean validityCheck(BNode<Integer> head) {
  135.         if(head == null)
  136.             return true;
  137.     else if(head.left == null && head.right==null){
  138.             return true;
  139.             }
  140.         else if((head.left != null && (head.info < head.left.info)) || (head.right!= null && (head.info < head.right.info))){
  141.             return false;
  142.             }else{
  143.             return validityCheck(head.left) && validityCheck(head.right);
  144.         }
  145.        
  146.     }
  147. }
Add Comment
Please, Sign In to add comment