Exemple #1
0
        public void RotateLeft(RbNode <T> Node, RbNode <T> Current)
        {
            RbNode <T> ptr = Current.Right;

            Current.Right = ptr.Left;
            if (ptr.Left != null)
            {
                ptr.Left.Parent = Current;
            }
            if (ptr != null)
            {
                ptr.Parent = Current.Parent;
            }
            if (Current.Parent != null)
            {
                if (Current == Current.Parent.Left)
                {
                    Current.Parent.Left = ptr;
                }
                else
                {
                    Current.Parent.Right = ptr;
                }
            }
            else
            {
                Node = ptr;
            }
            ptr.Left = Current;
            if (Current != null)
            {
                Current.Parent = ptr;
            }
        }
Exemple #2
0
        public void Add(Date data, RbNode <T> parent)
        {
            var node = new RbNode <T>(data, parent);

            if (node.Data.CompareTo(Data.Name) == -1)
            {
                if (Left == null)
                {
                    Left = node;
                }
                else
                {
                    Left.Add(data, Left);
                }
            }
            else
            {
                if (Right == null)
                {
                    Right = node;
                }
                else
                {
                    Right.Add(data, Right);
                }
            }

            Fix(parent, node);
        }
Exemple #3
0
 public RbNode(Date data, RbNode <T> left, RbNode <T> right, RbNode <T> parent)
 {
     Data   = data;
     Left   = left;
     Right  = right;
     Parent = parent;
 }
Exemple #4
0
        public void Fix(RbNode <T> Node, RbNode <T> newNode)
        {
            RbNode <T> current = newNode;

            //Проверка свойств
            while (current != Node && current.Parent.Color == "red")
            {
                //Проверка нарушений правил
                if (current.Parent == current.Parent.Parent.Left)
                {
                    RbNode <T> ptr = current.Parent.Parent.Right;
                    if (ptr.Color == "red")
                    {
                        ptr.Color = "black";
                        current.Parent.Parent.Color = "red";
                        current = current.Parent.Parent;
                    }

                    else
                    {
                        if (current == current.Parent.Parent.Right)
                        {
                            //Сделать Current левым потомком
                            current = current.Parent;
                            RotateLeft(Node, current);
                            //Перекрасить и повернуть
                            current.Parent.Color        = "black";
                            current.Parent.Parent.Color = "red";
                            RotateRight(Node, current.Parent.Parent);
                        }
                    }
                }
                else
                {
                    RbNode <T> ptr = current.Parent.Parent.Left;
                    if (ptr.Color == "red")
                    {
                        current.Parent.Color        = "black";
                        ptr.Color                   = "black";
                        current.Parent.Parent.Color = "red";
                        current = current.Parent.Parent;
                    }
                    else
                    {
                        if (current == current.Parent.Left)
                        {
                            current = current.Parent;
                            RotateRight(Node, current);
                        }
                        current.Parent.Color        = "black";
                        current.Parent.Parent.Color = "red";
                        RotateLeft(Node, current.Parent.Parent);
                    }
                }
            }
            Node.Color = "black";
        }
Exemple #5
0
        public void Add(string name, int pasId, int age)
        {
            Date data = new Date(name, pasId, age);

            if (Root == null)
            {
                Root  = new RbNode <T>(data, null);
                Count = 1;
                return;
            }

            Root.Add(data, Root);
            Count++;
        }
Exemple #6
0
 public RbNode(Date data, RbNode <T> parent)
 {
     Data   = data;
     Color  = "red";
     Parent = parent;
 }