Advertisement
DonCoutinho

balanced Parantheses

Jan 16th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.34 KB | None | 0 0
  1.  
  2. import java.util.ArrayList;
  3.  
  4. public class BalancedParantheses {
  5.    
  6.     private static BitList globalParantheses;
  7.     private static ArrayList<Character> globalNodes;
  8.  
  9.     /*
  10.      * Task 1 : Transform
  11.      * Write a method transform that can be called by
  12.         transform( Tree, Parentheses, Nodes )
  13.         and that transforms a given tree in prefix notation into a list of balanced parenthesis and a list of node
  14.         names. You shall assume that node names are only letters ‘a’..’z’ and that the input Tree is syntactically
  15.         correct.
  16.      */
  17.     public static void transform (String tree, BitList Parantheses, ArrayList<Character> Nodes) {
  18.         //work through the tree input
  19.         for(int i = 0; i < tree.length(); i++) {
  20.             if(tree.charAt(i) >= 'a' && tree.charAt(i) <= 'z') {
  21.                 Parantheses.insert(true, Parantheses.getSize());
  22.                 Nodes.add(tree.charAt(i)); //add nodes
  23.             }
  24.             else if(tree.charAt(i) == ')' || tree.charAt(i) == ',') {
  25.                 Parantheses.insert(false, Parantheses.getSize());
  26.             }
  27.         }
  28.         //add 0 to balance parantheses
  29.         Parantheses.insert(false, Parantheses.getSize());
  30.         globalNodes = Nodes;
  31.     }
  32.     /*
  33.      * Task 2 : Basic Navigation
  34.      * We assume that we have an unranked tree represented by a syntactically correct balanced
  35.         parentheses bit string S, where each ‘(‘ is encoded by a 1-bit and each ‘)’ is encoded by a 0-bit. For
  36.         example, S = [1, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 0]. We further assume that the current tree node is always
  37.         represented by the position of a 1-bit in the bit string S.
  38.         Implement a method fc (first-child) that can be called for each position pos where S[pos] is 1 by
  39.         fc_pos = fc(S,pos)
  40.         and that returns the position in S of the 1-bit representing the first child of the current tree node.
  41.         If a tree node does not have a first child, the value of -1 shall be returned.
  42.      */
  43.    
  44.     public static int fc(BitList S, int pos) {
  45.          if(S.get(pos+1)) return pos+1;
  46.         return -1;
  47.     }
  48.    
  49.      /* Implement a method ns (next-sibling) that can be called for each position pos where S[pos] is 1 by
  50.         ns_pos = ns(S,pos)
  51.         and that returns the position in S of the 1-bit representing the next sibling of the current tree node.
  52.         If a tree node does not have a next sibling, the value of -1 shall be returned.
  53.      */
  54.       public static int ns(BitList s, int position) {
  55.             int count = 1;
  56.             while(count != 0 && position < s.getSize() - 2)
  57.              {
  58.                 int c = s.get(position)?-1:1;
  59.                 position++;
  60.                 count+=c;
  61.                 if(count == 0 & s.get(position+1)) return position+1;
  62.             }
  63.             return -1;
  64.         }
  65.     /*
  66.     Implement a method fc1 (first-child^-1) that can be called for each position pos where S[pos] is 1 by
  67.     fc1_pos = fc1(S,pos)
  68.     and that returns the position in S of the 1-bit representing the parent of the current tree node if the
  69.     current tree node is the first child of this parent.
  70.     If the current tree node is not a first child of another tree node, the value of -1 shall be returned.
  71.      */
  72.     public static int fc1(BitList s, int position) {
  73.         if(position != 0) {
  74.             if(s.get(position-1)) return position-1;
  75.         }
  76.         return -1;
  77.     }
  78.     /*
  79.      * Implement a method ns1 (next-sibling^-1) that can be called for each position pos where S[pos] is 1 by
  80.         ns1_pos = ns1(S,pos)
  81.         and that returns the position in S of the 1-bit representing the previous sibling of the current tree node
  82.         if the current tree node is the next-sibling of this previous sibling.
  83.         If the current tree node is not the next-sibling of another tree node, the value of -1 shall be returned.
  84.      */
  85.     // Reverse NextSibling
  86.     public static int ns1(BitList s, int position) {
  87.         if(position == 1) {
  88.             int count = 0;
  89.             while ( position >= 1) {
  90.                 position--;
  91.                 int c = s.get(position)?-1:1;
  92.                 count+=c;
  93.                 if(count == 0) return position;
  94.             }
  95.         }
  96.         return -1;
  97.         }
  98.     /*
  99.      * Implement a method pa (parent) that can be called for each position pos where S[pos] is 1 by
  100.         pa_pos = pa(S,pos)
  101.         and that returns the position in S of the 1-bit representing the parent of the current tree node.
  102.         If the current tree node is the root node, the value of -1 shall be returned.
  103.      */
  104.     public static int pa(BitList s, int position) {
  105.         if(position == 0) return -1;
  106.         int count = 1;
  107.         while(position > 0) {
  108.             int c = s.get(position)?1:-1;
  109.             count+=c;
  110.         if(count == 0) return position;
  111.         }
  112.        
  113.         return -1;
  114.     }
  115.    
  116.     public static void test(String s) {
  117.         //fill globalParantheses
  118.         for(int i = 0; i < s.length(); i++) {
  119.             if(s.charAt(i) == '1') globalParantheses.insert(true, globalParantheses.getSize());
  120.             if(s.charAt(i) == '0') globalParantheses.insert(false, globalParantheses.getSize());
  121.         }
  122.         //now test it
  123.         System.out.println("Test Part2 with ["+s+"] :");
  124.         for(int i = 0; i < globalParantheses.getSize(); i++) {
  125.             if(globalParantheses.get(i)) {
  126.                 System.out.println(retString(globalParantheses, i));
  127.             }
  128.         }
  129.        
  130.     }
  131.     public static String retString(BitList s , int pos) {
  132.         int fc_pos = fc(s, pos);   
  133.         int fc1_pos = fc1(s, pos);
  134.         int ns_pos = ns(s,pos);
  135.         int ns1_pos = -1;
  136.         int pa_pos = -1;
  137.         String t = "pos = " + pos + ", fc = " + fc_pos + ", ns = " + ns_pos + ", fc^-1 = "+fc1_pos
  138.                 + ", ns^-1 = " +ns1_pos + ", parent = " + pa_pos;
  139.         return t;
  140.     }
  141.    
  142.    
  143.     public static void main(String[] args) {
  144.         //init global variables
  145.         globalParantheses = new BitList();
  146.         globalNodes = new ArrayList<Character>();
  147.        
  148.         test("1,1,1,0,1,0,0,1,0,1,0,0");
  149.        
  150.        
  151.        
  152.        
  153.     }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement