Advertisement
fatalryuu

c

Dec 19th, 2022
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.88 KB | None | 0 0
  1. package by.it.group151002.shatko.lesson10;
  2.  
  3. import java.util.*;
  4.  
  5. public class TaskC<E> implements NavigableSet<E> {
  6. private int amount = 0;
  7. private int i = 0;
  8. private Node<E> root;
  9. public int compareTo(E o, E nextO) {
  10. return Integer.compare((int)o, (int)nextO);
  11. }
  12. public class Node<E> {
  13. private E data;
  14. private Node<E> leftChild;
  15. private Node<E> rightChild;
  16. private Node<E> parent;
  17.  
  18. public void setLeftChild(Node<E> newNode) {
  19. leftChild = newNode;
  20. }
  21.  
  22. public void setRightChild(Node<E> newNode) {
  23. rightChild = newNode;
  24. }
  25.  
  26. public Node(E data) {
  27. this.data = data;
  28. }
  29. public Node<E> getLeftChild() {
  30. return leftChild;
  31. }
  32.  
  33. public Node<E> getRightChild() {
  34. return rightChild;
  35. }
  36.  
  37. public E getvalue() {
  38. return data;
  39. }
  40. }
  41. //Создайте БЕЗ использования других классов (включая абстрактные)
  42. //аналог дерева TreeSet
  43.  
  44. //Обязательные к реализации методы и конструкторы
  45. public TaskC() {
  46. }
  47.  
  48. @Override
  49. public boolean add(E e) {
  50. Node<E> current = root;
  51. Node<E> prev;
  52. Node<E> newNode = new Node<>(e);
  53. if(!contains(e)) {
  54. if (root == null) {
  55. root = newNode;
  56. root.parent = null;
  57. amount++;
  58. return true;
  59. } else {
  60. while (true) {
  61. prev = current;
  62. if (compareTo(current.getvalue(), e) > 0) {
  63. if (current.leftChild == null) {
  64. current.setLeftChild(newNode);
  65. current.leftChild.parent = current;
  66. amount++;
  67. return true;
  68. }
  69. current = current.getLeftChild();
  70. } else {
  71.  
  72. if (current.rightChild == null) {
  73. current.setRightChild(newNode);
  74. current.rightChild.parent = current;
  75. amount++;
  76. return true;
  77. }
  78. current = current.getRightChild();
  79. }
  80. }
  81. }
  82. }
  83. return false;
  84. }
  85.  
  86. @Override
  87. public boolean remove(Object o) {
  88. Node<E> current = root;
  89. boolean flag=false;
  90. while(!flag && amount>0){
  91. if(current==null) return false;
  92. if(current!=null && compareTo(current.getvalue(),(E)o)>0){
  93. current=current.getLeftChild();
  94. } else
  95. if(current!=null && compareTo(current.getvalue(),(E)o)<0){
  96. current=current.getRightChild();
  97. }
  98. if(current!=null && compareTo(current.getvalue(),(E)o)==0){
  99. flag = true;
  100. }
  101. }
  102. if(current.leftChild==null && current.rightChild==null){
  103. if(current.parent==null){
  104. root=null;
  105. } else
  106. if(current.parent.rightChild==current){
  107. current.parent.rightChild=null;
  108. }else {
  109. current.parent.leftChild = null;
  110. }
  111. amount--;
  112. return true;
  113. }
  114. if(current.leftChild==null || current.rightChild==null){
  115. if(current.leftChild==null){
  116. if(current.parent==null){
  117. root=current.getRightChild();
  118. } else
  119. if(current.parent.leftChild==current){
  120. current.rightChild.parent=current.parent;
  121. current.parent.leftChild=current.rightChild;
  122. } else
  123. if(current.parent.rightChild==current){
  124. current.rightChild.parent=current.parent;
  125. current.parent.rightChild = current.rightChild;
  126. }
  127. }
  128. if(current.rightChild==null){
  129. if(current.parent==null){
  130. root=current.getLeftChild();
  131. } else
  132. if(current.parent!=null && current.parent.rightChild==current){
  133. current.leftChild.parent=current.parent;
  134. current.parent.rightChild=current.leftChild;
  135. } else
  136. if(current.parent!=null && current.parent.leftChild==current){
  137. current.leftChild.parent=current.parent;
  138. current.parent.leftChild = current.leftChild;
  139. }
  140. }
  141. amount--;
  142. return true;
  143. }
  144. if(current.leftChild!=null && current.rightChild!=null){
  145. Node<E> tmp;
  146. tmp=current.getRightChild();
  147. boolean flag1=false;
  148. while(tmp.leftChild!=null){
  149. tmp=tmp.getLeftChild();
  150. flag1=true;
  151. }
  152. if(flag1){
  153. if(tmp.rightChild!=null){
  154. tmp.rightChild.parent=tmp.parent;
  155. tmp.parent.leftChild=tmp.rightChild;
  156. current.data=tmp.data;
  157. }
  158. if(tmp.rightChild==null){
  159. tmp.parent.leftChild=null;
  160. current.data=tmp.data;
  161. }
  162. amount--;
  163. return true;
  164. }
  165. else if(!flag1){
  166. if(tmp.rightChild!=null) {
  167. tmp.rightChild.parent = tmp.parent;
  168. tmp.parent.rightChild = tmp.rightChild;
  169. current.data = tmp.data;
  170. }
  171. if(tmp.rightChild==null){
  172. tmp.parent.rightChild=null;
  173. current.data=tmp.data;
  174. }
  175. amount--;
  176. return true;
  177. }
  178. amount--;
  179. return true;
  180. }
  181. return false;
  182. }
  183.  
  184. void inOrder(Node root, StringBuilder str){
  185. if(root==null){return;}
  186. inOrder(root.leftChild,str);
  187. str.append(root.data);
  188. str.append(", ");
  189. i++;
  190. inOrder(root.rightChild,str);
  191. }
  192.  
  193. @Override
  194. public String toString() {
  195. i=0;
  196. StringBuilder s = new StringBuilder();
  197. s.append('[');
  198. inOrder(root,s);
  199. if (s.length() < 2) {
  200. s.append("]");
  201. } else {
  202. s.delete(s.length()-2, s.length());
  203. s.append("]");
  204. }
  205. return s.toString();
  206. }
  207.  
  208. @Override
  209. public boolean contains(Object o) {
  210. Node<E> current = root;
  211. boolean flag=false;
  212. while(!flag && amount>0){
  213. if(current==null) return false;
  214. if(current!=null && compareTo(current.getvalue(),(E)o)>0){
  215. current=current.getLeftChild();
  216. }
  217. if(current!=null && compareTo(current.getvalue(),(E)o)<0){
  218. current=current.getRightChild();
  219. }
  220. if(current!=null && compareTo(current.getvalue(),(E)o)==0){
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226.  
  227. @Override
  228. public Iterator<E> iterator() {
  229. return null;
  230. }
  231.  
  232. @Override
  233. public void clear() {
  234. amount=0;
  235. root = null;
  236. }
  237.  
  238. @Override
  239. public boolean isEmpty() {
  240. return (amount==0);
  241. }
  242.  
  243. @Override
  244. public int size() {
  245. return amount;
  246. }
  247.  
  248. @Override
  249. public E first() {
  250. if(amount>0){
  251. Node<E> current = root;
  252. while(current.leftChild!=null){current= current.getLeftChild();}
  253. return current.data;
  254. }
  255. else return null;
  256. }
  257.  
  258. @Override
  259. public E last() {
  260. if(amount>0){
  261. Node<E> current = root;
  262. while(current.rightChild!=null){current= current.getRightChild();}
  263. return current.data;
  264. }
  265. else return null;
  266. }
  267.  
  268. void inArray (Node root, Object arr[]) {
  269. if (root == null)
  270. return;
  271. inArray(root.leftChild, arr);
  272. arr[i] = (int)root.data;
  273. i++;
  274. inArray(root.rightChild,arr);
  275. }
  276.  
  277. @Override
  278. public E lower(E e) { // < e
  279. Object[] A = new Object[amount];
  280. i = 0;
  281. inArray(root, A);
  282. Arrays.sort(A);
  283. int index = -1;
  284. for (int j = 0; j < amount; j++) {
  285. if ((int)A[j] >= (int)e) {
  286. index = j;
  287. break;
  288. }
  289. }
  290. if (index == -1 || index == 0)
  291. return null;
  292. return (E)A[index - 1];
  293. }
  294.  
  295. @Override
  296. public E floor(E e) { // <= e
  297. Object[] A = new Object[amount];
  298. i = 0;
  299. inArray(root,A);
  300. Arrays.sort(A);
  301. int index = -1;
  302. for (int j = 0; j < amount; j++) {
  303. if ((int)A[j] >= (int)e) {
  304. index = j;
  305. break;
  306. }
  307. }
  308. if (index == -1)
  309. return null;
  310. if ((int)A[index] == (int)e)
  311. return (E)A[index];
  312. if ((int)A[index] > (int)e)
  313. return (E)A[index - 1];
  314. return (E)A[index];
  315. }
  316.  
  317. @Override
  318. public E ceiling(E e) { // >=
  319. Object[] A = new Object[amount];
  320. i = 0;
  321. inArray(root,A);
  322. Arrays.sort(A);
  323. int index = -1;
  324. for (int j = 0; j < amount; j++){
  325. if ((int)A[j] >= (int)e) {
  326. index = j;
  327. break;
  328. }
  329. }
  330. if (index == -1)
  331. return null;
  332. return (E)A[index];
  333. }
  334.  
  335. @Override
  336. public E higher(E e) { // >
  337. Object[] A = new Object[amount];
  338. i = 0;
  339. inArray(root,A);
  340. Arrays.sort(A);
  341. int index = -1;
  342. for (int j = 0; j < amount; j++){
  343. if ((int)A[j] > (int)e) {
  344. index = j;
  345. break;
  346. }
  347. }
  348. if (index==-1 || index == amount - 1)
  349. return null;
  350. return (E)A[index];
  351. }
  352.  
  353. @Override
  354. public E pollFirst() {
  355. if (amount > 0) {
  356. Node<E> current = root;
  357. while (current.leftChild != null)
  358. current = current.getLeftChild();
  359. E el = current.data;
  360. remove(current.data);
  361. return el;
  362. }
  363. return null;
  364. }
  365.  
  366. @Override
  367. public E pollLast() {
  368. if (amount > 0) {
  369. Node<E> current = root;
  370. while (current.rightChild != null)
  371. current = current.getRightChild();
  372. E el = current.data;
  373. remove(current.data);
  374. return el;
  375. }
  376. return null;
  377. }
  378.  
  379. /////////////////////////////////////////////////////////////////////////
  380. /////////////////////////////////////////////////////////////////////////
  381. //////// Эти методы реализовывать необязательно ////////////
  382. /////////////////////////////////////////////////////////////////////////
  383. /////////////////////////////////////////////////////////////////////////
  384.  
  385. @Override
  386. public Object[] toArray() {
  387. return new Object[0];
  388. }
  389.  
  390. @Override
  391. public <T> T[] toArray(T[] a) {
  392. return null;
  393. }
  394.  
  395. @Override
  396. public boolean containsAll(Collection<?> c) {
  397. return false;
  398. }
  399.  
  400. @Override
  401. public boolean addAll(Collection<? extends E> c) {
  402. return false;
  403. }
  404.  
  405. @Override
  406. public boolean retainAll(Collection<?> c) {
  407. return false;
  408. }
  409.  
  410. @Override
  411. public boolean removeAll(Collection<?> c) {
  412. return false;
  413. }
  414.  
  415. @Override
  416. public NavigableSet<E> descendingSet() {
  417. return null;
  418. }
  419.  
  420. @Override
  421. public Iterator<E> descendingIterator() {
  422. return null;
  423. }
  424.  
  425. @Override
  426. public NavigableSet<E> subSet(E fromElement, boolean fromInclusive, E toElement, boolean toInclusive) {
  427. return null;
  428. }
  429.  
  430. @Override
  431. public NavigableSet<E> headSet(E toElement, boolean inclusive) {
  432. return null;
  433. }
  434.  
  435. @Override
  436. public NavigableSet<E> tailSet(E fromElement, boolean inclusive) {
  437. return null;
  438. }
  439.  
  440. @Override
  441. public Comparator<? super E> comparator() {
  442. return null;
  443. }
  444.  
  445. @Override
  446. public SortedSet<E> subSet(E fromElement, E toElement) {
  447. return null;
  448. }
  449.  
  450. @Override
  451. public SortedSet<E> headSet(E toElement) {
  452. return null;
  453. }
  454.  
  455. @Override
  456. public SortedSet<E> tailSet(E fromElement) {
  457. return null;
  458. }
  459.  
  460.  
  461. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement