红黑树

来源:互联网 发布:怎样在淘宝上买罂粟壳 编辑:程序博客网 时间:2024/05/22 06:34

前面的博文分析了一般查找树的性质,理论上来讲,可以构造m阶完全树,但这种结构,会导致调整过于频繁,所以保持相对平衡就成了一种策略。红黑树,B树都是这种考虑。分析这些树(红黑树,AVL,B树)等,一个重要的地方就是我们所做的大部分工作的目标就是需要保持树在一定程度上的平衡。

下面是红黑树常见操作的实现:

 

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace AlbertAlgorithms  
  7. {  
  8.     /// <summary>  
  9.     /// 节点颜色枚举  
  10.     /// </summary>  
  11.     public enum RBTreeNodeColor  
  12.     {  
  13.         Red,  
  14.         Black  
  15.     }  
  16.     /// <summary>  
  17.     /// 红黑树节点  
  18.     /// </summary>  
  19.     public class RBTreeNode  
  20.     {  
  21.         public int Key { getset; }  
  22.         public RBTreeNode Left { getset; }  
  23.         public RBTreeNode Right { getset; }  
  24.         public RBTreeNode Parent { getset; }  
  25.         public RBTreeNodeColor NodeColor { getset; }  
  26.     }  
  27.     /// <summary>  
  28.     /// 红黑树  
  29.     /// </summary>  
  30.     public class RBTree  
  31.     {  
  32.         /// <summary>  
  33.         /// 空节点,充当哨兵  
  34.         /// </summary>  
  35.         public static readonly RBTreeNode NullNode=new RBTreeNode();  
  36.         /// <summary>  
  37.         /// 静态构造函数,哨兵为黑色.  
  38.         /// </summary>  
  39.         static RBTree()  
  40.         {  
  41.             NullNode.NodeColor = RBTreeNodeColor.Black;  
  42.         }  
  43.         /// <summary>  
  44.         /// 根节点,默认为NullNode.  
  45.         /// </summary>  
  46.         public RBTreeNode Root = NullNode;  
  47.         /// <summary>  
  48.         /// 插入节点,这里假设了所有的节点key不相同.  
  49.         /// </summary>  
  50.         /// <param name="Node"></param>  
  51.         public void InsertNode(RBTreeNode Node)  
  52.         {  
  53.             ///寻找插入节点的插入点(父节点)theY.TheX是循环中间变量.  
  54.             RBTreeNode theY = NullNode;  
  55.             RBTreeNode theX = Root;  
  56.             //注意,如果不为空,则找到的插入节点一定是最底层内节点,仅有空字节点(哨兵节点).  
  57.             //这也是红黑树不是高度平衡树的一个重要原因.但这样简化了插入,不用判断补缺.  
  58.             while (theX != NullNode)  
  59.             {  
  60.                 theY = theX;  
  61.                 if (Node.Key > theX.Key)  
  62.                 {  
  63.                     theX = theX.Right;//此处错误  
  64.                 }  
  65.                 else  
  66.                 {  
  67.                     theX = theX.Left;  
  68.                 }  
  69.             }  
  70.             //将插入节点的父节点指向theY.  
  71.             Node.Parent = theY;  
  72.             //如果theY为空,则原来是空树,则新插入的节点是根节点.  
  73.             if (theY == NullNode)  
  74.             {  
  75.                 Root = Node;  
  76.             }  
  77.             else  
  78.             {  
  79.                 //如果有根节点,与theY的Key比较,大于则作为右子节点,小于则  
  80.                 //作为左节点.  
  81.                 if (Node.Key > theY.Key)  
  82.                 {  
  83.                     Node.Parent = theY.Right; //  
  84.                 }  
  85.                 else  
  86.                 {  
  87.                     Node.Parent = theY.Left;  
  88.                 }  
  89.             }  
  90.             //开始插入的节点都在非空叶子节点上,将空节点作为其两个子节点.  
  91.             Node.Right = NullNode;  
  92.             Node.Left = NullNode;  
  93.             //颜色置为红,然后整理.  
  94.             Node.NodeColor = RBTreeNodeColor.Red;  
  95.             RBInsertFixup(Node);  
  96.         }  
  97.         /// <summary>  
  98.         /// 红黑树的插入整理,以保持红黑树性质:非红即白;根和叶子黑;相邻节点不能同为红;  
  99.         /// 所有从根到叶子路径上的黑节点(黑度)相同.这里的相邻只会是父子节点间,书上的定义是  
  100.         /// 红节点的两个孩子节点都是黑的,表达是等价的.从根到叶子节点看做是树的路径,则路径  
  101.         /// 上红节点不能相邻.  
  102.         /// </summary>  
  103.         /// <param name="Node"></param>  
  104.         private void RBInsertFixup(RBTreeNode Node)  
  105.         {  
  106.             //因为当前节点为红色,从下向根方向整理,如果当前节点的父节点为红色,则违反了红黑树的性质,  
  107.             //就需要不断整理,直到当前整理节点的父节点是黑色为止.整理一定会有结束条件,  
  108.             //因为根节点肯定为黑。  
  109.             while (Node.Parent.NodeColor == RBTreeNodeColor.Red)  
  110.             {  
  111.                 //如果父节点为红色,则爷节点必为黑色(红黑树性质)。  
  112.                 //如果当前节点的父节点是左孩子  
  113.                 if (Node.Parent == Node.Parent.Parent.Left)  
  114.                 {  
  115.                     //找到其叔父节点.  
  116.                     RBTreeNode theY = Node.Parent.Parent.Right;  
  117.                     //如果叔父节点是红色节点,而爷节点为黑色节点。形式就是爷节点为黑,父辈节点为红,由于当前节点为红,  
  118.                     //父节点必须改为黑色。而且这种改变不能影响黑度(所有从根到叶子节点的路径中黑节点数相等),那么改变的策略  
  119.                     //就是将爷节点变为红色,父辈节点变为黑色,这种改变显然没有改变路径上的黑度。但因为爷节点变为了红色,可能  
  120.                     //会导致红黑树性质的改变,就需要继续整理,因此需将整理节点上移到爷节点,继续整理。  
  121.                     if (theY.NodeColor == RBTreeNodeColor.Red)  
  122.                     {  
  123.                         Node.Parent.NodeColor = RBTreeNodeColor.Black;  
  124.                         theY.NodeColor = RBTreeNodeColor.Black;  
  125.                         Node.Parent.Parent.NodeColor = RBTreeNodeColor.Red;  
  126.                         Node = Node.Parent.Parent;  
  127.                     }  
  128.                     //如果叔父节点是黑色,爷节点也为黑色,父节点因为要改成黑节点,就增加了当前节点路径上的黑度。因此处理就稍微复杂些。  
  129.                     else  
  130.                     {  
  131.                         //如果当前节点是右孩子,先左旋。因为轴是当前节点和父节点,都为红色,因此左旋不会改变黑度,这很关键。  
  132.                         if (Node == Node.Parent.Right)  
  133.                         {  
  134.                             Node = Node.Parent;  
  135.                             LeftRotate(Node);  
  136.                         }  
  137.                         //将父节点改为黑节点,爷节点改为红色节点,当前节点路径上的黑度没变,但叔父路径上的黑度被减了1,为了保持黑度不变,因此需要右旋  
  138.                         //其实就是将父节点上升到爷节点,原来的爷节点降为叔父节点,这样黑度就平衡了。  
  139.                         Node.Parent.NodeColor = RBTreeNodeColor.Black;  
  140.                         Node.Parent.Parent.NodeColor = RBTreeNodeColor.Red;  
  141.                         RightRotate(Node.Parent.Parent);  
  142.                     }  
  143.                 }//如果当前节点的父节点是右孩子.因为是对称情况,所以逻辑处理与前面类似.  
  144.                 else  
  145.                 {  
  146.                     //找到其叔父节点.  
  147.                     RBTreeNode theY = Node.Parent.Parent.Left;  
  148.                     if (theY.NodeColor == RBTreeNodeColor.Red)  
  149.                     {  
  150.                         Node.Parent.NodeColor = RBTreeNodeColor.Black;  
  151.                         theY.NodeColor = RBTreeNodeColor.Black;  
  152.                         Node.Parent.Parent.NodeColor = RBTreeNodeColor.Red;  
  153.                         Node = Node.Parent.Parent;  
  154.                     }  
  155.                     else  
  156.                     {  
  157.                         if (Node == Node.Parent.Left)  
  158.                         {  
  159.                             Node = Node.Parent;  
  160.                             RightRotate(Node);  
  161.                         }  
  162.                         Node.Parent.NodeColor = RBTreeNodeColor.Black;  
  163.                         Node.Parent.Parent.NodeColor = RBTreeNodeColor.Red;  
  164.                         LeftRotate(Node.Parent.Parent);  
  165.                     }  
  166.                 }  
  167.             }  
  168.             //保证根节点为黑色.保证下次while循环可正常结束.  
  169.             Root.NodeColor = RBTreeNodeColor.Black;  
  170.         }  
  171.         /// <summary>  
  172.         /// 对一般情况,可以分4种情况考虑:  
  173.         /// 1)无孩子,则可直接删除;  
  174.         /// 2)只有左孩子:那么只要用左孩子替换当前节点即可;  
  175.         /// 3)只有右孩子:那么只要用左孩子替换当前节点即可;  
  176.         /// 4)如果有两个孩子:则必须找到当前节点的后继节点y.y要么没孩子,要么只有右孩子。可把把当前节点与y交互信息(key和卫星数据).  
  177.         /// 然后删除y(情况1..3).如果Y的颜色为黑色,因为y的删除破坏了红黑树性质,则需要重新整理,如果y是红色的,则删除不会改变  
  178.         /// 红黑树的性质.  
  179.         /// </summary>  
  180.         /// <param name="Node"></param>  
  181.         public void RBTreeDelete(RBTreeNode Node)  
  182.         {  
  183.             //默认theY指向当前节点.  
  184.             RBTreeNode theY = Node;  
  185.             //如果节点的左右子孩子都不为空,则需找到当前节点的后继节点,theY指向当前节点的后继节点.  
  186.             //注意这种情况下,当前节点的后继节点一定是右孩子的最小key节点.而且theY不可能有左孩子。  
  187.             if(Node.Right != NullNode && Node.Left != NullNode)  
  188.             {  
  189.                 theY = GetSuccessor(Node);  
  190.             }  
  191.             RBTreeNode theX = NullNode;  
  192.             //theY.Left如果不为空,则theY就是指向Node节点,且没有右孩子.  
  193.             //如果theY没有左孩子,则theX指向theY的右孩子,注意theX也可能为空.  
  194.             if (theY.Left != NullNode)  
  195.             {  
  196.                 theX = theY.Left;  
  197.             }  
  198.             else  
  199.             {  
  200.                 theX = theY.Right;  
  201.             }  
  202.             //开始删除theY.  
  203.             theX.Parent = theY.Parent;  
  204.             //如果theY.Parent为空,则删除的是根节点,那么theX就变成了根节点。  
  205.             if (theY.Parent == NullNode)  
  206.             {  
  207.                 Root = theX;  
  208.             }  
  209.             else  
  210.             {  
  211.                 if (theY == theY.Parent.Left)  
  212.                 {  
  213.                     theY.Parent.Left = theX;  
  214.                 }  
  215.                 else  
  216.                 {  
  217.                     theY.Parent.Right = theX;  
  218.                 }  
  219.             }  
  220.             //如果是theY不是指向当前节点,则表明是当前节点有两个孩子的情况,需交互数据.  
  221.             if (theY != Node)  
  222.             {  
  223.                 Node.Key = theY.Key;  
  224.                 //注意这里还序号拷贝卫星数据.  
  225.             }  
  226.             //如果删除的是黑节点,则必须重新整理(黑度发生变化,破坏了红黑树性质).  
  227.             if (theY.NodeColor == RBTreeNodeColor.Black)  
  228.             {  
  229.                 RBTreeDelFixup(theX);  
  230.             }  
  231.         }  
  232.         /// <summary>  
  233.         /// 红黑书删除整理.因为删除的节点是黑色,则由此往上需要平衡黑度.  
  234.         /// </summary>  
  235.         /// <param name="Node"></param>  
  236.         public void RBTreeDelFixup(RBTreeNode Node)  
  237.         {  
  238.             while (Node != Root && Node.NodeColor == RBTreeNodeColor.Black)  
  239.             {  
  240.                 //如果是左孩子  
  241.                 if (Node == Node.Parent.Left)  
  242.                 {  
  243.                     RBTreeNode theW = Node.Parent.Right;  
  244.                     //兄弟节点是红色,父节点必然是黑色,这时可以通过将兄弟节点置为黑色,父节点置为红色,  
  245.                     //然后左旋,右孩子保持黑度不变,左孩子黑度还是少1,便于继续处理。.  
  246.                     if (theW.NodeColor == RBTreeNodeColor.Red)  
  247.                     {  
  248.                         theW.NodeColor = RBTreeNodeColor.Black;  
  249.                         Node.Parent.NodeColor = RBTreeNodeColor.Red;  
  250.                         LeftRotate(Node.Parent);  
  251.                         theW = Node.Parent.Right;  
  252.                     }  
  253.                     //如果兄弟节点的左右孩子都是黑色,则可将兄弟节点置为红色,将需要平衡的节点上移后,还保持要平衡节点  
  254.                     //的黑度少1  
  255.                     if (theW.Left.NodeColor == RBTreeNodeColor.Black && theW.Right.NodeColor == RBTreeNodeColor.Black)  
  256.                     {  
  257.                         theW.NodeColor = RBTreeNodeColor.Red;  
  258.                         Node = Node.Parent;  
  259.                     }  
  260.                     else  
  261.                     {  
  262.                         //如果兄弟节点的孩子有一个是红色的,则可以通过调整就可以达到这种平衡.  
  263.                         if (theW.Right.NodeColor == RBTreeNodeColor.Black)  
  264.                         {  
  265.                             theW.Left.NodeColor = RBTreeNodeColor.Black;  
  266.                             theW.NodeColor = RBTreeNodeColor.Red;  
  267.                             RightRotate(theW);  
  268.                             theW = Node.Parent.Right;  
  269.                         }  
  270.                         theW.NodeColor = Node.Parent.NodeColor;  
  271.                         Node.Parent.NodeColor = RBTreeNodeColor.Black;  
  272.                         theW.Right.NodeColor = RBTreeNodeColor.Black;  
  273.                         LeftRotate(Node.Parent);  
  274.                         Node = Root;  
  275.                     }  
  276.                 }  
  277.                 else  
  278.                 {  
  279.                     RBTreeNode theW = Node.Parent.Left;  
  280.                     //兄弟节点是红色,父节点必然是黑色,这时可以通过将兄弟节点置为黑色,父节点置为红色,  
  281.                     //然后右旋,左孩子保持黑度不变,右孩子黑度还是少1,便于继续处理。.  
  282.                     if (theW.NodeColor == RBTreeNodeColor.Red)  
  283.                     {  
  284.                         theW.NodeColor = RBTreeNodeColor.Black;  
  285.                         Node.Parent.NodeColor = RBTreeNodeColor.Red;  
  286.                         RightRotate(Node.Parent);  
  287.                         theW = Node.Parent.Left;  
  288.                     }  
  289.                     //如果兄弟节点的左右孩子都是黑色,则可将兄弟节点置为红色,将需要平衡的节点上移后,还保持要平衡节点  
  290.                     //的黑度少1  
  291.                     if (theW.Left.NodeColor == RBTreeNodeColor.Black && theW.Right.NodeColor == RBTreeNodeColor.Black)  
  292.                     {  
  293.                         theW.NodeColor = RBTreeNodeColor.Red;  
  294.                         Node = Node.Parent;  
  295.                     }  
  296.                     else  
  297.                     {  
  298.                         //如果兄弟节点的孩子有一个是红色的,则可以通过调整就可以达到这种平衡.  
  299.                         if (theW.Left.NodeColor == RBTreeNodeColor.Black)  
  300.                         {  
  301.                             theW.Left.NodeColor = RBTreeNodeColor.Black;  
  302.                             theW.NodeColor = RBTreeNodeColor.Red;  
  303.                             LeftRotate(theW);  
  304.                             theW = Node.Parent.Left;  
  305.                         }  
  306.                         theW.NodeColor = Node.Parent.NodeColor;  
  307.                         Node.Parent.NodeColor = RBTreeNodeColor.Black;  
  308.                         theW.Left.NodeColor = RBTreeNodeColor.Black;  
  309.                         RightRotate(Node.Parent);  
  310.                         Node = Root;  
  311.                     }  
  312.   
  313.                 }  
  314.                   
  315.             }  
  316.             Node.NodeColor = RBTreeNodeColor.Black;  
  317.         }  
  318.         /*所谓二叉树的旋转,其实可以这样理解,当前节点c,和其孩子l,r.看做是滑轮上的三个点,C在滑轮上的支点,l,r分别吊在 
  319.          滑轮的两边。左旋的结果就是右边得孩子到了滑轮支点,C向左下滑,右旋则相反,这种旋转,目的就是保持某种平衡。比如右边孩子 
  320.          * 如果重了,就会引起不平衡,就需要左旋,当然,因为要保持查找树的性质,左右孩子(l,r)的子节点需要做一定调整.*/  
  321.         /// <summary>  
  322.         /// 左旋,以当前节点和其右孩子为轴。  
  323.         /// </summary>  
  324.         /// <param name="Node"></param>  
  325.         private void LeftRotate(RBTreeNode Node)  
  326.         {  
  327.             RBTreeNode theY = Node.Right;  
  328.             Node.Right = theY.Left;  
  329.             theY.Left.Parent = Node;  
  330.             if (Node.Parent == NullNode)  
  331.             {  
  332.                 Root = theY;  
  333.             }  
  334.             else if (Node == Node.Parent.Left)  
  335.             {  
  336.                 Node.Parent.Left = theY;  
  337.             }  
  338.             else  
  339.             {  
  340.                 Node.Parent.Right = theY;  
  341.             }  
  342.             theY.Left = Node;  
  343.             Node.Parent = theY;  
  344.               
  345.         }  
  346.         /// <summary>  
  347.         /// 右旋,以当前节点和其左孩子为轴.  
  348.         /// </summary>  
  349.         /// <param name="Node"></param>  
  350.         private void RightRotate(RBTreeNode Node)  
  351.         {  
  352.             RBTreeNode theY = Node.Left;  
  353.             Node.Left = theY.Right;  
  354.             theY.Right.Parent = Node;  
  355.             if (Node.Parent == NullNode)  
  356.             {  
  357.                 Root = theY;  
  358.             }  
  359.             else if (Node == Node.Parent.Left)  
  360.             {  
  361.                 Node.Parent.Left = theY;  
  362.             }  
  363.             else  
  364.             {  
  365.                 Node.Parent.Right = theY;  
  366.             }  
  367.             theY.Right = Node;  
  368.             Node.Parent = theY;  
  369.         }  
  370.         /// <summary>  
  371.         /// 查找树的节点.  
  372.         /// </summary>  
  373.         /// <param name="Key">关键值.</param>  
  374.         /// <returns></returns>  
  375.         public RBTreeNode TreeSearch(int Key)  
  376.         {  
  377.             RBTreeNode theX = Root;  
  378.             while (theX != NullNode && theX.Key != Key)  
  379.             {  
  380.                 if (theX.Key < Key)  
  381.                 {  
  382.                     theX = theX.Right;  
  383.                 }  
  384.                 else  
  385.                 {  
  386.                     theX = theX.Left;  
  387.                 }  
  388.             }  
  389.             return theX;  
  390.         }  
  391.         /// <summary>  
  392.         /// 获取以Node为根节点的子树的最小关键值节点  
  393.         /// </summary>  
  394.         /// <returns></returns>  
  395.         public RBTreeNode GetMinKeyNode(RBTreeNode Node)  
  396.         {  
  397.             RBTreeNode theX = Node;  
  398.             while (theX != NullNode)  
  399.             {  
  400.                 theX = theX.Left;  
  401.             }  
  402.             return theX;  
  403.         }  
  404.         /// <summary>  
  405.         /// 获取以指定节点为根的子树的最大关键值节点  
  406.         /// </summary>  
  407.         /// <returns></returns>  
  408.         public RBTreeNode GetMaxKeyNode(RBTreeNode Node)  
  409.         {  
  410.             RBTreeNode theX = Node;  
  411.             while (theX != NullNode)  
  412.             {  
  413.                 theX = theX.Right;  
  414.             }  
  415.             return theX;  
  416.         }  
  417.         /// <summary>  
  418.         /// 寻找指定节点的后继节点.  
  419.         /// </summary>  
  420.         /// <param name="node"></param>  
  421.         /// <returns></returns>  
  422.         public RBTreeNode GetSuccessor(RBTreeNode Node)  
  423.         {  
  424.             //如果当前节点右孩子不为空,则后续节点显然是右孩子子树的最小节点。  
  425.             if (Node.Right != NullNode)  
  426.             {  
  427.                 return GetMinKeyNode(Node.Right);  
  428.             }  
  429.             RBTreeNode theF = Node.Parent;  
  430.             //如果当前节点的右孩子为空,则从其父节点开始判断,如果当前节点是其父节点的左孩子,则后续节点就是父节点.  
  431.             //如果是右孩子,则继续向上寻找,直到为空或者当前节点是其父节点的左孩子为止.  
  432.             while (theF != NullNode && Node != theF.Right)  
  433.             {  
  434.                 Node = theF;  
  435.                 theF = theF.Parent;  
  436.             }  
  437.             return theF;  
  438.         }  
  439.         /// <summary>  
  440.         /// 寻找指定节点的继节点.  
  441.         /// </summary>  
  442.         /// <param name="node"></param>  
  443.         /// <returns></returns>  
  444.         public RBTreeNode GetPredecessor(RBTreeNode Node)  
  445.         {  
  446.             //如果当前节点左孩子不为空,则后续节点显然是左孩子子树的最大节点。  
  447.             if (Node.Left != NullNode)  
  448.             {  
  449.                 return GetMaxKeyNode(Node.Left);  
  450.             }  
  451.             RBTreeNode theF = Node.Parent;  
  452.             //如果当前节点的左孩子为空,则从其父节点开始判断,如果当前节点是其父节点的右孩子,则后续节点就是父节点.  
  453.             //如果是左孩子,则继续向上寻找,直到为空或者当前节点是其父节点的右孩子为止.  
  454.             while (theF != NullNode && Node != theF.Left)  
  455.             {  
  456.                 Node = theF;  
  457.                 theF = theF.Parent;  
  458.             }  
  459.             return theF;  
  460.         }  
  461.     }  
  462.       
  463. }  


参考算法导论.


更正:

[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /// <summary>  
  2.        /// 左旋,以当前节点和其右孩子为轴。  
  3.        /// </summary>  
  4.        /// <param name="Node"></param>  
  5.        private void LeftRotate(RBTreeNode Node)  
  6.        {  
  7.            RBTreeNode theY = Node.Right;  
  8.            Node.Right = theY.Left;  
  9.            theY.Left.Parent = Node;  
  10.              
  11.            theY.Parent = Node.Parent;  
  12.   
  13.            if (Node.Parent == NullNode)  
  14.            {  
  15.                Root = theY;  
  16.            }  
  17.            else if (Node == Node.Parent.Left)  
  18.            {  
  19.                Node.Parent.Left = theY;  
  20.            }  
  21.            else  
  22.            {  
  23.                Node.Parent.Right = theY;  
  24.            }  
  25.            theY.Left = Node;  
  26.            Node.Parent = theY;  
  27.   
  28.        }  
  29.        /// <summary>  
  30.        /// 右旋,以当前节点和其左孩子为轴.  
  31.        /// </summary>  
  32.        /// <param name="Node"></param>  
  33.        private void RightRotate(RBTreeNode Node)  
  34.        {  
  35.            RBTreeNode theY = Node.Left;  
  36.            Node.Left = theY.Right;  
  37.            theY.Right.Parent = Node;  
  38.              
  39.            theY.Parent = Node.Parent;  
  40.            if (Node.Parent == NullNode)  
  41.            {  
  42.                Root = theY;  
  43.            }  
  44.            else if (Node == Node.Parent.Left)  
  45.            {  
  46.                Node.Parent.Left = theY;  
  47.            }  
  48.            else  
  49.            {  
  50.                Node.Parent.Right = theY;  
  51.            }  
  52.            theY.Right = Node;  
  53.            Node.Parent = theY;  
  54.        }  
[csharp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. /// <summary>  
  2.        /// 插入节点,这里假设了所有的节点key不相同.  
  3.        /// </summary>  
  4.        /// <param name="Node"></param>  
  5.        public void InsertNode(RBTreeNode Node)  
  6.        {  
  7.            ///寻找插入节点的插入点(父节点)theY.TheX是循环中间变量.  
  8.            RBTreeNode theY = NullNode;  
  9.            RBTreeNode theX = Root;  
  10.            //注意,如果不为空,则找到的插入节点一定是最底层内节点,仅有空字节点(哨兵节点).  
  11.            //这也是红黑树不是高度平衡树的一个重要原因.但这样简化了插入,不用判断补缺.  
  12.            while (theX != NullNode)  
  13.            {  
  14.                theY = theX;  
  15.                if (Node.Key > theX.Key)  
  16.                {  
  17.                    theX = theX.Right;  
  18.                }  
  19.                else  
  20.                {  
  21.                    theX = theX.Left;  
  22.                }  
  23.            }  
  24.            //将插入节点的父节点指向theY.  
  25.            Node.Parent = theY;  
  26.            //如果theY为空,则原来是空树,则新插入的节点是根节点.  
  27.            if (theY == NullNode)  
  28.            {  
  29.                Root = Node;  
  30.            }  
  31.            else  
  32.            {  
  33.                //如果有根节点,与theY的Key比较,大于则作为右子节点,小于则  
  34.                //作为左节点.  
  35.                if (Node.Key > theY.Key)  
  36.                {  
  37.                    theY.Right = Node; //此处原来写错Node.Parent=theY.Right;  
  38.                }  
  39.                else  
  40.                {  
  41.                    theY.Left = Node; //此处原文写错Node.Parent=theY.Left;  
  42.                }  
  43.            }  
  44.            //开始插入的节点都在非空叶子节点上,将空节点作为其两个子节点.  
  45.            Node.Right = NullNode;  
  46.            Node.Left = NullNode;  
  47.            //颜色置为红,然后整理.  
  48.            Node.NodeColor = RBTreeNodeColor.Red;  
  49.            RBInsertFixup(Node);  
  50.        }  

更正后插入没问题,其它地方没有检验,如不行可参考算法导论完成即可。
0 0