Advertisement
Guest User

Untitled

a guest
Mar 4th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.46 KB | None | 0 0
  1. import tester.Tester;
  2.  
  3. //-------------------------------------------------------------------------------------------------
  4.  
  5. //-------------------------------------------------------------------------------------------------
  6.  
  7. //Representation for a Book
  8.  
  9. class Book {
  10. String title;
  11. String author;
  12. int price;
  13.  
  14. Book(String title, String author, int price) {
  15. this.title = title;
  16. this.author = author;
  17. this.price = price;
  18. }
  19.  
  20. }
  21.  
  22. //-------------------------------------------------------------------------------------------------
  23. //-------------------------------------------------------------------------------------------------
  24. //Interface for any compare function object
  25. interface IComparator<T> {
  26. // apply function that lets you compare books by either
  27. // title, author, or price
  28. int apply(T data, T item);
  29. }
  30.  
  31. //-------------------------------------------------------------------------------------------------
  32. class BooksByTitle implements IComparator<Book> {
  33. // compares books by title lexicographically
  34. public int apply(Book b1, Book b2) {
  35. return b1.title.compareTo(b2.title);
  36. }
  37. }
  38. //-------------------------------------------------------------------------------------------------
  39. class BooksByAuthor implements IComparator<Book> {
  40. //compares books by author lexicographically
  41. public int apply(Book b1, Book b2) {
  42. return b1.author.compareTo(b2.title);
  43. }
  44. }
  45. //-------------------------------------------------------------------------------------------------
  46. class BooksByPrice implements IComparator<Book> {
  47. // compares books by price (cheapest comes first)
  48. public int apply(Book b1, Book b2) {
  49. return (b1.price - b2.price);
  50. }
  51. }
  52. //-------------------------------------------------------------------------------------------------
  53. //-------------------------------------------------------------------------------------------------
  54. //Abstract Binary Search Tree
  55. abstract class ABST<T> {
  56. IComparator<T> order;
  57.  
  58. ABST(IComparator<T> order) {
  59. this.order = order;
  60. }
  61. //Inserting new item according to current IComparator
  62. abstract ABST<T> insert(T item);
  63. //Getting leftmost item of tree
  64. abstract Node<T> getLeftMost();
  65. //Helper for getLeftMost
  66. abstract Node<T> getLeftMostHelper(Node<T> n);
  67. abstract ABST<T> getRight();
  68. abstract boolean isLeaf();
  69. abstract boolean sameTree(ABST<T> t);
  70. abstract boolean sameLeaf(Leaf<T> t);
  71. abstract boolean sameNode(Node<T> t);
  72. abstract boolean sameData(ABST<T> t);
  73. abstract boolean sameDataLeaf(Leaf<T> t);
  74. abstract boolean sameDataNode(Node<T> t);
  75. }
  76.  
  77. //-------------------------------------------------------------------------------------------------
  78. //Representation for a leaf
  79. class Leaf<T> extends ABST<T> {
  80. Leaf(IComparator<T> order) {
  81. super(order);
  82. }
  83.  
  84. //Inserts item according to current order
  85. public ABST<T> insert(T item) {
  86. return new Node<T>(this.order, item, new Leaf<T>(this.order), new Leaf<T>(this.order));
  87. }
  88. //Getting leftmost item of tree
  89. public Node<T> getLeftMost() {
  90. throw new RuntimeException("No leftmost item of an empty tree");
  91. }
  92. //Helper for getLeftMost
  93. public Node<T> getLeftMostHelper(Node<T> n) {
  94. return n;
  95. }
  96. //Returns all of tree but the leftmost
  97. public ABST<T> getRight() {
  98. throw new RuntimeException("No right of an empty tree");
  99. }
  100. //Sees if a given tree is a leaf
  101. public boolean isLeaf() {
  102. return true;
  103. }
  104. //Compares two trees
  105. public boolean sameTree(ABST<T> t) {
  106. return t.sameLeaf(this);
  107. }
  108. //Compares two leaves
  109. public boolean sameLeaf(Leaf<T> that) {
  110. return true;
  111. }
  112. //Returns false, as no leaf and node are equal
  113. public boolean sameNode(Node<T> that) {
  114. return false;
  115. }
  116. public boolean sameData(ABST<T> that) {
  117. return that.sameDataLeaf(this);
  118. }
  119. public boolean sameDataLeaf(Leaf<T> that) {
  120. return true;
  121. }
  122. public boolean sameDataNode(Node<T> that) {
  123. return false;
  124. }
  125. }
  126. //-------------------------------------------------------------------------------------------------
  127. //Representation for a node
  128. class Node<T> extends ABST<T> {
  129. T data;
  130. ABST<T> left;
  131. ABST<T> right;
  132.  
  133. //Constructor
  134. Node(IComparator<T> order, T data, ABST<T> left, ABST<T> right) {
  135. super(order);
  136. this.data = data;
  137. this.left = left;
  138. this.right = right;
  139. }
  140.  
  141. //Inserts item according to current order
  142. public ABST<T> insert(T item) {
  143. if (this.order.apply(this.data, item) < 0) {
  144. return new Node<T>(this.order, this.data,
  145. new Node<T>(this.order, item, this.left, new Leaf<T>(this.order)), this.right);
  146. }
  147. else {
  148. return new Node<T>(this.order, this.data, this.left, this.right.insert(item));
  149. }
  150. }
  151. //Helper for getLeftMost
  152. public Node<T> getLeftMostHelper(Node<T> n) {
  153. return this.left.getLeftMostHelper(this);
  154. }
  155. //Getting leftmost item of tree
  156. public Node<T> getLeftMost() {
  157. return getLeftMostHelper(this);
  158. }
  159. //Returns all of tree but the leftmost
  160. public Node<T> getRightHelper(Node<T> n, Node<T>n1) {
  161. return getRightHelper(this, n);
  162. }
  163. //Returns all of tree but the leftmost
  164. public ABST<T> getRight() {
  165. if(this.left.isLeaf())
  166. return this.left;
  167. else {
  168. return new Node<T>(this.order, this.data, this.left.getRight(), this.right);
  169. }
  170.  
  171. }
  172. //Sees if node is a leaf(always false)
  173. public boolean isLeaf() {
  174. return false;
  175. }
  176. //Compares two trees
  177. public boolean sameTree(ABST<T> that) {
  178. return that.sameNode(this);
  179. }
  180. //Always fasle
  181. public boolean sameLeaf(Leaf<T> that) {
  182. return false;
  183. }
  184. //Compares two nodes
  185. public boolean sameNode(Node<T> that) {
  186. return this.order.apply(this.data, that.data) == 0;
  187. }
  188. public boolean sameData(ABST<T> that) {
  189. return that.sameDataNode(this);
  190. }
  191. public boolean sameDataLeaf(Leaf<T> that) {
  192. return false;
  193. }
  194. public boolean sameDataNode(Node<T> that) {
  195. return this.allIn(that) && that.allIn(this);
  196. }
  197. public boolean allIn(Node<T> that) {
  198. return this.data.in(that) && this.left.allIn(that) && this.right.allIn(that);
  199.  
  200. }
  201. }
  202.  
  203. //-------------------------------------------------------------------------------------------------
  204. //-------------------------------------------------------------------------------------------------
  205. //Examples class
  206. class ABSTExamples {
  207.  
  208. String razzaq = "Razzaq";
  209. //Book examples
  210. Book b1 = new Book("HTDP", razzaq, 7);
  211. Book b2 = new Book("Psychology", "Neil", 99);
  212. Book b3 = new Book("What is?", "Yudkovich", 44);
  213. Book b4 = new Book("Am I doing this right?", "Descartes", 44);
  214. Book b5 = new Book("HTDP", razzaq, 44);
  215.  
  216. //ABST examples
  217. ABST<Book> t1 = new Leaf<Book>(new BooksByAuthor());
  218. ABST<Book> t2 = new Node<Book>(new BooksByAuthor(), b1, new Leaf<Book>(new BooksByAuthor()),
  219. new Leaf<Book>(new BooksByAuthor()));
  220. ABST<Book> t3 = new Node<Book>(new BooksByTitle(), b4, t2, t1);
  221. ABST<Book> t4 = new Node<Book>(new BooksByPrice(), b5, t3, t2);
  222. //Test for BooksByAuthor
  223. boolean testBooksByAuthor(Tester t) {
  224. return t.checkExpect((new BooksByAuthor().apply(this.b1, this.b2) > 0), true) &&
  225. t.checkExpect((new BooksByAuthor().apply(this.b1, this.b3) < 0), true) &&
  226. t.checkExpect((new BooksByAuthor().apply(this.b1, this.b5) == 0), true);
  227. }
  228. //Test for BooksByTitle
  229. boolean testBooksByTitle(Tester t) {
  230. return t.checkExpect((new BooksByTitle().apply(this.b1, this.b2) < 0), true) &&
  231. t.checkExpect((new BooksByTitle().apply(this.b3, this.b2) > 0), true) &&
  232. t.checkExpect((new BooksByTitle().apply(this.b5, this.b1) == 0), true);
  233. }
  234. //Test for BooksByPrice
  235. boolean testBooksByPrice(Tester t) {
  236. return t.checkExpect((new BooksByPrice().apply(this.b1, this.b2) < 0), true) &&
  237. t.checkExpect((new BooksByPrice().apply(this.b3, this.b1) > 0), true) &&
  238. t.checkExpect((new BooksByPrice().apply(this.b4, this.b5) == 0), true);
  239. }
  240. //Test for insert
  241. boolean testInsert(Tester t) {
  242. return t.checkExpect(t1.insert(b1),t2) &&
  243. t.checkExpect(t2.insert(b5), new Node<Book>(new BooksByAuthor(), b1,
  244. new Leaf<Book>(new BooksByAuthor()),
  245. new Node<Book>(new BooksByAuthor(), b5, new Leaf<Book>(new BooksByAuthor()),
  246. new Leaf<Book>(new BooksByAuthor())))) &&
  247. t.checkExpect(t2.insert(b3), new Node<Book>(new BooksByAuthor(), b1,
  248. new Node<Book>(new BooksByAuthor(), b3, new Leaf<Book>(new BooksByAuthor()),
  249. new Leaf<Book>(new BooksByAuthor())),
  250. new Leaf<Book>(new BooksByAuthor()))) &&
  251. t.checkExpect(t2.insert(b4), new Node<Book>(new BooksByAuthor(), b1,
  252. new Leaf<Book>(new BooksByAuthor()),
  253. new Node<Book>(new BooksByAuthor(), b4, new Leaf<Book>(new BooksByAuthor()),
  254. new Leaf<Book>(new BooksByAuthor()))));
  255. }
  256. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement