Advertisement
Guest User

Untitled

a guest
May 30th, 2015
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.16 KB | None | 0 0
  1. package eksam0106;
  2.  
  3. import java.util.*;
  4. //Ott Kingisepp
  5. public class Answer {
  6.  
  7. private String name;
  8. private Answer firstChild;
  9. private Answer nextSibling;
  10.  
  11. Answer (String n, Answer d, Answer r) {
  12. setName (n);
  13. setFirstChild (d);
  14. setNextSibling (r);
  15. }
  16.  
  17. public void setName (String n) { name = n; }
  18. public String getName() { return name; }
  19. public void setFirstChild (Answer d) { firstChild = d; }
  20. public Answer getFirstChild() { return firstChild; }
  21. public void setNextSibling (Answer r) { nextSibling = r; }
  22. public Answer getNextSibling() { return nextSibling; }
  23.  
  24. @Override
  25. public String toString() {
  26. return leftParentheticRepresentation();
  27. }
  28.  
  29. public String leftParentheticRepresentation() {
  30. StringBuffer b = new StringBuffer();
  31. b.append (getName());
  32. if (getFirstChild() != null) {
  33. b.append ("(");
  34. b.append (getFirstChild().leftParentheticRepresentation());
  35. Answer right = getFirstChild().getNextSibling();
  36. while (right != null) {
  37. b.append ("," + right.leftParentheticRepresentation());
  38. right = right.getNextSibling();
  39. }
  40. b.append (")");
  41. }
  42. return b.toString();
  43. }
  44.  
  45. public static Answer parseTree (String s) {
  46. if (s == null) return null;
  47. if (s.length() == 0) return null;
  48. Answer root = null;
  49. Answer curr = null;
  50. Answer last = null;
  51. int state = 0; // begin
  52. Stack<Answer> stk = new Stack<Answer>();
  53. StringTokenizer tok = new StringTokenizer (s, "(),", true);
  54. while (tok.hasMoreTokens()) {
  55. String w = tok.nextToken().trim();
  56. if (w.equals ("(")) {
  57. state = 1; // from up
  58. } else if (w.equals (",")) {
  59. state = 2; // from left
  60. } else if (w.equals (")")) {
  61. state = 3; // from down
  62. stk.pop();
  63. } else {
  64. curr = new Answer (w, null, null);
  65. switch (state) {
  66. case 0: {
  67. root = curr;
  68. break;
  69. }
  70. case 1: {
  71. last = stk.peek();
  72. last.setFirstChild (curr);
  73. break;
  74. }
  75. case 2: {
  76. last = stk.pop();
  77. last.setNextSibling (curr);
  78. break;
  79. }
  80. default: {
  81. // do not pop here but after ")"
  82. }
  83. } // switch
  84. stk.push (curr);
  85. }
  86. } // next w
  87. return root;
  88. }
  89.  
  90. public int maxWidth() {
  91. Answer v = firstChild;
  92. int tmpWidth = countChildren(firstChild);
  93. if(firstChild== null && nextSibling == null){
  94. return 0 ;
  95. }
  96. if(firstChild != null && nextSibling == null){
  97.  
  98. Answer s = firstChild;
  99. if(tmpWidth > s.maxWidth()){
  100. return tmpWidth;
  101. }
  102. else{
  103. return s.maxWidth();
  104. }
  105. }
  106.  
  107. if(firstChild == null && nextSibling != null){
  108. Answer s = nextSibling;
  109. int comp1 = s.maxWidth();
  110. if(comp1>tmpWidth){
  111. return comp1;
  112. }
  113. else{
  114. return tmpWidth;
  115. }
  116. }
  117. if(firstChild != null && nextSibling !=null){
  118. if(firstChild.maxWidth() > nextSibling.maxWidth()){
  119. if(tmpWidth >firstChild.maxWidth()){
  120. return tmpWidth;
  121. }
  122. else{return firstChild.maxWidth();}
  123.  
  124. }
  125. else{
  126. if(tmpWidth>nextSibling.maxWidth()){
  127. return tmpWidth;
  128. }
  129. else{return nextSibling.maxWidth();}
  130. }
  131. }
  132. return 0;
  133. }
  134. public int countChildren(Answer v){
  135. if(v ==null){
  136. return 0;
  137. }
  138. int counter = 1;
  139. while(v.nextSibling !=null){
  140. counter++;
  141. v = v.nextSibling;
  142. }
  143. return counter;
  144. }
  145.  
  146. public static void main (String[] param) {
  147. Answer v = Answer.parseTree ("A(C,D(F,G,H(Y,U,I,O,P))");
  148. System.out.println (v);
  149. int n = v.maxWidth();
  150. System.out.println ("Maximum number of children: " + n); // 4
  151. // TODO!!! Your tests here!
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement