fork download
  1. public class Main
  2. {
  3. public static void main(String[] args)
  4. {
  5. java.util.Random r = new java.util.Random();
  6. Tree tree = new Tree();
  7. int[] a = seq(256);
  8. sfl(a, r);
  9. for (int i : a)
  10. {
  11. tree.add(i);
  12. }
  13. for (int i = 0; i < a.length - 15; i++)
  14. {
  15. tree.remove(a[i]);
  16. }
  17. tree.print();
  18. }
  19.  
  20. static int[] rnd(int c, java.util.Random r)
  21. {
  22. int[] a = new int[c];
  23. for (int i = 0; i < c; i++)
  24. {
  25. a[i] = r.nextInt(c);
  26. }
  27. return a;
  28. }
  29.  
  30. static int[] seq(int c)
  31. {
  32. int[] a = new int[c];
  33. for (int i = 0; i < c; i++)
  34. {
  35. a[i] = i;
  36. }
  37. return a;
  38. }
  39.  
  40. static void rev(int a[])
  41. {
  42. int i = 0;
  43. int j = a.length - 1;
  44. while (i < j)
  45. {
  46. int t = a[i];
  47. a[i] = a[j];
  48. a[j] = t;
  49. i++;
  50. j--;
  51. }
  52. }
  53.  
  54. static void sfl(int a[], java.util.Random r)
  55. {
  56. for (int i = a.length; 0 < i; i--)
  57. {
  58. int j = r.nextInt(i);
  59. int t = a[j];
  60. a[j] = a[i - 1];
  61. a[i - 1] = t;
  62. }
  63. }
  64. }
  65.  
  66. class Tree
  67. {
  68. Node root;
  69. Node nullNode;
  70.  
  71. public Tree()
  72. {
  73. nullNode = new Node(0, null, null, Color.black);
  74. root = nullNode;
  75. }
  76.  
  77. public void add(int value)
  78. {
  79. Node node = new Node(value, nullNode, nullNode, Color.red);
  80. Adder adder = new Adder(node);
  81. adder.add();
  82. }
  83.  
  84. public void remove(int value)
  85. {
  86. Remover remover = new Remover(value);
  87. remover.remove();
  88. }
  89.  
  90. public void print()
  91. {
  92. Printer printer = new Printer();
  93. printer.print();
  94. }
  95.  
  96. enum Color
  97. {
  98. red,
  99. black
  100. }
  101.  
  102. class Node
  103. {
  104. int value;
  105. Node left;
  106. Node right;
  107. Color color;
  108.  
  109. Node(int value, Node left, Node right, Color color)
  110. {
  111. this.value = value;
  112. this.left = left;
  113. this.right = right;
  114. this.color = color;
  115. }
  116. }
  117.  
  118. class Remover
  119. {
  120. int removeValue;
  121. Node resNode;
  122. int minValue;
  123. boolean balanced;
  124. boolean removed;
  125.  
  126. Remover(int removeValue)
  127. {
  128. this.removeValue = removeValue;
  129. this.resNode = nullNode;
  130. this.balanced = false;
  131. this.removed = false;
  132. }
  133.  
  134. void remove()
  135. {
  136. if (root == nullNode)
  137. {
  138. }
  139. else
  140. {
  141. remove(root);
  142. if (removed)
  143. {
  144. root = resNode;
  145. }
  146. }
  147. }
  148.  
  149. void remove(Node node)
  150. {
  151. if (removeValue < node.value)
  152. {
  153. if (node.left == nullNode)
  154. {
  155. removed = false;
  156. balanced = true;
  157. }
  158. else
  159. {
  160. remove(node.left);
  161. if (removed)
  162. {
  163. node.left = resNode;
  164. balanceL(node);
  165. }
  166. }
  167. }
  168. else
  169. {
  170. if (node.value < removeValue)
  171. {
  172. if (node.right == nullNode)
  173. {
  174. removed = false;
  175. balanced = true;
  176. }
  177. else
  178. {
  179. remove(node.right);
  180. if (removed)
  181. {
  182. node.right = resNode;
  183. balanceR(node);
  184. }
  185. }
  186. }
  187. else
  188. {
  189. removed = true;
  190. if (node.left == nullNode)
  191. {
  192. if (node.right == nullNode)
  193. {
  194. resNode = nullNode;
  195. balanced = node.color == Color.red;
  196. }
  197. else
  198. {
  199. resNode = node.right;
  200. resNode.color = Color.black;
  201. balanced = true;
  202. }
  203. }
  204. else
  205. {
  206. if (node.right == nullNode)
  207. {
  208. resNode = node.left;
  209. resNode.color = Color.black;
  210. balanced = true;
  211. }
  212. else
  213. {
  214. removeMin(node.right);
  215. node.right = resNode;
  216. node.value = minValue;
  217. balanceR(node);
  218. }
  219. }
  220. }
  221. }
  222. }
  223.  
  224. void removeMin(Node node)
  225. {
  226. if (node.left == nullNode)
  227. {
  228. minValue = node.value;
  229. resNode = node.right;
  230. balanced = node.color == Color.red;
  231. }
  232. else
  233. {
  234. removeMin(node.left);
  235. node.left = resNode;
  236. balanceL(node);
  237. }
  238. }
  239.  
  240. void balanceR(Node a)
  241. {
  242. if (balanced)
  243. {
  244. resNode = a;
  245. }
  246. else
  247. {
  248. Node b = a.left;
  249. Node c = b.right;
  250. Node d = b.left;
  251. if (a.color == Color.black)
  252. {
  253. if (b.color == Color.red)
  254. {
  255. Node e = c.right;
  256. Node f = c.left;
  257. if (e.color == Color.red)
  258. {
  259. resNode = rotateR3(a);
  260. e.color = Color.black;
  261. balanced = true;
  262. }
  263. else
  264. {
  265. if (f.color == Color.red)
  266. {
  267. resNode = rotateR2(a);
  268. f.color = Color.black;
  269. balanced = true;
  270. }
  271. else
  272. {
  273. resNode = rotateR1(a);
  274. b.color = Color.black;
  275. c.color = Color.red;
  276. balanced = true;
  277. }
  278. }
  279. }
  280. else
  281. {
  282. if (c.color == Color.red)
  283. {
  284. resNode = rotateR2(a);
  285. c.color = Color.black;
  286. balanced = true;
  287. }
  288. else
  289. {
  290. if (d.color == Color.red)
  291. {
  292. resNode = rotateR1(a);
  293. d.color = Color.black;
  294. balanced = true;
  295. }
  296. else
  297. {
  298. resNode = a;
  299. b.color = Color.red;
  300. }
  301. }
  302. }
  303. }
  304. else
  305. {
  306. if (c.color == Color.red)
  307. {
  308. resNode = rotateR2(a);
  309. a.color = Color.black;
  310. balanced = true;
  311. }
  312. else
  313. {
  314. if (d.color == Color.red)
  315. {
  316. resNode = rotateR1(a);
  317. a.color = Color.black;
  318. b.color = Color.red;
  319. d.color = Color.black;
  320. balanced = true;
  321. }
  322. else
  323. {
  324. resNode = a;
  325. a.color = Color.black;
  326. b.color = Color.red;
  327. balanced = true;
  328. }
  329. }
  330. }
  331. }
  332. }
  333.  
  334. Node rotateR1(Node a)
  335. {
  336. Node b = a.left;
  337. a.left = b.right;
  338. b.right = a;
  339. return b;
  340. }
  341.  
  342. Node rotateR2(Node a)
  343. {
  344. Node b = a.left;
  345. Node c = b.right;
  346. b.right = c.left;
  347. a.left = c.right;
  348. c.left = b;
  349. c.right = a;
  350. return c;
  351. }
  352.  
  353. Node rotateR3(Node a)
  354. {
  355. Node b = a.left;
  356. Node c = b.right;
  357. Node e = c.right;
  358. c.right = e.left;
  359. a.left = e.right;
  360. e.left = b;
  361. e.right = a;
  362. return e;
  363. }
  364.  
  365. void balanceL(Node a)
  366. {
  367. if (balanced)
  368. {
  369. resNode = a;
  370. }
  371. else
  372. {
  373. Node b = a.right;
  374. Node c = b.left;
  375. Node d = b.right;
  376. if (a.color == Color.black)
  377. {
  378. if (b.color == Color.red)
  379. {
  380. Node e = c.left;
  381. Node f = c.right;
  382. if (e.color == Color.red)
  383. {
  384. resNode = rotateL3(a);
  385. e.color = Color.black;
  386. balanced = true;
  387. }
  388. else
  389. {
  390. if (f.color == Color.red)
  391. {
  392. resNode = rotateL2(a);
  393. f.color = Color.black;
  394. balanced = true;
  395. }
  396. else
  397. {
  398. resNode = rotateL1(a);
  399. b.color = Color.black;
  400. c.color = Color.red;
  401. balanced = true;
  402. }
  403. }
  404. }
  405. else
  406. {
  407. if (c.color == Color.red)
  408. {
  409. resNode = rotateL2(a);
  410. c.color = Color.black;
  411. balanced = true;
  412. }
  413. else
  414. {
  415. if (d.color == Color.red)
  416. {
  417. resNode = rotateL1(a);
  418. d.color = Color.black;
  419. balanced = true;
  420. }
  421. else
  422. {
  423. resNode = a;
  424. b.color = Color.red;
  425. }
  426. }
  427. }
  428. }
  429. else
  430. {
  431. if (c.color == Color.red)
  432. {
  433. resNode = rotateL2(a);
  434. a.color = Color.black;
  435. balanced = true;
  436. }
  437. else
  438. {
  439. if (d.color == Color.red)
  440. {
  441. resNode = rotateL1(a);
  442. a.color = Color.black;
  443. b.color = Color.red;
  444. d.color = Color.black;
  445. balanced = true;
  446. }
  447. else
  448. {
  449. resNode = a;
  450. a.color = Color.black;
  451. b.color = Color.red;
  452. balanced = true;
  453. }
  454. }
  455. }
  456. }
  457. }
  458.  
  459. Node rotateL1(Node a)
  460. {
  461. Node b = a.right;
  462. a.right = b.left;
  463. b.left = a;
  464. return b;
  465. }
  466.  
  467. Node rotateL2(Node a)
  468. {
  469. Node b = a.right;
  470. Node c = b.left;
  471. b.left = c.right;
  472. c.right = b;
  473. a.right = c.left;
  474. c.left = a;
  475. return c;
  476. }
  477.  
  478. Node rotateL3(Node a)
  479. {
  480. Node b = a.right;
  481. Node c = b.left;
  482. Node e = c.left;
  483. c.left = e.right;
  484. e.right = b;
  485. a.right = e.left;
  486. e.left = a;
  487. return e;
  488. }
  489. }
  490.  
  491. class Adder
  492. {
  493. Node addNode;
  494. Node resNode;
  495. boolean balanced;
  496. boolean added;
  497.  
  498. Adder(Node addNode)
  499. {
  500. this.addNode = addNode;
  501. this.resNode = nullNode;
  502. this.balanced = false;
  503. this.added = false;
  504. }
  505.  
  506. void add()
  507. {
  508. if (root == nullNode)
  509. {
  510. root = addNode;
  511. }
  512. else
  513. {
  514. add(root);
  515. if (added)
  516. {
  517. root = resNode;
  518. root.color = Color.black;
  519. }
  520. }
  521. }
  522.  
  523. Node rotateR1(Node a)
  524. {
  525. Node b = a.left;
  526. a.left = b.right;
  527. b.right = a;
  528. return b;
  529. }
  530.  
  531. Node rotateR2(Node a)
  532. {
  533. Node b = a.left;
  534. Node e = b.right;
  535. a.left = e.right;
  536. b.right = e.left;
  537. e.right = a;
  538. e.left = b;
  539. return e;
  540. }
  541.  
  542. Node rotateL1(Node a)
  543. {
  544. Node b = a.right;
  545. a.right = b.left;
  546. b.left = a;
  547. return b;
  548. }
  549.  
  550. Node rotateL2(Node a)
  551. {
  552. Node b = a.right;
  553. Node e = b.left;
  554. a.right = e.left;
  555. b.left = e.right;
  556. e.left = a;
  557. e.right = b;
  558. return e;
  559. }
  560.  
  561. void balanceL(Node a)
  562. {
  563. if (balanced)
  564. {
  565. resNode = a;
  566. }
  567. else
  568. {
  569. if (a.color == Color.black)
  570. {
  571. Node b = a.left;
  572. Node c = a.right;
  573. Node d = b.left;
  574. Node e = b.right;
  575. if (c.color == Color.black)
  576. {
  577. if (e.color == Color.red)
  578. {
  579. resNode = rotateR2(a);
  580. a.color = Color.red;
  581. e.color = Color.black;
  582. balanced = true;
  583. }
  584. else
  585. {
  586. if (d.color == Color.red)
  587. {
  588. resNode = rotateR1(a);
  589. a.color = Color.red;
  590. b.color = Color.black;
  591. balanced = true;
  592. }
  593. else
  594. {
  595. resNode = a;
  596. balanced = true;
  597. }
  598. }
  599. }
  600. else
  601. {
  602. if (e.color == Color.red || d.color == Color.red)
  603. {
  604. resNode = a;
  605. a.color = Color.red;
  606. b.color = Color.black;
  607. c.color = Color.black;
  608. }
  609. else
  610. {
  611. resNode = a;
  612. balanced = true;
  613. }
  614. }
  615. }
  616. else
  617. {
  618. resNode = a;
  619. }
  620. }
  621. }
  622.  
  623. void balanceR(Node a)
  624. {
  625. if (balanced)
  626. {
  627. resNode = a;
  628. }
  629. else
  630. {
  631. if (a.color == Color.black)
  632. {
  633. Node b = a.right;
  634. Node c = a.left;
  635. Node d = b.right;
  636. Node e = b.left;
  637. if (c.color == Color.black)
  638. {
  639. if (e.color == Color.red)
  640. {
  641. resNode = rotateL2(a);
  642. a.color = Color.red;
  643. e.color = Color.black;
  644. balanced = true;
  645. }
  646. else
  647. {
  648. if (d.color == Color.red)
  649. {
  650. resNode = rotateL1(a);
  651. a.color = Color.red;
  652. b.color = Color.black;
  653. balanced = true;
  654. }
  655. else
  656. {
  657. resNode = a;
  658. balanced = true;
  659. }
  660. }
  661. }
  662. else
  663. {
  664. if (e.color == Color.red || d.color == Color.red)
  665. {
  666. resNode = a;
  667. a.color = Color.red;
  668. b.color = Color.black;
  669. c.color = Color.black;
  670. }
  671. else
  672. {
  673. resNode = a;
  674. balanced = true;
  675. }
  676. }
  677. }
  678. else
  679. {
  680. resNode = a;
  681. }
  682. }
  683. }
  684.  
  685. void add(Node node)
  686. {
  687. if (addNode.value < node.value)
  688. {
  689. if (node.left == nullNode)
  690. {
  691. node.left = addNode;
  692. balanced = node.color == Color.black;
  693. resNode = node;
  694. added = true;
  695. }
  696. else
  697. {
  698. add(node.left);
  699. if (added)
  700. {
  701. node.left = resNode;
  702. balanceL(node);
  703. }
  704. }
  705. }
  706. else if (node.value < addNode.value)
  707. {
  708. if (node.right == nullNode)
  709. {
  710. node.right = addNode;
  711. balanced = node.color == Color.black;
  712. resNode = node;
  713. added = true;
  714. }
  715. else
  716. {
  717. add(node.right);
  718. if (added)
  719. {
  720. node.right = resNode;
  721. balanceR(node);
  722. }
  723. }
  724. }
  725. else
  726. {
  727. added = false;
  728. balanced = true;
  729. }
  730. }
  731. }
  732.  
  733. class Printer
  734. {
  735. String indent;
  736.  
  737. Printer()
  738. {
  739. indent = "";
  740. }
  741.  
  742. void print()
  743. {
  744. if (root == nullNode)
  745. {
  746. System.out.println("empty");
  747. }
  748. else
  749. {
  750. indent = "";
  751. print(root);
  752. }
  753. }
  754.  
  755. void print(Node node)
  756. {
  757. String p = indent;
  758. indent = indent + " ";
  759. if (node.right != nullNode)
  760. {
  761. print(node.right);
  762. }
  763. String c;
  764. if (node.color == Color.black)
  765. {
  766. c = "(B)";
  767. }
  768. else
  769. {
  770. c = "(R)";
  771. }
  772. System.out.println(p + node.value + c);
  773. if (node.left != nullNode)
  774. {
  775. print(node.left);
  776. }
  777. indent = p;
  778. }
  779. }
  780. }
  781.  
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
      253(B)
    251(B)
        235(R)
      233(B)
  214(R)
      192(B)
    182(B)
      165(B)
156(B)
      140(B)
    132(R)
      122(B)
  77(B)
      68(R)
    67(B)