Guest User

Untitled

a guest
Jan 23rd, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.51 KB | None | 0 0
  1. (a(b()())(c(d()())()))
  2.  
  3. a b
  4. a c d
  5.  
  6. import java.util.ArrayList;
  7. import java.util.Arrays;
  8. import java.util.Scanner;
  9.  
  10.  
  11. public class BinaryTreeFinal {
  12. public static void main(String[] args) {
  13. Scanner scan = new Scanner(System.in);
  14. String tree = scan.nextLine();//correct format : (a()())
  15. String[] t = splitTree(tree);
  16. System.out.println(Arrays.toString(t));
  17. //System.out.println(tree2("a", "(a(b()())(c(d()())()))"));
  18. }
  19.  
  20. public static String[] splitTree(String tree)
  21. {
  22. //expected format
  23. //(node tree tree)
  24. //0 1 2-x x-(length-2) length-1
  25. if(tree.length() <= 2)//tree not long enough to process
  26. return new String[]{tree};
  27.  
  28. String[] temp = new String[3];
  29. temp[0] = "" + tree.charAt(1);//grab tree node
  30. tree = tree.substring(2, tree.length()-1);//remove node and outer paren
  31. int parenCount = 0;//count of open paren
  32. int endTreeOne = 0;//end of first tree
  33. for(int i = 0; i < tree.length(); i++)
  34. {
  35. if(tree.charAt(i) == '(')
  36. parenCount++;
  37. if(tree.charAt(i) == ')')
  38. parenCount--;
  39. if(parenCount == 0)
  40. {
  41. endTreeOne = i;
  42. break;//ends for loop early
  43. }
  44. }
  45. temp[1] = tree.substring(0, endTreeOne+1);//left tree
  46. temp[2] = tree.substring(endTreeOne+1);//right tree
  47. return temp;
  48. }
  49.  
  50. public static char tree2(String root, String path)
  51. {
  52.  
  53. int counter = 0;
  54. String[] trees = splitTree(path);
  55.  
  56. if(trees[1].charAt(counter) == '(' && trees[1].charAt(counter++) == ')')
  57. {
  58. counter++;
  59. //return trees[1].charAt(counter);
  60. return tree2(String, String);
  61. //System.out.println(trees[1].charAt(counter));
  62.  
  63.  
  64. }
  65. else
  66. //System.out.println(trees[1].charAt(counter));
  67. return trees[1].charAt(counter);
  68. //counter++;
  69.  
  70.  
  71. }
  72.  
  73. Input: `(a(b()())(c(d()())()))`
  74.  
  75. Output: a b
  76. a c d
  77.  
  78. Input: `(a(b()())(c(d()())(e)))`
  79.  
  80. Output: a b
  81. a c d
  82. a c e
  83.  
  84. Stack<Character> stack = new Stack<Character>();
  85. String st = "(a(b()())(c(d()())(e)))";
  86. String parent = null;
  87. for (int i = 0; i < st.length(); i++) {
  88. char c = st.charAt(i);
  89.  
  90. if (c != ')') {
  91.  
  92. // if it is an alphabet
  93. if (c != '(') {
  94. // will require printing of parents
  95. // iff there are more characters to print
  96. if (parent != null) {
  97. System.out.print(parent);
  98. parent = null;
  99. }
  100. System.out.print(c + " ");
  101. }
  102. stack.push(c);
  103. } else {
  104. // is the character on top a matching opening bracket?
  105. // if it is, then nothing to do, if not
  106. char curTop = stack.pop();
  107. if (curTop != '(')
  108. while (curTop != '(')
  109. curTop = stack.pop();
  110. else
  111. continue;
  112.  
  113. // done working with a character; move to next line
  114. System.out.println();
  115.  
  116. // now, need to reprint the `a` portion
  117. // iff more children are present
  118. Stack<Character> temp = new Stack<Character> ();
  119. while (!stack.isEmpty())
  120. temp.push(stack.pop());
  121.  
  122. while (!temp.isEmpty()) {
  123. Character ch = temp.pop();
  124. if (!(ch == '(' || ch == ')')) {
  125. // store content
  126. if (parent == null)
  127. parent = "";
  128. parent += ch + " ";
  129. }
  130.  
  131. stack.push(ch);
  132. }
  133.  
  134. }
  135. }
Add Comment
Please, Sign In to add comment