Mon répertoire pour le bureau d'étude graphes de 3MIC à l'INSA de Toulouse
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BinaryHeapFormatter.java 6.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. package org.insa.graphs.algorithm.utils;
  2. import java.util.ArrayList;
  3. public class BinaryHeapFormatter {
  4. /**
  5. * This class is used by {@link #toStringTree}, and simply contains three string
  6. * accumulating. This is an immutable class.
  7. *
  8. */
  9. private static class Context {
  10. // Output text:
  11. public final String acu;
  12. // Margin to get back exactly under the current position:
  13. public final String margin;
  14. // Last margin used for the last child of a node. The markers are different:
  15. public final String lastmargin;
  16. /**
  17. * Creaet a new {@code Context}.
  18. *
  19. * @param acu The accumulated string.
  20. * @param margin The current margin.
  21. * @param lastMargin The last margin used.
  22. */
  23. public Context(String acu, String margin, String lastMargin) {
  24. this.acu = acu;
  25. this.margin = margin;
  26. this.lastmargin = lastMargin;
  27. }
  28. /**
  29. * Creates a new context by appending newlines to this context.
  30. *
  31. * @param n Number of newlines to append.
  32. *
  33. * @return a new context with {@code n} newlines appended.
  34. */
  35. public Context appendNewlines(int n) {
  36. if (n <= 0) {
  37. return this;
  38. }
  39. else {
  40. return (new Context(this.acu + "\n" + this.margin, this.margin, this.lastmargin)
  41. .appendNewlines(n - 1));
  42. }
  43. }
  44. /**
  45. * Creates a new context by appending the given string to this context.
  46. *
  47. * @param count Number of spaces to add to the margin, or {@code null} to use
  48. * the length of the string.
  49. * @param text String to append.
  50. *
  51. * @return a new context with {@code text} appended.
  52. */
  53. public Context appendText(Integer count, String text) {
  54. int cnt = (count == null) ? text.length() : count;
  55. final String spaces = new String(new char[cnt]).replace('\0', ' ');
  56. return new Context(this.acu + text, this.margin + spaces, this.lastmargin + spaces);
  57. }
  58. /**
  59. * Creates a new context by appending a branch to this context.
  60. *
  61. * @param n Number of spaces to add to the margin, or {@code null} to use
  62. * the length of the string.
  63. * @param label Name of the branch.
  64. *
  65. * @return a new context with the branch appended.
  66. */
  67. public Context appendBranch(Integer count, String label) {
  68. final Context ctxt = this.appendText(count, label);
  69. if (count == null) {
  70. return new Context(ctxt.acu + "_", ctxt.margin + "|", ctxt.margin + " ");
  71. }
  72. else {
  73. return new Context(ctxt.acu, ctxt.margin + "|", ctxt.margin + " ")
  74. .appendNewlines(1);
  75. }
  76. }
  77. }
  78. /*
  79. * Input : ready to write the current node at the current context position.
  80. * Output : the last character of acu is the last character of the current node.
  81. */
  82. protected static <E extends Comparable<E>> Context toStringLoop(BinaryHeap<E> heap,
  83. Context ctxt, int node, int max_depth) {
  84. if (max_depth < 0) {
  85. return ctxt.appendText(null, "...");
  86. }
  87. else {
  88. E nodeval = heap.array.get(node);
  89. String nodevals = nodeval.toString();
  90. ArrayList<Integer> childs = new ArrayList<Integer>();
  91. // Add childs
  92. int index_left = heap.indexLeft(node);
  93. int index_right = index_left + 1;
  94. if (index_left < heap.size()) {
  95. childs.add(index_left);
  96. }
  97. if (index_right < heap.size()) {
  98. childs.add(index_right);
  99. }
  100. Context ctxt2 = childs.isEmpty() ? ctxt.appendText(null, nodevals)
  101. : ctxt.appendBranch(1, nodevals);
  102. for (int ch = 0; ch < childs.size(); ch++) {
  103. boolean is_last = (ch == childs.size() - 1);
  104. int child = childs.get(ch);
  105. if (is_last) {
  106. Context ctxt3 = new Context(ctxt2.acu, ctxt2.lastmargin, ctxt2.lastmargin);
  107. ctxt2 = new Context(toStringLoop(heap, ctxt3.appendText(null, "___"), child,
  108. max_depth - 1).acu, ctxt2.margin, ctxt2.lastmargin);
  109. }
  110. else {
  111. ctxt2 = new Context(toStringLoop(heap, ctxt2.appendText(null, "___"), child,
  112. max_depth - 1).acu, ctxt2.margin, ctxt2.lastmargin).appendNewlines(2);
  113. }
  114. }
  115. return ctxt2;
  116. }
  117. }
  118. /**
  119. * Creates a multi-lines string representing a tree view of the given binary
  120. * heap.
  121. *
  122. * @param heap The binary heap to display.
  123. * @param maxDepth Maximum depth of the tree to display.
  124. *
  125. * @return a string containing a tree view of the given binary heap.
  126. */
  127. public static <E extends Comparable<E>> String toStringTree(BinaryHeap<E> heap, int maxDepth) {
  128. final Context init_context = new Context(" ", " ", " ");
  129. final Context result = toStringLoop(heap, init_context, 0, maxDepth);
  130. return result.acu;
  131. }
  132. /**
  133. * Creates a multi-lines string representing a sorted view of the given binary
  134. * heap.
  135. *
  136. * @param heap The binary heap to display.
  137. * @param maxElement Maximum number of elements to display. or {@code -1} to
  138. * display all the elements.
  139. *
  140. * @return a string containing a sorted view the given binary heap.
  141. */
  142. public static <E extends Comparable<E>> String toStringSorted(BinaryHeap<E> heap,
  143. int max_elements) {
  144. String result = "";
  145. final BinaryHeap<E> copy = new BinaryHeap<E>(heap);
  146. final String truncate;
  147. if (max_elements < 0 || max_elements >= heap.size()) {
  148. truncate = "";
  149. }
  150. else {
  151. truncate = ", only " + max_elements + " elements are shown";
  152. }
  153. result += "======== Sorted HEAP (size = " + heap.size() + truncate + ") ========\n\n";
  154. while (!copy.isEmpty() && max_elements-- != 0) {
  155. result += copy.deleteMin() + "\n";
  156. }
  157. result += "\n-------- End of heap --------";
  158. return result;
  159. }
  160. public static void main(String[] args) {
  161. final BinaryHeap<Integer> heap = new BinaryHeap<Integer>();
  162. for (int i = 0; i < 12; i++) {
  163. heap.insert(i);
  164. }
  165. System.out.println(heap.toStringSorted(-1));
  166. System.out.println(heap.toStringTree(6));
  167. }
  168. }