Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.55 KB | None | 0 0
  1. package fr.istic.prg1.tree_util;
  2.  
  3. import java.util.Stack;
  4.  
  5. public class BinaryTree<T>
  6. {
  7. private Root element;
  8. private static boolean firstTime;
  9.  
  10. static {
  11. BinaryTree.firstTime = true;
  12. }
  13.  
  14. public BinaryTree() {
  15. if (BinaryTree.firstTime) {
  16. System.out.println("mise en oeuvre arbre binaire - pile des peres - version enseignant");
  17. BinaryTree.firstTime = false;
  18. }
  19. this.element = new Root();
  20. }
  21.  
  22. public Iterator<T> iterator() {
  23. return new Element((Element)null);
  24. }
  25.  
  26. public boolean isEmpty() {
  27. return this.element.isEmpty();
  28. }
  29.  
  30. public static void controle() {
  31. final double v = 2013.0322;
  32. System.out.println("valeur de controle " + v);
  33. }
  34.  
  35. private class Root
  36. {
  37. public T nextRoot;
  38. public Root doubleRoot;
  39. public Root simpleRoot;
  40.  
  41. public Root() {
  42. this.nextRoot = null;
  43. this.doubleRoot = null;
  44. this.simpleRoot = null;
  45. }
  46.  
  47. public boolean isEmpty() {
  48. return this.doubleRoot == null && this.simpleRoot == null;
  49. }
  50. }
  51.  
  52. public class Element implements Iterator<T>
  53. {
  54. private Root root;
  55. private Stack<Root> stack;
  56.  
  57. private Element() {
  58. this.stack = new Stack<Root>();
  59. this.root = BinaryTree.this.element;
  60. }
  61.  
  62. @Override
  63. public void goLeft() {
  64. try {
  65. assert !this.isEmpty() : "le butoir n'a pas de fils";
  66. assert this.root.doubleRoot != null : "le fils gauche d'existe pas";
  67. }
  68. catch (AssertionError e) {
  69. e.printStackTrace();
  70. System.exit(0);
  71. }
  72. this.stack.push(this.root);
  73. this.root = this.root.doubleRoot;
  74. }
  75.  
  76. @Override
  77. public void goRight() {
  78. try {
  79. assert !this.isEmpty() : "le butoir n'a pas de fils";
  80. assert this.root.simpleRoot != null : "le fils droit d'existe pas";
  81. }
  82. catch (AssertionError e) {
  83. e.printStackTrace();
  84. System.exit(0);
  85. }
  86. this.stack.push(this.root);
  87. this.root = this.root.simpleRoot;
  88. }
  89.  
  90. @Override
  91. public void goUp() {
  92. try {
  93. assert !this.stack.empty() : " la racine n'a pas de pere";
  94. }
  95. catch (AssertionError e) {
  96. e.printStackTrace();
  97. System.exit(0);
  98. }
  99. this.root = this.stack.pop();
  100. }
  101.  
  102. @Override
  103. public void goRoot() {
  104. this.stack.clear();
  105. this.root = BinaryTree.this.element;
  106. }
  107.  
  108. @Override
  109. public boolean isEmpty() {
  110. return this.root.isEmpty();
  111. }
  112.  
  113. @Override
  114. public NodeType nodeType() {
  115. if (this.isEmpty()) {
  116. return NodeType.SENTINEL;
  117. }
  118. if (this.root.doubleRoot.isEmpty()) {
  119. if (this.root.simpleRoot.isEmpty()) {
  120. return NodeType.LEAF;
  121. }
  122. return NodeType.SIMPLE_RIGHT;
  123. }
  124. else {
  125. if (this.root.simpleRoot.isEmpty()) {
  126. return NodeType.SIMPLE_LEFT;
  127. }
  128. return NodeType.DOUBLE;
  129. }
  130. }
  131.  
  132. @Override
  133. public void remove() {
  134. try {
  135. assert this.nodeType() != NodeType.DOUBLE : "retirer : retrait d'un noeud double non permis";
  136. }
  137. catch (AssertionError e) {
  138. e.printStackTrace();
  139. System.exit(0);
  140. }
  141. Root sentinel = null;
  142. switch (this.nodeType()) {
  143. case SENTINEL: {
  144. return;
  145. }
  146. case SIMPLE_LEFT: {
  147. sentinel = this.root.doubleRoot;
  148. break;
  149. }
  150. case SIMPLE_RIGHT: {
  151. sentinel = this.root.simpleRoot;
  152. break;
  153. }
  154. case LEAF: {
  155. sentinel = new Root();
  156. break;
  157. }
  158. }
  159. this.root.nextRoot = sentinel.nextRoot;
  160. this.root.doubleRoot = sentinel.doubleRoot;
  161. this.root.simpleRoot = sentinel.simpleRoot;
  162. }
  163.  
  164. @Override
  165. public void clear() {
  166. this.root.nextRoot = null;
  167. this.root.doubleRoot = null;
  168. this.root.simpleRoot = null;
  169. }
  170.  
  171. @Override
  172. public T getValue() {
  173. return this.root.nextRoot;
  174. }
  175.  
  176. @Override
  177. public void addValue(final T v) {
  178. try {
  179. assert this.isEmpty() : "Ajouter : on n'est pas sur un butoir";
  180. }
  181. catch (AssertionError e) {
  182. e.printStackTrace();
  183. System.exit(0);
  184. }
  185. this.root.nextRoot = v;
  186. this.root.doubleRoot = new Root();
  187. this.root.simpleRoot = new Root();
  188. }
  189.  
  190. @Override
  191. public void setValue(final T v) {
  192. this.root.nextRoot = v;
  193. }
  194.  
  195. private void ancestor(final int i, final int j) {
  196. try {
  197. assert !this.stack.empty() : "switchValue : argument trop grand";
  198. }
  199. catch (AssertionError e) {
  200. e.printStackTrace();
  201. System.exit(0);
  202. }
  203. final Root x = this.stack.pop();
  204. if (j < i) {
  205. this.ancestor(i, j + 1);
  206. }
  207. else {
  208. final T v = x.nextRoot;
  209. x.nextRoot = this.root.nextRoot;
  210. this.root.nextRoot = v;
  211. }
  212. this.stack.push(x);
  213. }
  214.  
  215. @Override
  216. public void switchValue(final int i) {
  217. try {
  218. assert i >= 0 : "switchValue : argument negatif";
  219. }
  220. catch (AssertionError e) {
  221. e.printStackTrace();
  222. System.exit(0);
  223. }
  224. if (i > 0) {
  225. this.ancestor(i, 1);
  226. }
  227. }
  228. }
  229. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement