平衡二叉查找树

目的

解决普通二叉查找树在频繁的插入、删除等动态更新的情况下,出现时间复杂度退化的问题。“平衡”就是让整棵树左右看起来比较“对称”,不要出现左高右矮的情况,让整棵树的高度相对来说低一些,相应的插入、删除、查找等操作的效率高一些。

定义

平衡二叉树中任意一个节点的左右子树的高度相差不能大于 1。
完全二叉树、满二叉树其实都是平衡二叉树,但是非完全二叉树也有可能是平衡二叉树。
image.png
平衡二叉树:Splay Tree(伸展树)、Treap(树堆)、AVL树

红黑树(R-B Tree)

红黑树并不是严格意义上的平衡二叉树,因为它从根节点到各个叶子节点的最长路径,有可能会比最短路径大一倍。但是只要树的高度不比 log2n 大很多,红黑树的高度近似 2log2n。,尽管它不符合严格的平衡二叉查找树的定义,但仍然是一个合格的平衡二叉查找树。
AVL是严格的平衡二叉查找树,查找效率非常高,但为了维持这种高度的平衡,就要付出更多的代价:每次插入、删除都要做调整,比较复杂、耗时。所以,对于有频繁的插入、删除操作的数据集合,使用 AVL 树的代价就有点高了。
红黑树只是做到了近似平衡,并不是严格的平衡,红黑树的插入、删除、查找各种操作性能都比较稳定,所以在维护平衡的成本上,要比 AVL 树要低。

定义

红黑树中的节点,一类被标记为黑色,一类被标记为红色。除此之外,还需要满足:

  • 根节点是黑色的;插入节点是红色
  • 任何上下相邻的节点不能同时为红色,也就是说,红色节点是被黑色节点隔开的;
  • 每个节点,从该节点到达其叶子节点的所有路径,都包含相同数目的黑色节点;
  • 叶子节点必须是黑色的空节点(NIL)

在插入、删除节点的过程中,第二、第三点要求可能会被破坏,所以左旋(rotate left)右旋(rotate right),实际上就是要把被破坏的第二、第三点恢复过来。
image.pngimage.png
第四条“叶子节点必须是黑色的空节点”,主要是当插入新节点的时候,其父节点也是红色这种情况也能满足下面CASE 2。同时具体实现中并不会浪费存储空间,可以共用一个黑色的、空的节点
image.png
image.png

插入

红黑树规定,插入的节点必须是红色的。而且,二叉查找树中新插入的节点都是放在叶子节点上
插入普通情况:

  • 如果插入节点的父节点是黑色的,那什么都不用做。
  • 如果插入的节点是根节点,那直接改变它的颜色,把它变成黑色就可以了。

三种特殊情况:

把父节点的兄弟节点叫作叔叔节点,父节点的父节点叫作祖父节点。

CASE 1:如果关注节点是 a,它的叔叔节点 d 是红色,就依次执行下面的操作:

  • 将关注节点 a 的父节点 b、叔叔节点 d 的颜色都设置成黑色;
  • 将关注节点 a 的祖父节点 c 的颜色设置成红色;
  • 关注节点变成 a 的祖父节点 c;
  • 跳到 CASE 2 或者 CASE 3。

image.png
CASE 2:如果关注节点是 a,它的叔叔节点 d 是黑色,关注节点 a 是其父节点 b 的右子节点,就依次执行下面的操作:

  • 关注节点变成节点 a 的父节点 b;
  • 围绕新的关注节点b 左旋;
  • 跳到 CASE 3。

image.png
CASE 3:如果关注节点是 a,它的叔叔节点 d 是黑色,关注节点 a 是其父节点 b 的左子节点,就依次执行下面的操作:

  • 围绕关注节点 a 的祖父节点 c 右旋;
  • 将关注节点 a 的父节点 b、兄弟节点 c 的颜色互换。
  • 调整结束。

image.png

删除

一、针对删除节点初步调整
初步调整保证整棵红黑树在一个节点删除之后,仍然满足最每个节点,从该节点到达其可达叶子节点的所有路径,都包含相同数目的黑色节点。
CASE 1:如果要删除的节点是 a,它只有一个子节点 b,那就依次进行下面的操作:

  • 删除节点 a,并且把节点 b 替换到节点 a 的位置,这一部分操作跟普通的二叉查找树的删除操作一样;
  • 节点 a 只能是黑色,节点 b 也只能是红色,其他情况均不符合红黑树的定义。这种情况下,把节点 b 改为黑色;
  • 调整结束,不需要进行二次调整。

image.png
CASE 2:如果要删除的节点 a 有两个非空子节点,并且它的后继节点就是节点 a 的右子节点 c。就依次进行下面的操作:

  • 如果节点 a 的后继节点就是右子节点 c,那右子节点 c 肯定没有左子树。把节点 a 删除,并且将节点 c 替换到节点 a 的位置。这一部分操作跟普通的二叉查找树的删除操作无异;
  • 然后把节点 c 的颜色设置为跟节点 a 相同的颜色;
  • 如果节点 c 是黑色,为了不违反红黑树的最后一条定义,我们给节点 c 的右子节点 d 多加一个黑色,这个时候节点 d 就成了“红 - 黑”或者“黑 - 黑”;
  • 这个时候,关注节点变成了节点 d,第二步的调整操作就会针对关注节点来做。

image.png
CASE 3:如果要删除的是节点 a,它有两个非空子节点,并且节点 a 的后继节点不是右子节点,就依次进行下面的操作:

  • 找到后继节点 d,并将它删除,删除后继节点 d 的过程参照 CASE 1;
  • 将节点 a 替换成后继节点 d;
  • 把节点 d 的颜色设置为跟节点 a 相同的颜色;
  • 如果节点 d 是黑色,为了不违反红黑树的最后一条定义,给节点 d 的右子节点 c 多加一个黑色,这个时候节点 c 就成了“红 - 黑”或者“黑 - 黑”;
  • 这个时候,关注节点变成了节点 c,第二步的调整操作就会针对关注节点来做。

image.png
二、针对关注节点进行二次调整
保证了不存在上下相邻的两个红色节点。
CASE 1:如果关注节点是 a,它的兄弟节点 c 是红色的,就依次进行下面的操作:

  • 围绕关注节点 a 的父节点 b 左旋;
  • 关注节点 a 的父节点 b 和祖父节点 c 交换颜色;
  • 关注节点不变;
  • 继续从四种情况中选择适合的规则来调整。

image.png
CASE 2:如果关注节点是 a,它的兄弟节点 c 是黑色的,并且节点 c 的左右子节点 d、e 都是黑色的,就依次进行下面的操作:

  • 将关注节点 a 的兄弟节点 c 的颜色变成红色;
  • 从关注节点 a 中去掉一个黑色,这个时候节点 a 就是单纯的红色或者黑色;
  • 给关注节点 a 的父节点 b 添加一个黑色,这个时候节点 b 就变成了“红 - 黑”或者“黑 - 黑”;
  • 关注节点从 a 变成其父节点 b;
  • 继续从四种情况中选择符合的规则来调整。

image.png
CASE 3:如果关注节点是 a,它的兄弟节点 c 是黑色,c 的左子节点 d 是红色,c 的右子节点 e 是黑色,就依次进行下面的操作:

  • 围绕关注节点 a 的兄弟节点 c 右旋;
  • 节点 c 和节点 d 交换颜色;
  • 关注节点不变;
  • 跳转到 CASE 4,继续调整。

image.png
CASE 4:如果关注节点 a 的兄弟节点 c 是黑色的,并且 c 的右子节点是红色的,就依次进行下面的操作:

  • 围绕关注节点 a 的父节点 b 左旋;
  • 将关注节点 a 的兄弟节点 c 的颜色,跟关注节点 a 的父节点 b 设置成相同的颜色;
  • 将关注节点 a 的父节点 b 的颜色设置为黑色;
  • 从关注节点 a 中去掉一个黑色,节点 a 就变成了单纯的红色或者黑色;
  • 将关注节点 a 的叔叔节点 e 设置为黑色;
  • 调整结束。

image.png

代码实现

  1. /**
  2. *
  3. * 1. 规则
  4. * 1. 根结点必须为黑色
  5. * 2. 不能有R-R的关系
  6. * 3. 每条到叶子节点的路径必须包含相同的黑色节点树
  7. */
  8. const RedBlackTreeNode = require('./RedBlackTreeNode');
  9. const BLACK = 1;
  10. const RED = 0;
  11. class RedBlackTree {
  12. constructor() {
  13. this.root = null;
  14. }
  15. /**
  16. *
  17. * @param {number} val
  18. * @param {RedBlackTreeNode} node
  19. * @return {RedBlackTreeNode}
  20. */
  21. _insert(val) {
  22. let t = this.root;
  23. let p = null;
  24. while (t) {
  25. p = t;
  26. if (t.val < val) {
  27. t = t.right;
  28. } else if (t.val > val) {
  29. t = t.left;
  30. } else return;
  31. }
  32. let node = new RedBlackTreeNode(val);
  33. node.parent = p;
  34. if (p === null) {
  35. this.root = node;
  36. } else if (p.val < node.val) {
  37. p.right = node;
  38. } else {
  39. p.left = node;
  40. }
  41. node.color = RED;
  42. this.insertFix(node);
  43. }
  44. /**
  45. *
  46. * @param {RedBlackTreeNode} node
  47. * @param {number} val
  48. * @return {boolean}
  49. */
  50. _remove(node, val) {
  51. while (node.val !== val && node) {
  52. if (node.val > val) {
  53. node = node.left
  54. } else {
  55. node = node.right
  56. }
  57. }
  58. if (node) {
  59. if (!node.left || !node.right) {
  60. this._removeAndFix(node);
  61. } else {
  62. let minNode = this.findMin(node.right);
  63. node.val = minNode.val;
  64. this._remove(node.right, minNode.val);
  65. }
  66. } else return false;
  67. return true
  68. }
  69. /**
  70. * @param {RedBlackTreeNode} node
  71. */
  72. _removeAndFix(node) {
  73. if (node.color === RED) {
  74. this._removeNode(node);
  75. } else {
  76. this._removeFix(node);
  77. }
  78. }
  79. /**
  80. *
  81. * @param {RedBlackTreeNode} node
  82. */
  83. _removeNode(node) {
  84. if (node === this.root) {
  85. if (node.left) {
  86. this.root = node.left;
  87. node.parent = null;
  88. } else if (node.right) {
  89. this.root = node.right;
  90. node.parent = null;
  91. } else {
  92. this.root = null;
  93. }
  94. } else {
  95. if (node.parent.left === node) {
  96. if (node.left) {
  97. node.parent.left = node.left;
  98. node.left.parent = node.parent;
  99. } else if (node.right) {
  100. node.parent.left = node.right;
  101. node.right.parent = node.parent
  102. } else {
  103. node.parent.left = null
  104. }
  105. } else {
  106. if (node.left) {
  107. node.parent.right = node.left;
  108. node.left.parent = node.parent;
  109. } else if (node.right) {
  110. node.parent.right = node.right;
  111. node.right.parent = node.parent
  112. } else {
  113. node.parent.right = null
  114. }
  115. }
  116. }
  117. }
  118. /**
  119. *
  120. * @param {RedBlackTreeNode} node
  121. */
  122. _removeFix(node) {
  123. let nodeRef = node;
  124. while (node !== this.root && node.color === BLACK) {
  125. if (node.parent.left === node) {
  126. let brotherNode = node.parent.right;
  127. if (brotherNode.color === RED) {
  128. brotherNode.color = BLACK;
  129. node.color = BLACK;
  130. node.parent.color = RED;
  131. this._rotateLeft(node.parent);
  132. } else if (
  133. (!brotherNode.left || brotherNode.left.color === BLACK)
  134. &&
  135. (!brotherNode.right || brotherNode.right.color === BLACK)
  136. ) {
  137. if (node.parent.color === RED) {
  138. brotherNode.color = RED;
  139. node.parent.color = BLACK;
  140. node = this.root;
  141. } else {
  142. brotherNode.color = RED;
  143. node = node.parent;
  144. }
  145. } else if (brotherNode.left.color === RED && brotherNode.right.color === BLACK) {
  146. this._rotateRight(brotherNode);
  147. brotherNode.color = RED;
  148. brotherNode.parent.color = BLACK;
  149. } else if (brotherNode.right.color === RED) {
  150. brotherNode.color = node.parent.color;
  151. node.parent.color = BLACK;
  152. brotherNode.right.color = BLACK;
  153. this._rotateLeft(node.parent);
  154. node = this.root;
  155. }
  156. } else {
  157. let brotherNode = node.parent.left;
  158. if (brotherNode.color === RED) {
  159. brotherNode.color = BLACK;
  160. node.color = BLACK;
  161. node.parent.color = RED;
  162. this._rotateRight(node.parent);
  163. } else if (
  164. (!brotherNode.left || brotherNode.left.color === BLACK)
  165. &&
  166. (!brotherNode.right || brotherNode.right.color === BLACK)
  167. ) {
  168. if (node.parent.color === RED) {
  169. brotherNode.color = RED;
  170. node.parent.color = BLACK;
  171. node = this.root;
  172. } else {
  173. brotherNode.color = RED;
  174. node = node.parent;
  175. }
  176. } else if (brotherNode.right.color === RED && brotherNode.left.color === BLACK) {
  177. this._rotateLeft(brotherNode);
  178. brotherNode.color = RED;
  179. brotherNode.parent.color = BLACK;
  180. } else if (brotherNode.left.color === RED) {
  181. brotherNode.color = node.parent.color;
  182. node.parent.color = BLACK;
  183. brotherNode.left.color = BLACK;
  184. this._rotateRight(node.parent);
  185. node = this.root;
  186. }
  187. }
  188. }
  189. node.color = BLACK;
  190. this._removeNode(nodeRef);
  191. }
  192. /**
  193. *
  194. * @param {RedBlackTreeNode} node
  195. */
  196. insertFix(node) {
  197. while (node.parent && node.parent.color !== BLACK) {
  198. if (node.parent === node.parent.parent.left) {
  199. let uncleNode = node.parent.parent.right;
  200. if (uncleNode && uncleNode.color === RED) {
  201. node.parent.color = BLACK;
  202. uncleNode.color = BLACK;
  203. node.parent.parent.color = RED;
  204. node = node.parent.parent;
  205. } else if (node === node.parent.right) {
  206. node = node.parent;
  207. this._rotateLeft(node);
  208. } else {
  209. node.parent.color = BLACK;
  210. node.parent.parent.color = RED;
  211. this._rotateRight(node.parent.parent);
  212. }
  213. } else {
  214. let uncleNode = node.parent.parent.left;
  215. if (uncleNode && uncleNode.color === RED) {
  216. node.parent.color = BLACK;
  217. uncleNode.color = BLACK;
  218. node.parent.parent.color = RED;
  219. node = node.parent.parent;
  220. } else if (node === node.parent.left) {
  221. node = node.parent;
  222. this._rotateRight(node);
  223. } else {
  224. node.parent.color = BLACK;
  225. node.parent.parent.color = RED;
  226. this._rotateLeft(node.parent.parent);
  227. }
  228. }
  229. }
  230. this.root.color = BLACK;
  231. }
  232. /**
  233. *
  234. * @param {RedBlackTreeNode} node
  235. * @return {RedBlackTreeNode}
  236. */
  237. _rotateLeft(node) {
  238. let rightNode = node.right;
  239. let rightNodeLeft = rightNode.left;
  240. node.right = rightNodeLeft;
  241. if (rightNodeLeft) rightNodeLeft.parent = node;
  242. rightNode.left = node;
  243. if (node.parent) {
  244. if (node.parent.left === node) {
  245. node.parent.left = rightNode;
  246. } else {
  247. node.parent.right = rightNode;
  248. }
  249. rightNode.parent = node.parent;
  250. } else {
  251. this.root = rightNode;
  252. rightNode.parent = null;
  253. }
  254. node.parent = rightNode;
  255. }
  256. /**
  257. *
  258. * @param {RedBlackTreeNode} node
  259. * @return {RedBlackTreeNode}
  260. */
  261. _rotateRight(node) {
  262. let leftNode = node.left;
  263. let leftNodeRight = leftNode.right;
  264. node.left = leftNodeRight;
  265. if (leftNodeRight) leftNodeRight.parent = node;
  266. leftNode.right = node;
  267. if (node.parent) {
  268. if (node.parent.left === node) {
  269. node.parent.left = leftNode;
  270. } else {
  271. node.parent.right = leftNode;
  272. }
  273. leftNode.parent = node.parent;
  274. } else {
  275. this.root = leftNode;
  276. leftNode.parent = null;
  277. }
  278. node.parent = leftNode;
  279. }
  280. /**
  281. * @param {RedBlackTreeNode} node
  282. * @return {RedBlackTreeNode}
  283. */
  284. findMin(node) {
  285. if (node.left) {
  286. return this.findMin(node.left)
  287. } else return node
  288. }
  289. /**
  290. *
  291. * @param {number} val
  292. */
  293. insert(val) {
  294. this._insert(val);
  295. }
  296. /**
  297. *
  298. * @param {number} val
  299. * @return {boolean}
  300. */
  301. remove(val) {
  302. this._remove(this.root, val);
  303. }
  304. }
  305. const t = new RedBlackTree();
  306. t.insert(1);
  307. t.insert(13);
  308. t.insert(5);
  309. t.insert(11);
  310. t.insert(2);
  311. t.insert(14);
  312. t.insert(15);
  313. t.insert(7);
  314. t.insert(8);
  315. t.remove(13);
  316. const t2 = new RedBlackTree();
  317. t2.insert(41);
  318. t2.insert(38);
  319. t2.insert(31);
  320. t2.insert(12);
  321. t2.insert(19);
  322. t2.insert(8);
  323. t2.remove(8);
  324. t2.remove(19);
  325. t2.remove(12);
  326. t2.remove(31);
  327. t2.remove(38);
  328. t2.remove(41);
  329. module.exports = RedBlackTree;