Advertisement
Guest User

Untitled

a guest
Dec 16th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package adtpp;
  7.  
  8. /**
  9. *
  10. * @author Denis
  11. */
  12. public class BinaryTree {
  13.  
  14. private BinaryNode root;
  15. private int positions = 0;
  16.  
  17. public BinaryTree()
  18. {
  19.  
  20. }
  21.  
  22. public BinaryTree(Person p)
  23. {
  24. root = new BinaryNode(p);
  25. positions = 3;
  26. }
  27.  
  28. public void add(Person p)
  29. {
  30. if(root == null)
  31. {
  32. root = new BinaryNode(p);
  33. positions = 3;
  34. return;
  35. }
  36. BinaryNode temp = root;
  37. while(true)
  38. {
  39. if(temp.getData().getAge() > p.getAge())
  40. {
  41. if(temp.getLeft() == null)
  42. {
  43. temp.setLeft(new BinaryNode(p));
  44. positions = positions + 2;
  45. return;
  46. }
  47. temp = temp.getLeft();
  48. }
  49. else
  50. {
  51. if(temp.getRight() == null)
  52. {
  53. temp.setRight(new BinaryNode(p));
  54. positions = positions + 2;
  55. return;
  56. }
  57. temp = temp.getRight();
  58. }
  59. }
  60. }
  61.  
  62. public String toString()
  63. {
  64. if(root != null)
  65. {
  66. return toString(root.getLeft()) + " " + root.getData() + " " + toString(root.getRight());
  67. }
  68. return "empty";
  69. }
  70. private String toString(BinaryNode btnode)
  71. {
  72. if(btnode != null)
  73. {
  74. return toString(btnode.getLeft()) + " " + btnode.getData() + " " + toString(btnode.getRight());
  75. }
  76. return "";
  77. }
  78. }
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105. /*
  106. * To change this license header, choose License Headers in Project Properties.
  107. * To change this template file, choose Tools | Templates
  108. * and open the template in the editor.
  109. */
  110. package adtpp;
  111.  
  112. /**
  113. *
  114. * @author Denis
  115. */
  116. public class DList {
  117.  
  118. private Node head;
  119.  
  120. public DList(Person p)
  121. {
  122. head = new Node(p);
  123. }
  124.  
  125. public void add(Person p)
  126. {
  127. if(head == null)
  128. {
  129. head = new Node(p);
  130. return;
  131. }
  132. if(head.getData().getAge() >= p.getAge())
  133. {
  134. head = new Node(p, head);
  135. return;
  136. }
  137. Node temp = head;
  138. if(temp.getNext() != null && temp.getNext().getData().getAge() < p.getAge())
  139. {
  140. temp = temp.getNext();
  141. }
  142. Node n = new Node(p, temp.getNext());
  143. temp.setNext(n);
  144. }
  145.  
  146. public Person remove(String name, int age)
  147. {
  148. Person returnedPerson = null;
  149. if(head == null)
  150. {
  151. return null;
  152. }
  153. if(head.getData().getName().equals(name) && head.getData().getAge() == age)
  154. {
  155. returnedPerson = head.getData();
  156. head = head.getNext();
  157. return returnedPerson;
  158. }
  159. Node temp = head;
  160. while(temp.getNext() != null)
  161. {
  162. if(temp.getNext().getData().getName().equals(name) && temp.getNext().getData().getAge() == age)
  163. {
  164. returnedPerson = temp.getNext().getData();
  165. temp.setNext(temp.getNext().getNext());
  166. return returnedPerson;
  167. }
  168. temp = temp.getNext();
  169. }
  170. return returnedPerson;
  171. }
  172.  
  173. public String toString()
  174. {
  175. String s = "";
  176. Node temp = head;
  177. while(temp != null)
  178. {
  179. s = s + temp.getData() + " ";
  180. temp = temp.getNext();
  181. }
  182. return s;
  183. }
  184.  
  185. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement