Esempio n. 1
0
 internal bool InOrderTreeWalk(TreeWalkAction <T> action)
 {
     if (this.root != null)
     {
         Stack <TreeSet <T> .Node> stack = new Stack <TreeSet <T> .Node>(2 * (int)System.Math.Log((double)(this.Count + 1)));
         for (TreeSet <T> .Node node = this.root; node != null; node = node.Left)
         {
             stack.Push(node);
         }
         while (stack.Count != 0)
         {
             TreeSet <T> .Node node = stack.Pop();
             if (!action(node))
             {
                 return(false);
             }
             for (TreeSet <T> .Node node2 = node.Right; node2 != null; node2 = node2.Left)
             {
                 stack.Push(node2);
             }
         }
     }
     return(true);
 }
Esempio n. 2
0
 public bool Callback(TreeSet <T> .Node node)
 {
     this.array[this.index++] = node.Item;
     return(true);
 }
Esempio n. 3
0
 private static void Split4Node(TreeSet <T> .Node node)
 {
     node.IsRed       = true;
     node.Left.IsRed  = false;
     node.Right.IsRed = false;
 }
Esempio n. 4
0
 private static TreeRotation RotationNeeded(TreeSet <T> .Node parent, TreeSet <T> .Node current, TreeSet <T> .Node sibling)
 {
     if (TreeSet <T> .IsRed(sibling.Left))
     {
         if (parent.Left == current)
         {
             return(TreeRotation.RightLeftRotation);
         }
         return(TreeRotation.RightRotation);
     }
     else
     {
         if (parent.Left == current)
         {
             return(TreeRotation.LeftRotation);
         }
         return(TreeRotation.LeftRightRotation);
     }
 }
Esempio n. 5
0
 private void ReplaceNode(TreeSet <T> .Node match, TreeSet <T> .Node parentOfMatch, TreeSet <T> .Node succesor, TreeSet <T> .Node parentOfSuccesor)
 {
     if (succesor == match)
     {
         succesor = match.Left;
     }
     else
     {
         if (succesor.Right != null)
         {
             succesor.Right.IsRed = false;
         }
         if (parentOfSuccesor != match)
         {
             parentOfSuccesor.Left = succesor.Right;
             succesor.Right        = match.Right;
         }
         succesor.Left = match.Left;
     }
     if (succesor != null)
     {
         succesor.IsRed = match.IsRed;
     }
     this.ReplaceChildOfNodeOrRoot(parentOfMatch, match, succesor);
 }
Esempio n. 6
0
 private void ReplaceChildOfNodeOrRoot(TreeSet <T> .Node parent, TreeSet <T> .Node child, TreeSet <T> .Node newChild)
 {
     if (parent == null)
     {
         this.root = newChild;
         return;
     }
     if (parent.Left == child)
     {
         parent.Left = newChild;
         return;
     }
     parent.Right = newChild;
 }
Esempio n. 7
0
        public bool Remove(T item)
        {
            if (this.root == null)
            {
                return(false);
            }
            TreeSet <T> .Node node          = this.root;
            TreeSet <T> .Node node2         = null;
            TreeSet <T> .Node node3         = null;
            TreeSet <T> .Node node4         = null;
            TreeSet <T> .Node parentOfMatch = null;
            bool flag = false;

            while (node != null)
            {
                if (TreeSet <T> .Is2Node(node))
                {
                    if (node2 == null)
                    {
                        node.IsRed = true;
                    }
                    else
                    {
                        TreeSet <T> .Node node5 = TreeSet <T> .GetSibling(node, node2);

                        if (node5.IsRed)
                        {
                            if (node2.Right == node5)
                            {
                                TreeSet <T> .RotateLeft(node2);
                            }
                            else
                            {
                                TreeSet <T> .RotateRight(node2);
                            }
                            node2.IsRed = true;
                            node5.IsRed = false;
                            this.ReplaceChildOfNodeOrRoot(node3, node2, node5);
                            node3 = node5;
                            if (node2 == node4)
                            {
                                parentOfMatch = node5;
                            }
                            node5 = ((node2.Left == node) ? node2.Right : node2.Left);
                        }
                        if (TreeSet <T> .Is2Node(node5))
                        {
                            TreeSet <T> .Merge2Nodes(node2, node, node5);
                        }
                        else
                        {
                            TreeRotation treeRotation = TreeSet <T> .RotationNeeded(node2, node, node5);

                            TreeSet <T> .Node node6 = null;
                            switch (treeRotation)
                            {
                            case TreeRotation.LeftRotation:
                                node5.Right.IsRed = false;
                                node6             = TreeSet <T> .RotateLeft(node2);

                                break;

                            case TreeRotation.RightRotation:
                                node5.Left.IsRed = false;
                                node6            = TreeSet <T> .RotateRight(node2);

                                break;

                            case TreeRotation.RightLeftRotation:
                                node6 = TreeSet <T> .RotateRightLeft(node2);

                                break;

                            case TreeRotation.LeftRightRotation:
                                node6 = TreeSet <T> .RotateLeftRight(node2);

                                break;
                            }
                            node6.IsRed = node2.IsRed;
                            node2.IsRed = false;
                            node.IsRed  = true;
                            this.ReplaceChildOfNodeOrRoot(node3, node2, node6);
                            if (node2 == node4)
                            {
                                parentOfMatch = node6;
                            }
                        }
                    }
                }
                int num = flag ? -1 : this.comparer.Compare(item, node.Item);
                if (num == 0)
                {
                    flag          = true;
                    node4         = node;
                    parentOfMatch = node2;
                }
                node3 = node2;
                node2 = node;
                if (num < 0)
                {
                    node = node.Left;
                }
                else
                {
                    node = node.Right;
                }
            }
            if (node4 != null)
            {
                this.ReplaceNode(node4, parentOfMatch, node2, node3);
                this.count--;
            }
            if (this.root != null)
            {
                this.root.IsRed = false;
            }
            this.version++;
            return(flag);
        }
Esempio n. 8
0
 private static void Merge2Nodes(TreeSet <T> .Node parent, TreeSet <T> .Node child1, TreeSet <T> .Node child2)
 {
     parent.IsRed = false;
     child1.IsRed = true;
     child2.IsRed = true;
 }
Esempio n. 9
0
 private static bool IsRed(TreeSet <T> .Node node)
 {
     return(node != null && node.IsRed);
 }
Esempio n. 10
0
 private static bool IsNullOrBlack(TreeSet <T> .Node node)
 {
     return(node == null || !node.IsRed);
 }
Esempio n. 11
0
 private static bool Is4Node(TreeSet <T> .Node node)
 {
     return(TreeSet <T> .IsRed(node.Left) && TreeSet <T> .IsRed(node.Right));
 }
Esempio n. 12
0
 private static bool Is2Node(TreeSet <T> .Node node)
 {
     return(TreeSet <T> .IsBlack(node) && TreeSet <T> .IsNullOrBlack(node.Left) && TreeSet <T> .IsNullOrBlack(node.Right));
 }
Esempio n. 13
0
        private void InsertionBalance(TreeSet <T> .Node current, ref TreeSet <T> .Node parent, TreeSet <T> .Node grandParent, TreeSet <T> .Node greatGrandParent)
        {
            bool flag  = grandParent.Right == parent;
            bool flag2 = parent.Right == current;

            TreeSet <T> .Node node;
            if (flag == flag2)
            {
                node = (flag2 ? TreeSet <T> .RotateLeft(grandParent) : TreeSet <T> .RotateRight(grandParent));
            }
            else
            {
                node   = (flag2 ? TreeSet <T> .RotateLeftRight(grandParent) : TreeSet <T> .RotateRightLeft(grandParent));
                parent = greatGrandParent;
            }
            grandParent.IsRed = true;
            node.IsRed        = false;
            this.ReplaceChildOfNodeOrRoot(greatGrandParent, grandParent, node);
        }