红黑树的旋转操作

作者:追风剑情 发布于:2017-8-19 23:03 分类:Algorithms

参考 http://blog.csdn.net/xudong_98/article/details/51471688

上一篇: 构建一颗红黑树

左旋转与右旋转互为逆操作

示例:C#


  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace RedBlackTree1
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. int[] keys = new int[] { 13, 8, 1, 11, 6, 17, 15, 25, 22, 27};
  14. Console.Write("测试数据为: ");
  15. for (int i = 0; i < keys.Length; i++)
  16. {
  17. Console.Write(keys[i] + " ");
  18. }
  19. Console.WriteLine();
  20.  
  21. //根节点
  22. Node root = null;
  23. //录入10个节点
  24. for (int i = 0; i < keys.Length; i++)
  25. {
  26. Node node = new Node();
  27. node.key = keys[i];
  28. if (null == root)
  29. root = node;
  30. else
  31. Insert(root, node);
  32. }
  33. Console.Write("构建的二叉搜索树为: ");
  34. Print(root);
  35.  
  36. Console.WriteLine();
  37. Console.WriteLine("给二叉搜索树染色: ");
  38. Coloring(root, Color.Black);//根节点为黑色
  39. Print(root, true);
  40.  
  41. Console.WriteLine();
  42.  
  43. Console.WriteLine("对key等于8的结点进行右旋转");
  44. Node node8 = GetNodeByKey(root, 8);
  45. RotationRight(node8);
  46. Print(root, true);
  47.  
  48. Console.WriteLine();
  49.  
  50. Console.WriteLine("对key等于1的结点进行左旋转");
  51. Node node1 = GetNodeByKey(root, 1);
  52. RotationLeft(node1);
  53. Print(root, true);
  54.  
  55. Console.ReadKey();
  56. }
  57.  
  58. static Node GetNodeByKey(Node node, int key)
  59. {
  60. if (node == null)
  61. return null;
  62. if (node.key == key)
  63. return node;
  64.  
  65. Node left = GetNodeByKey(node.left, key);
  66. if (left != null && left.key == key)
  67. return left;
  68.  
  69. Node right = GetNodeByKey(node.right, key);
  70. if (right != null && right.key == key)
  71. return right;
  72.  
  73. return null;
  74. }
  75.  
  76. static void Insert(Node root, Node child)
  77. {
  78. if(root.key >= child.key)
  79. {
  80. if (null == root.left)
  81. {
  82. root.left = child;
  83. child.parent = root;
  84. }
  85. else
  86. {
  87. Insert(root.left, child);
  88. }
  89. }
  90. else
  91. {
  92. if (null == root.right)
  93. {
  94. root.right = child;
  95. child.parent = root;
  96. }
  97. else
  98. {
  99. Insert(root.right, child);
  100. }
  101. }
  102. }
  103.  
  104. // 向左旋转
  105. static void RotationLeft(Node node)
  106. {
  107. //必须要有右节点
  108. if (node.right == null)
  109. return;
  110.  
  111. //轴节点
  112. Node pivot = node;
  113. //轴的左节点
  114. Node left = node.left;
  115. //轴的右节点
  116. Node right = node.right;
  117. //轴的右节点的左节点
  118. Node right_left = right.left;
  119.  
  120. //改变轴的父节点相应指针的指向
  121. Node pivot_parent = pivot.parent;
  122. if (null != pivot_parent)
  123. {
  124. if (pivot_parent.left == pivot)
  125. {
  126. pivot_parent.left = right;
  127.  
  128. }
  129. else if (pivot_parent.right == pivot)
  130. {
  131. pivot_parent.right = right;
  132. }
  133. }
  134. //右节点的父指针指向轴节点的父
  135. right.parent = pivot_parent;
  136. //右节点的左指针指向轴节点
  137. right.left = pivot;
  138. //轴节点的父指针指向右节点
  139. pivot.parent = right;
  140. //轴节点的右指针指向右节点的左节点
  141. pivot.right = right_left;
  142. if (null != right_left)
  143. right_left.parent = pivot;
  144. }
  145.  
  146. // 向右旋转
  147. static void RotationRight(Node node)
  148. {
  149. //必须要有左节点
  150. if (node.left == null)
  151. return;
  152.  
  153. //轴节点
  154. Node pivot = node;
  155. //轴的左节点
  156. Node left = node.left;
  157. //轴的右节点
  158. Node right = node.right;
  159. //轴的左节点的右节点
  160. Node left_right = left.right;
  161.  
  162. //改变轴的父节点相应指针的指向
  163. Node pivot_parent = pivot.parent;
  164. if (null != pivot_parent)
  165. {
  166. if (pivot_parent.left == pivot)
  167. {
  168. pivot_parent.left = left;
  169. }
  170. else if (pivot_parent.right == pivot)
  171. {
  172. pivot_parent.right = left;
  173. }
  174. }
  175. //左节点的父指针指向轴节点的父
  176. left.parent = pivot_parent;
  177. //左节点的右指针指向轴节点
  178. left.right = pivot;
  179. //轴节点的父指针指向左节点
  180. pivot.parent = left;
  181. //轴节点的左指针指向左节点的右节点
  182. pivot.left = left_right;
  183. if (null != left_right)
  184. left_right.parent = pivot;
  185. }
  186.  
  187. static void Coloring(Node root, Color color)
  188. {
  189. if (null == root)
  190. return;
  191. root.color = color;
  192. color = (color == Color.Black) ? Color.Red : Color.Black;
  193. Coloring(root.left, color);
  194. Coloring(root.right, color);
  195. }
  196.  
  197. static void Print(Node root, bool outputColor = false)
  198. {
  199. if (null == root)
  200. return;
  201.  
  202. if (root != null)
  203. {
  204. if (outputColor)
  205. Console.Write("{0}({1}) ", root.key, root.color);
  206. else
  207. Console.Write("{0} ", root.key);
  208. }
  209.  
  210. Print(root.left, outputColor);
  211. Print(root.right, outputColor);
  212. }
  213. }
  214.  
  215. public enum Color
  216. {
  217. Black,
  218. Red
  219. }
  220.  
  221. public class Node
  222. {
  223. public Color color;
  224. public int key;
  225. public Node parent;
  226. public Node left;
  227. public Node right;
  228. }
  229.  
  230.  
  231. }


111111.png

绕8号节点右旋后的图像为:

11111.jpg

标签: Algorithms

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号