Example #1
0
        protected void RotateLeft(SplayNode <T> father)
        {
            var son = father.Right;

            if (son != null)
            {
                if (son.Left != null)
                {
                    son.Left.Parent = father;
                }
                father.Right = son.Left;
                son.Parent   = father.Parent;
                son.Left     = father;
            }

            if (father.Parent == null)
            {
                Root = son;
            }
            else if (father == father.Parent.Left)
            {
                father.Parent.Left = son;
            }
            else
            {
                father.Parent.Right = son;
            }

            father.Parent = son;
        }
        public void Remove(K key)
        {
            if (root == null)
            {
                return;
            }
            root = splay(root, key);
            int compare = key.CompareTo(root.Key);

            if (compare == 0)
            {
                if (root.Left == null)
                {
                    root = root.Right;
                }
                else
                {
                    SplayNode node = root.Right;
                    root = root.Left;
                    splay(node, key);
                    root.Right = node;
                }
                _count--;
            }
        }
Example #3
0
        SplayNode SplayInsert(SplayNode splayroot, Otri newkey, Point searchpoint)
        {
            SplayNode newsplaynode;

            newsplaynode = new SplayNode(); //poolalloc(m.splaynodes);
            splaynodes.Add(newsplaynode);
            newkey.Copy(ref newsplaynode.keyedge);
            newsplaynode.keydest = newkey.Dest();
            if (splayroot == null)
            {
                newsplaynode.lchild = null;
                newsplaynode.rchild = null;
            }
            else if (RightOfHyperbola(ref splayroot.keyedge, searchpoint))
            {
                newsplaynode.lchild = splayroot;
                newsplaynode.rchild = splayroot.rchild;
                splayroot.rchild    = null;
            }
            else
            {
                newsplaynode.lchild = splayroot.lchild;
                newsplaynode.rchild = splayroot;
                splayroot.lchild    = null;
            }
            return(newsplaynode);
        }
        public void Add(K key, T value)
        {
            if (root == null)
            {
                root = new SplayNode(key, value);
                _count++;
                return;
            }
            root = splay(root, key);

            int compare = key.CompareTo(root.Key);

            if (compare < 0)
            {
                SplayNode node = new SplayNode(key, value);
                node.Left  = root.Left;
                node.Right = root;
                root.Left  = null;
                root       = node;
            }
            else if (compare > 0)
            {
                SplayNode node = new SplayNode(key, value);
                node.Right = root.Right;
                node.Left  = root;
                root.Right = null;
                root.Right = node;
            }
            else
            {
                root.Value = value;
                _count--;
            }
            _count++;
        }
Example #5
0
        private SplayNode <T> DeleteAux(SplayNode <T> node, T value)
        {
            if (node == null)
            {
            }
            else if (node.value.CompareTo(value) == 1)
            {
                node.left = DeleteAux(node.left, value);
            }
            else if (node.value.CompareTo(value) == -1)
            {
                node.right = DeleteAux(node.right, value);
            }
            else if (node.left != null && node.right != null)
            {
                T replace = FindMin(node.right).value;

                node.value = replace;
                node.right = DeleteAux(node.right, value);
            }
            else
            {
                node = node.left == null ? node.right : node.left;
            }
            return(node);
        }
        private SplayNode rotateLeft(SplayNode node)
        {
            SplayNode res = node.Right;

            node.Right = res.Left;
            res.Left   = node;
            return(res);
        }
 private int height(SplayNode x)
 {
     if (x == null)
     {
         return(-1);
     }
     return(1 + Math.Max(height(x.Left), height(x.Right)));
 }
Example #8
0
 private SplayNode <T> FindMin(SplayNode <T> node)
 {
     if (node.left == null)
     {
         return(node);
     }
     return(FindMin(node.left));
 }
Example #9
0
 protected override void Splay(SplayNode <T> node)
 {
     while (node.Parent != null)
     {
         if (node.Parent.Left == node)
         {
             RotateRight(node.Parent);
         }
         else
         {
             RotateLeft(node.Parent);
         }
     }
 }
Example #10
0
 private SplayNode <T> GetAux(SplayNode <T> node, T value)
 {
     if (node == null)
     {
     }
     else if (node.value.CompareTo(value) == 1)
     {
         node = GetAux(node.left, value);
         node.RotateRight();
     }
     else if (node.value.CompareTo(value) == -1)
     {
         node = GetAux(node.right, value);
         node.RotateLeft();
     }
     return(node);
 }
Example #11
0
        SplayNode FrontLocate(SplayNode splayroot, Otri bottommost, Vertex searchvertex,
                              ref Otri searchtri, ref bool farright)
        {
            bool farrightflag;

            bottommost.Copy(ref searchtri);
            splayroot = Splay(splayroot, searchvertex, ref searchtri);

            farrightflag = false;
            while (!farrightflag && RightOfHyperbola(ref searchtri, searchvertex))
            {
                searchtri.Onext();
                farrightflag = searchtri.Equals(bottommost);
            }
            farright = farrightflag;
            return(splayroot);
        }
Example #12
0
        public void Insert(T key)
        {
            if (Root == null)
            {
                Root = CreateNode(key);
                return;
            }

            var node = FindNode(key);

            Splay(node);

            var cmp = key.CompareTo(Root.Key);

            if (cmp < 0)
            {
                var newNode = CreateNode(key);
                newNode.Left = Root.Left;
                Root.Left    = newNode;

                newNode.Parent = Root;
                if (newNode.Left != null)
                {
                    newNode.Left.Parent = newNode;
                }
            }
            else if (cmp > 0)
            {
                var newNode = CreateNode(key);
                newNode.Right = Root.Right;
                Root.Right    = newNode;

                newNode.Parent = Root;
                if (newNode.Right != null)
                {
                    newNode.Right.Parent = newNode;
                }
            }
            else
            {
                // key exists, do nothing
            }
        }
Example #13
0
        SplayNode CircleTopInsert(SplayNode splayroot, Otri newkey,
                                  Vertex pa, Vertex pb, Vertex pc, double topy)
        {
            double ccwabc;
            double xac, yac, xbc, ybc;
            double aclen2, bclen2;
            Point  searchpoint = new Point(); // TODO: mesh.nextras
            Otri   dummytri    = default(Otri);

            ccwabc        = predicates.CounterClockwise(pa, pb, pc);
            xac           = pa.x - pc.x;
            yac           = pa.y - pc.y;
            xbc           = pb.x - pc.x;
            ybc           = pb.y - pc.y;
            aclen2        = xac * xac + yac * yac;
            bclen2        = xbc * xbc + ybc * ybc;
            searchpoint.x = pc.x - (yac * bclen2 - ybc * aclen2) / (2.0 * ccwabc);
            searchpoint.y = topy;
            return(SplayInsert(Splay(splayroot, searchpoint, ref dummytri), newkey, searchpoint));
        }
 public T this[K key]
 {
     get
     {
         try
         {
             root = splay(root, key);
             return(key.CompareTo(root.Key) == 0 ? root.Value : default(T));
         }
         catch { return(default(T)); }
     }
     set
     {
         root = splay(root, key);
         if (key.CompareTo(key) == 0)
         {
             root.Value = value;
         }
         else
         {
             Add(key, value);
         }
     }
 }
Example #15
0
        SplayNode SplayInsert(SplayNode splayroot, Otri newkey, Point searchpoint)
        {
            SplayNode newsplaynode;

            newsplaynode = new SplayNode(); //poolalloc(m.splaynodes);
            splaynodes.Add(newsplaynode);
            newkey.Copy(ref newsplaynode.keyedge);
            newsplaynode.keydest = newkey.Dest();
            if (splayroot == null)
            {
                newsplaynode.lchild = null;
                newsplaynode.rchild = null;
            }
            else if (RightOfHyperbola(ref splayroot.keyedge, searchpoint))
            {
                newsplaynode.lchild = splayroot;
                newsplaynode.rchild = splayroot.rchild;
                splayroot.rchild = null;
            }
            else
            {
                newsplaynode.lchild = splayroot.lchild;
                newsplaynode.rchild = splayroot;
                splayroot.lchild = null;
            }
            return newsplaynode;
        }
Example #16
0
        SplayNode Splay(SplayNode splaytree, Point searchpoint, ref Otri searchtri)
        {
            SplayNode child, grandchild;
            SplayNode lefttree, righttree;
            SplayNode leftright;
            Vertex checkvertex;
            bool rightofroot, rightofchild;

            if (splaytree == null)
            {
                return null;
            }
            checkvertex = splaytree.keyedge.Dest();
            if (checkvertex == splaytree.keydest)
            {
                rightofroot = RightOfHyperbola(ref splaytree.keyedge, searchpoint);
                if (rightofroot)
                {
                    splaytree.keyedge.Copy(ref searchtri);
                    child = splaytree.rchild;
                }
                else
                {
                    child = splaytree.lchild;
                }
                if (child == null)
                {
                    return splaytree;
                }
                checkvertex = child.keyedge.Dest();
                if (checkvertex != child.keydest)
                {
                    child = Splay(child, searchpoint, ref searchtri);
                    if (child == null)
                    {
                        if (rightofroot)
                        {
                            splaytree.rchild = null;
                        }
                        else
                        {
                            splaytree.lchild = null;
                        }
                        return splaytree;
                    }
                }
                rightofchild = RightOfHyperbola(ref child.keyedge, searchpoint);
                if (rightofchild)
                {
                    child.keyedge.Copy(ref searchtri);
                    grandchild = Splay(child.rchild, searchpoint, ref searchtri);
                    child.rchild = grandchild;
                }
                else
                {
                    grandchild = Splay(child.lchild, searchpoint, ref searchtri);
                    child.lchild = grandchild;
                }
                if (grandchild == null)
                {
                    if (rightofroot)
                    {
                        splaytree.rchild = child.lchild;
                        child.lchild = splaytree;
                    }
                    else
                    {
                        splaytree.lchild = child.rchild;
                        child.rchild = splaytree;
                    }
                    return child;
                }
                if (rightofchild)
                {
                    if (rightofroot)
                    {
                        splaytree.rchild = child.lchild;
                        child.lchild = splaytree;
                    }
                    else
                    {
                        splaytree.lchild = grandchild.rchild;
                        grandchild.rchild = splaytree;
                    }
                    child.rchild = grandchild.lchild;
                    grandchild.lchild = child;
                }
                else
                {
                    if (rightofroot)
                    {
                        splaytree.rchild = grandchild.lchild;
                        grandchild.lchild = splaytree;
                    }
                    else
                    {
                        splaytree.lchild = child.rchild;
                        child.rchild = splaytree;
                    }
                    child.lchild = grandchild.rchild;
                    grandchild.rchild = child;
                }
                return grandchild;
            }
            else
            {
                lefttree = Splay(splaytree.lchild, searchpoint, ref searchtri);
                righttree = Splay(splaytree.rchild, searchpoint, ref searchtri);

                splaynodes.Remove(splaytree);
                if (lefttree == null)
                {
                    return righttree;
                }
                else if (righttree == null)
                {
                    return lefttree;
                }
                else if (lefttree.rchild == null)
                {
                    lefttree.rchild = righttree.lchild;
                    righttree.lchild = lefttree;
                    return righttree;
                }
                else if (righttree.lchild == null)
                {
                    righttree.lchild = lefttree.rchild;
                    lefttree.rchild = righttree;
                    return lefttree;
                }
                else
                {
                    //      printf("Holy Toledo!!!\n");
                    leftright = lefttree.rchild;
                    while (leftright.rchild != null)
                    {
                        leftright = leftright.rchild;
                    }
                    leftright.rchild = righttree;
                    return lefttree;
                }
            }
        }
Example #17
0
 public SplayNode <T> Get(T value)
 {
     root = GetAux(root, value);
     return(root);
 }
Example #18
0
        SplayNode Splay(SplayNode splaytree, Point searchpoint, ref Otri searchtri)
        {
            SplayNode child, grandchild;
            SplayNode lefttree, righttree;
            SplayNode leftright;
            Vertex    checkvertex;
            bool      rightofroot, rightofchild;

            if (splaytree == null)
            {
                return(null);
            }
            checkvertex = splaytree.keyedge.Dest();
            if (checkvertex == splaytree.keydest)
            {
                rightofroot = RightOfHyperbola(ref splaytree.keyedge, searchpoint);
                if (rightofroot)
                {
                    splaytree.keyedge.Copy(ref searchtri);
                    child = splaytree.rchild;
                }
                else
                {
                    child = splaytree.lchild;
                }
                if (child == null)
                {
                    return(splaytree);
                }
                checkvertex = child.keyedge.Dest();
                if (checkvertex != child.keydest)
                {
                    child = Splay(child, searchpoint, ref searchtri);
                    if (child == null)
                    {
                        if (rightofroot)
                        {
                            splaytree.rchild = null;
                        }
                        else
                        {
                            splaytree.lchild = null;
                        }
                        return(splaytree);
                    }
                }
                rightofchild = RightOfHyperbola(ref child.keyedge, searchpoint);
                if (rightofchild)
                {
                    child.keyedge.Copy(ref searchtri);
                    grandchild   = Splay(child.rchild, searchpoint, ref searchtri);
                    child.rchild = grandchild;
                }
                else
                {
                    grandchild   = Splay(child.lchild, searchpoint, ref searchtri);
                    child.lchild = grandchild;
                }
                if (grandchild == null)
                {
                    if (rightofroot)
                    {
                        splaytree.rchild = child.lchild;
                        child.lchild     = splaytree;
                    }
                    else
                    {
                        splaytree.lchild = child.rchild;
                        child.rchild     = splaytree;
                    }
                    return(child);
                }
                if (rightofchild)
                {
                    if (rightofroot)
                    {
                        splaytree.rchild = child.lchild;
                        child.lchild     = splaytree;
                    }
                    else
                    {
                        splaytree.lchild  = grandchild.rchild;
                        grandchild.rchild = splaytree;
                    }
                    child.rchild      = grandchild.lchild;
                    grandchild.lchild = child;
                }
                else
                {
                    if (rightofroot)
                    {
                        splaytree.rchild  = grandchild.lchild;
                        grandchild.lchild = splaytree;
                    }
                    else
                    {
                        splaytree.lchild = child.rchild;
                        child.rchild     = splaytree;
                    }
                    child.lchild      = grandchild.rchild;
                    grandchild.rchild = child;
                }
                return(grandchild);
            }
            else
            {
                lefttree  = Splay(splaytree.lchild, searchpoint, ref searchtri);
                righttree = Splay(splaytree.rchild, searchpoint, ref searchtri);

                splaynodes.Remove(splaytree);
                if (lefttree == null)
                {
                    return(righttree);
                }
                else if (righttree == null)
                {
                    return(lefttree);
                }
                else if (lefttree.rchild == null)
                {
                    lefttree.rchild  = righttree.lchild;
                    righttree.lchild = lefttree;
                    return(righttree);
                }
                else if (righttree.lchild == null)
                {
                    righttree.lchild = lefttree.rchild;
                    lefttree.rchild  = righttree;
                    return(lefttree);
                }
                else
                {
                    //      printf("Holy Toledo!!!\n");
                    leftright = lefttree.rchild;
                    while (leftright.rchild != null)
                    {
                        leftright = leftright.rchild;
                    }
                    leftright.rchild = righttree;
                    return(lefttree);
                }
            }
        }
Example #19
0
 public void Clear()
 {
     Root = null;
     WasRead.Clear();
 }
Example #20
0
 protected abstract void Splay(SplayNode <T> node2);
Example #21
0
        SplayNode CircleTopInsert(SplayNode splayroot, Otri newkey,
                                  Vertex pa, Vertex pb, Vertex pc, double topy)
        {
            double ccwabc;
            double xac, yac, xbc, ybc;
            double aclen2, bclen2;
            Point searchpoint = new Point(); // TODO: mesh.nextras
            Otri dummytri = default(Otri);

            ccwabc = Primitives.CounterClockwise(pa, pb, pc);
            xac = pa.x - pc.x;
            yac = pa.y - pc.y;
            xbc = pb.x - pc.x;
            ybc = pb.y - pc.y;
            aclen2 = xac * xac + yac * yac;
            bclen2 = xbc * xbc + ybc * ybc;
            searchpoint.x = pc.x - (yac * bclen2 - ybc * aclen2) / (2.0 * ccwabc);
            searchpoint.y = topy;
            return SplayInsert(Splay(splayroot, searchpoint, ref dummytri), newkey, searchpoint);
        }
Example #22
0
 public void Append(T value)
 {
     root = AppendAux(root, value);
 }
Example #23
0
 public void Delete(T value)
 {
     root = DeleteAux(root, value);
 }
Example #24
0
        SplayNode FrontLocate(SplayNode splayroot, Otri bottommost, Vertex searchvertex,
                              ref Otri searchtri, ref bool farright)
        {
            bool farrightflag;

            bottommost.Copy(ref searchtri);
            splayroot = Splay(splayroot, searchvertex, ref searchtri);

            farrightflag = false;
            while (!farrightflag && RightOfHyperbola(ref searchtri, searchvertex))
            {
                searchtri.OnextSelf();
                farrightflag = searchtri.Equal(bottommost);
            }
            farright = farrightflag;
            return splayroot;
        }
        private SplayNode splay(SplayNode node, K key)
        {
            if (node == null)
            {
                return(null);
            }
            int compare1 = key.CompareTo(node.Key);

            if (compare1 < 0)
            {
                if (node.Left == null)
                {
                    return(node);
                }
                int compare2 = key.CompareTo(node.Left.Key);
                if (compare2 < 0)
                {
                    node.Left.Left = splay(node.Left.Left, key);
                    node           = rotateRight(node);
                }
                else if (compare2 > 0)
                {
                    node.Left.Right = splay(node.Left.Right, key);
                    if (node.Left.Right != null)
                    {
                        node.Left = rotateLeft(node.Left);
                    }
                }

                if (node.Left == null)
                {
                    return(node);
                }
                else
                {
                    return(rotateRight(node));
                }
            }
            else if (compare1 > 0)
            {
                if (node.Right == null)
                {
                    return(node);
                }
                int compare2 = key.CompareTo(node.Right.Key);
                if (compare2 < 0)
                {
                    node.Right.Left = splay(node.Right.Left, key);
                    if (node.Right.Left != null)
                    {
                        node.Right = rotateLeft(node.Right);
                    }
                }
                else if (compare2 > 0)
                {
                    node.Right.Right = splay(node.Right.Right, key);
                    node             = rotateLeft(node);
                }

                if (node.Right == null)
                {
                    return(node);
                }
                else
                {
                    return(rotateLeft(node));
                }
            }
            else
            {
                return(node);
            }
        }