fork download
  1. class Tree<T> {
  2. private T label;
  3. private Tree<T> parent;
  4. private Tree<T> nextSibling; // next node on the list of parents's
  5. // children
  6. private Tree<T> firstChild; // first in the linked list of children
  7.  
  8.  
  9. // Getters and setters
  10. public T getLabel() { return label; }
  11. public void setLabel(T v) { label = v; }
  12. public Tree<T> getParent() { return parent;}
  13. public Tree<T> getNextSibling() { return nextSibling;}
  14. public Tree<T> getFirstChild() { return firstChild;}
  15.  
  16. // Add C to the front of the children of this
  17. public void addChild(Tree<T> c) {
  18. c.parent = this;
  19. if (firstChild == null)
  20. firstChild = c;
  21. else {
  22. c.nextSibling = firstChild;
  23. firstChild = c;
  24. }
  25. }
  26.  
  27. // Check if the node is a leaf
  28. public boolean Leaf() { return firstChild==null; }
  29.  
  30. // `Convert the tree into a string. The structure is indicated by
  31. // square brackets. Uses a preorder listing.
  32. public String toString() {
  33. String S = "[ " + label.toString();
  34. Tree<T> N = firstChild;
  35. while (N != null) {
  36. S = S + " " + N.toString();
  37. N = N.nextSibling;
  38. }
  39. return S+" ]";
  40. }
  41.  
  42. public void displayPreorder() {
  43. displayPreorder1(0); }
  44.  
  45. public void displayPreorder1(int Indent) {
  46. for (int I = 0; I < Indent; I++) System.out.print(" ");
  47. System.out.println(label.toString());
  48. Tree<T> N = firstChild;
  49. while (N != null) {
  50. N.displayPreorder1(Indent+3);
  51. N = N.nextSibling;
  52. }
  53. }
  54.  
  55. public void displayPostorder() {
  56. displayPostorder1(0); }
  57.  
  58. public void displayPostorder1(int Indent) {
  59. Tree<T> N = firstChild;
  60. while (N != null) {
  61. N.displayPostorder1(Indent+3);
  62. N = N.nextSibling;
  63. }
  64. for (int I = 0; I < Indent; I++) System.out.print(" ");
  65. System.out.println(label.toString());
  66. }
  67. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
spoj: The program compiled successfully, but main class was not found.
      Main class should contain method: public static void main (String[] args).
stdout
Standard output is empty