Advertisement
HeatPulse

Lab7-2 Nerekurzivno izminuvanje vo preorder

Dec 11th, 2019
986
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.73 KB | None | 0 0
  1. Нерекурзивно изминување во preorder Problem 2 (1 / 1)
  2. Да се напише функција preorderNonRecursive за нерекурзивно изминување на бинарно стебло во preorder.
  3.  
  4. Име на класата: PreorderNonRecursive
  5.  
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStreamReader;
  9. import java.util.StringTokenizer;
  10. import java.util.NoSuchElementException;
  11.  
  12. class BNode<E> {
  13.  
  14. public E info;
  15. public BNode<E> left;
  16. public BNode<E> right;
  17.  
  18. static int LEFT = 1;
  19. static int RIGHT = 2;
  20.  
  21. public BNode(E info) {
  22. this.info = info;
  23. left = null;
  24. right = null;
  25. }
  26.  
  27. public BNode() {
  28. this.info = null;
  29. left = null;
  30. right = null;
  31. }
  32.  
  33. public BNode(E info, BNode<E> left, BNode<E> right) {
  34. this.info = info;
  35. this.left = left;
  36. this.right = right;
  37. }
  38.  
  39. }
  40.  
  41. class BTree<E> {
  42.  
  43. public BNode<E> root;
  44.  
  45. public BTree() {
  46. root = null;
  47. }
  48.  
  49. public BTree(E info) {
  50. root = new BNode<E>(info);
  51. }
  52.  
  53. public void makeRoot(E elem) {
  54. root = new BNode(elem);
  55. }
  56.  
  57. public void makeRootNode(BNode<E> node) {
  58. root = node;
  59. }
  60.  
  61. public BNode<E> addChild(BNode<E> node, int where, E elem) {
  62.  
  63. BNode<E> tmp = new BNode<E>(elem);
  64.  
  65. if (where == BNode.LEFT) {
  66. if (node.left != null) // veke postoi element
  67. return null;
  68. node.left = tmp;
  69. } else {
  70. if (node.right != null) // veke postoi element
  71. return null;
  72. node.right = tmp;
  73. }
  74.  
  75. return tmp;
  76. }
  77.  
  78. public BNode<E> addChildNode(BNode<E> node, int where, BNode<E> tmp) {
  79.  
  80. if (where == BNode.LEFT) {
  81. if (node.left != null) // veke postoi element
  82. return null;
  83. node.left = tmp;
  84. } else {
  85. if (node.right != null) // veke postoi element
  86. return null;
  87. node.right = tmp;
  88. }
  89.  
  90. return tmp;
  91. }
  92.  
  93. public void PreorderNonRecursive() {
  94.  
  95. Stack<BNode<E>> stack = new ArrayStack<>(100);
  96. BNode<E> temp = root;
  97.  
  98. while (true) {
  99.  
  100. while (temp != null) {
  101.  
  102. System.out.print(temp.info + " ");
  103. stack.push(temp);
  104. temp = temp.left;
  105. }
  106.  
  107. if (stack.isEmpty())
  108. break;
  109.  
  110. temp = stack.pop();
  111. temp = temp.right;
  112. }
  113.  
  114. }
  115.  
  116. }
  117.  
  118. interface Stack<E> {
  119.  
  120. // Elementi na stekot se objekti od proizvolen tip.
  121.  
  122. // Metodi za pristap:
  123.  
  124. public boolean isEmpty ();
  125. // Vrakja true ako i samo ako stekot e prazen.
  126.  
  127. public E peek ();
  128. // Go vrakja elementot na vrvot od stekot.
  129.  
  130. // Metodi za transformacija:
  131.  
  132. public void clear ();
  133. // Go prazni stekot.
  134.  
  135. public void push (E x);
  136. // Go dodava x na vrvot na stekot.
  137.  
  138. public E pop ();
  139. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  140. }
  141.  
  142. class ArrayStack<E> implements Stack<E> {
  143. private E[] elems;
  144. private int depth;
  145.  
  146. @SuppressWarnings("unchecked")
  147. public ArrayStack (int maxDepth) {
  148. // Konstrukcija na nov, prazen stek.
  149. elems = (E[]) new Object[maxDepth];
  150. depth = 0;
  151. }
  152.  
  153.  
  154. public boolean isEmpty () {
  155. // Vrakja true ako i samo ako stekot e prazen.
  156. return (depth == 0);
  157. }
  158.  
  159.  
  160. public E peek () {
  161. // Go vrakja elementot na vrvot od stekot.
  162. if (depth == 0)
  163. throw new NoSuchElementException();
  164. return elems[depth-1];
  165. }
  166.  
  167.  
  168. public void clear () {
  169. // Go prazni stekot.
  170. for (int i = 0; i < depth; i++) elems[i] = null;
  171. depth = 0;
  172. }
  173.  
  174.  
  175. public void push (E x) {
  176. // Go dodava x na vrvot na stekot.
  177. elems[depth++] = x;
  178. }
  179.  
  180.  
  181. public E pop () {
  182. // Go otstranuva i vrakja elementot shto e na vrvot na stekot.
  183. if (depth == 0)
  184. throw new NoSuchElementException();
  185. E topmost = elems[--depth];
  186. elems[depth] = null;
  187. return topmost;
  188. }
  189. }
  190.  
  191. public class PreorderNonRecursive {
  192.  
  193. public static void main(String[] args) throws Exception {
  194. int i, j, k;
  195. int index;
  196. String action;
  197.  
  198. String line;
  199.  
  200. BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  201. StringTokenizer st;
  202.  
  203. int N = Integer.parseInt(br.readLine());
  204.  
  205. BNode<String> nodes[] = new BNode[N];
  206. BTree<String> tree = new BTree<String>();
  207.  
  208. for (i=0;i<N;i++)
  209. nodes[i] = new BNode<String>();
  210.  
  211. for (i = 0; i < N; i++) {
  212. line = br.readLine();
  213. st = new StringTokenizer(line);
  214. index = Integer.parseInt(st.nextToken());
  215. nodes[index].info = st.nextToken();
  216. action = st.nextToken();
  217. if (action.equals("LEFT")) {
  218. tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.LEFT, nodes[index]);
  219. } else if (action.equals("RIGHT")) {
  220. tree.addChildNode(nodes[Integer.parseInt(st.nextToken())], BNode.RIGHT, nodes[index]);
  221. } else {
  222. // this node is the root
  223. tree.makeRootNode(nodes[index]);
  224. }
  225. }
  226.  
  227. br.close();
  228.  
  229. tree.PreorderNonRecursive();
  230.  
  231. }
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement