Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.56 KB | None | 0 0
  1. import java.io.PrintStream;
  2.  
  3. public class ST {
  4.  
  5. private class TreeNode {
  6. int id;
  7. String city;
  8. TreeNode l;
  9. TreeNode r;
  10. int N;
  11. List booklist = new List();
  12.  
  13. TreeNode(int id, String city) {
  14. this.id = id;
  15. this.city = city;
  16. }
  17. }
  18.  
  19. private TreeNode head;
  20.  
  21. void insertWarehouse(int nodeId, String name) {
  22. if (searchWarehouse(nodeId) != null) {
  23. System.out.println("Warehouse with this id already exists.");
  24. }
  25. head = insertR(head, new TreeNode(nodeId, name));
  26. System.out.println("New warehouse added.");
  27. }
  28.  
  29. void insertBookAtWarehouse(int nodeId, int isbn, int copies) {
  30. TreeNode targetWarehouse = searchWarehouse(nodeId);
  31. if (targetWarehouse == null){
  32. System.out.println("Warehouse doesn't exist.");
  33. } else{
  34. targetWarehouse.booklist.insert(new Node(new BookInfo(isbn, copies)));
  35. }
  36. }
  37.  
  38. void removeWarehouse(int nodeId) {
  39. head = removeWarehouse(head, nodeId);
  40. }
  41.  
  42. private TreeNode removeWarehouse(TreeNode node, int nodeId) {
  43. if(node == null) return null;
  44. if(nodeId < node.id){
  45. node.l = removeWarehouse(node.l, nodeId);
  46. }
  47. else if(nodeId > node.id){
  48. node.r = removeWarehouse(node.r , nodeId);
  49. }
  50. else{
  51. node = joinTree(node.l, node.r);
  52. if(node!=null)
  53. node.N--;
  54. }
  55. return node;
  56. }
  57.  
  58. private TreeNode joinTree(TreeNode left, TreeNode right){
  59. if(left == null)
  60. return right;
  61. if(right == null)
  62. return left;
  63. int N = left.N + right.N;
  64. if(Math.random() * N < left.N){
  65. left.r = joinTree(left.r, right);
  66. return left;
  67. }
  68. else{
  69. right.l = joinTree(left, right.l);
  70. return right;
  71. }
  72. }
  73.  
  74. void removeBook(int nodeId, int isbn) {
  75. TreeNode targetWarehouse = searchWarehouse(nodeId);
  76. if(targetWarehouse==null){
  77. System.out.println("Warehouse doesn't exist.");
  78. }
  79. else{
  80. Node book = targetWarehouse.booklist.getBook(isbn);
  81. if (book != null) {
  82. targetWarehouse.booklist.remove(book);
  83. } else {
  84. System.out.println("Book doesn't exist in warehouse.");
  85. }
  86. }
  87. }
  88.  
  89. void searchByWarehouse(int nodeId) {
  90. TreeNode cursor = head;
  91. while(cursor != null){
  92. if(nodeId == cursor.id){
  93. cursor.booklist.print(System.out);
  94. return;
  95. }
  96. cursor = nodeId < cursor.id ? cursor.l : cursor.r;
  97. }
  98. }
  99.  
  100. void searchBookInWarehouse(int nodeId, int isbn) {
  101. TreeNode toCheck = searchWarehouse(nodeId);
  102.  
  103. if(toCheck == null){
  104. System.out.println("Warehouse not found!");
  105. } else {
  106. Node book = toCheck.booklist.getBook(isbn);
  107. if (book == null) {
  108. System.out.println("Book not found in warehouse!");
  109. } else {
  110. System.out.println("Book found: " + book.getInfo().toString());
  111. }
  112. }
  113. }
  114.  
  115. void searchBook(int isbn) {
  116. searchBook(head, isbn);
  117. }
  118.  
  119. private void searchBook(TreeNode node, int isbn) {
  120. if (node == null) return;
  121. Node book = node.booklist.getBook(isbn);
  122. if (book != null) {
  123. System.out.println("Book found in warehouse " + node.id + ": " + book.getInfo().toString());
  124. }
  125. searchBook(node.r, isbn);
  126. searchBook(node.l, isbn);
  127. }
  128.  
  129. void printΤree(PrintStream stream) {
  130. printΤree(head, stream);
  131. }
  132.  
  133. private void printΤree(TreeNode node, PrintStream stream) {
  134. if (node == null) return;
  135. System.out.println("Warehouse " + node.id + " (" + node.city +"):");
  136. node.booklist.print(stream);
  137. printΤree(node.l, stream);
  138. printΤree(node.r, stream);
  139. }
  140.  
  141. private TreeNode searchWarehouse(int nodeId){
  142. TreeNode cursor = head;
  143. while(cursor != null){
  144. if(nodeId == cursor.id){
  145. return cursor;
  146. }
  147. cursor = nodeId < cursor.id ? cursor.l : cursor.r;
  148. }
  149. return null;
  150. }
  151.  
  152. TreeNode rotR(TreeNode h){ //rotate right
  153. TreeNode x = h.l;
  154. h.l=x.r;
  155. x.r=h;
  156. return x;
  157. }
  158.  
  159. TreeNode rotL(TreeNode h){ //rotate left
  160. TreeNode x= h.r;
  161. h.r=x.l;
  162. x.l=h;
  163. return x;
  164. }
  165.  
  166. private TreeNode insertR(TreeNode parent, TreeNode current) {
  167. if (parent == null) {
  168. return current;
  169. }
  170. if(Math.random()*(parent.N + 1)<1) {
  171. return insertS(parent, current);
  172. }
  173. if(current.id < parent.id){
  174. parent.l = insertR(parent.l , current);
  175. }
  176. else{
  177. parent.r = insertR(parent.r , current);
  178. }
  179. parent.N++;
  180. return parent;
  181. }
  182.  
  183. private TreeNode insertS(TreeNode parent, TreeNode current){
  184. if (parent == null) {
  185. return current;
  186. }
  187. if(current.id < parent.id){
  188. parent.l = insertS(parent.l, current);
  189. parent = rotR(parent);
  190. }
  191. else{
  192. parent.r = insertS(parent.r, current);
  193. parent = rotL(parent);
  194. }
  195. parent.N++;
  196. return parent;
  197. }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement