1. //红黑树 : 10到1的顺序插入验证木得问题了!
    2. class Nil {
    3. constructor(parent) {
    4. this.val = null;
    5. this.parent = parent;
    6. this.color = 2;
    7. }
    8. }
    9. class Node {
    10. constructor(val) {
    11. this.val = val;
    12. this.color = 1; //红为1,黑为2,
    13. this.parent = null;
    14. this.left = new Nil(this);
    15. this.right = new Nil(this);
    16. }
    17. }
    18. class RedBlackTree {
    19. constructor(item) {
    20. this.root = item;
    21. }
    22. _search(val, root) {
    23. const {left, right} = root;
    24. const node = root.val > val ? left : right;
    25. if (node instanceof Nil) return root;
    26. return this._search(val, node);
    27. }
    28. search(val) {
    29. if (this.root) {
    30. return this._search(val, this.root);
    31. }
    32. return null;
    33. }
    34. update(node) {
    35. const {parent, val} = node; //默认传入的node的颜色都是1
    36. if (parent) {
    37. if (parent.color === 2) return;
    38. const key = parent.val > val ? "left" : "right";
    39. const sibling = parent[key === "left" ? "right" : "left"];
    40. const grandP = parent.parent;
    41. if (grandP) {
    42. const key1 = grandP.val > parent.val ? "left" : "right";
    43. const uncle = grandP[key === "left" ? "right" : "left"];
    44. if (uncle.color === 1) { //情况3
    45. parent.color = 2;
    46. uncle.color = 2;
    47. grandP.color = 1;
    48. this.update(grandP);
    49. } else {
    50. if (key === "left") { //情况4
    51. const grandPP = grandP.parent;
    52. grandP.color = 1;
    53. parent.color = 2;
    54. parent.right = grandP;
    55. grandP.left = sibling;
    56. grandP.parent = parent;
    57. // console.log(grandPP,'ppppp');
    58. if (grandPP) {
    59. const key2 = grandPP.val > grandP.val ?
    60. "left" : "right";
    61. grandPP[key2] = parent;
    62. parent.parent = grandPP;
    63. } else {
    64. parent.parent = null;
    65. this.root = parent
    66. // console.log(parent,'ppp',node);
    67. }
    68. }else{ //情况5
    69. grandP[key1] = node
    70. node.parent = grandP
    71. node.left = parent
    72. parent.parent = node
    73. this.update(parent)
    74. }
    75. }
    76. } else {
    77. parent.color = 2
    78. }
    79. } else {
    80. node.color = 2
    81. }
    82. }
    83. insert(val) {
    84. const parent = this.search(val);
    85. let node = new Node(val);
    86. if (parent) {
    87. const {color} = parent;
    88. const key = val > parent.val ? "right" : "left";
    89. if (!parent.parent) { //2演变
    90. node.parent = parent;
    91. parent[key] = node;
    92. } else {
    93. if (color === 2) { //2
    94. node.parent = parent;
    95. return parent[key] = node;
    96. } else {
    97. parent[key] = node;
    98. node.parent = parent; //连上node
    99. this.update(node);
    100. }
    101. }
    102. } else { //1
    103. node.color = 2;
    104. this.root = node;
    105. }
    106. }
    107. }
    108. const rbt=new RedBlackTree()
    109. for (let i=10;i>=0;i--){
    110. rbt.insert(i)
    111. console.log(i,rbt.root);
    112. }