Exemple #1
0
        public KeyValuePair <TKey, TValue> LastOrDefault()
        {
            SplayTreeNode t = root;

            if (t == null)
            {
                return(new KeyValuePair <TKey, TValue>(default(TKey), default(TValue)));
            }

            while (t.RightChild != null)
            {
                t = t.RightChild;
            }

            return(new KeyValuePair <TKey, TValue>(t.Key, t.Value));
        }
Exemple #2
0
        public KeyValuePair <TKey, TValue>?Last()
        {
            SplayTreeNode t = root;

            if (t == null)
            {
                return(null);
            }

            while (t.RightChild != null)
            {
                t = t.RightChild;
            }

            return(new KeyValuePair <TKey, TValue>(t.Key, t.Value));
        }
Exemple #3
0
      void Set(TKey key, TValue value, bool throwOnExisting)
      {
          if (this.count == 0)
          {
              this.version++;
              this.root  = new SplayTreeNode(key, value);
              this.count = 1;
              return;
          }

          this.Splay(key);

          var c = key.CompareTo(this.root.Key);

          if (c == 0)
          {
              if (throwOnExisting)
              {
                  throw new ArgumentException("An item with the same key already exists in the tree.");
              }

              this.version++;
              this.root.Value = value;
              return;
          }

          var n = new SplayTreeNode(key, value);

          if (c < 0)
          {
              n.LeftChild         = this.root.LeftChild;
              n.RightChild        = this.root;
              this.root.LeftChild = null;
          }
          else
          {
              n.RightChild         = this.root.RightChild;
              n.LeftChild          = this.root;
              this.root.RightChild = null;
          }

          this.root = n;
          this.count++;
          this.Splay(key);
          this.version++;
      }
        public void Insert(K key, V value)
        {
            // splay key to root
            if (root == null)
            {
                root = new SplayTreeNode(key, value);
                return;
            }

            root = Splay(root, key);

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

            // Insert new node at root
            if (cmp < 0)
            {
                SplayTreeNode n = new SplayTreeNode(key, value);
                n.lnode    = root.lnode;
                n.rnode    = root;
                root.lnode = null;
                root       = n;
            }

            // Insert new node at root
            else if (cmp > 0)
            {
                SplayTreeNode n = new SplayTreeNode(key, value);
                n.rnode    = root.rnode;
                n.lnode    = root;
                root.rnode = null;
                root       = n;
            }

            // It was a duplicate key. Simply replace the value
            else
            {
                root.Value = value;
            }
        }
Exemple #5
0
 public void Clear()
 {
     this.root  = null;
     this.count = 0;
     this.version++;
 }
Exemple #6
0
      void Splay(TKey key)
      {
          SplayTreeNode l, r, t, y, header;

          l = r = header = new SplayTreeNode(default(TKey), default(TValue));
          t = this.root;
          while (true)
          {
              var c = key.CompareTo(t.Key);
              if (c < 0)
              {
                  if (t.LeftChild == null)
                  {
                      break;
                  }

                  if (key.CompareTo(t.LeftChild.Key) < 0)
                  {
                      y            = t.LeftChild;
                      t.LeftChild  = y.RightChild;
                      y.RightChild = t;
                      t            = y;
                      if (t.LeftChild == null)
                      {
                          break;
                      }
                  }

                  r.LeftChild = t;
                  r           = t;
                  t           = t.LeftChild;
              }
              else if (c > 0)
              {
                  if (t.RightChild == null)
                  {
                      break;
                  }

                  if (key.CompareTo(t.RightChild.Key) > 0)
                  {
                      y            = t.RightChild;
                      t.RightChild = y.LeftChild;
                      y.LeftChild  = t;
                      t            = y;
                      if (t.RightChild == null)
                      {
                          break;
                      }
                  }

                  l.RightChild = t;
                  l            = t;
                  t            = t.RightChild;
              }
              else
              {
                  break;
              }
          }

          l.RightChild = t.LeftChild;
          r.LeftChild  = t.RightChild;
          t.LeftChild  = header.RightChild;
          t.RightChild = header.LeftChild;
          this.root    = t;
      }
Exemple #7
0
        public void CheckIfTreeIsSplayedAfterRemoval()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            tree.Add(75);
            tree.Add(25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            for (int i = 0; i < elementsCount - 1; i++)
            {
                // Get parent of node for removal. That node have to be splayed
                SplayTreeNode <int> curNode  = tree.Root;
                SplayTreeNode <int> lastNode = null;
                while (curNode.Value != i)
                {
                    var cmp = i.CompareTo(curNode.Value);
                    if (cmp == 0)
                    {
                        break;
                    }

                    lastNode = curNode;

                    if (cmp > 0)
                    {
                        curNode = curNode.Right;
                    }
                    else
                    {
                        curNode = curNode.Left;
                    }
                }

                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
                else if (lastNode != null)
                {
                    Assert.IsTrue(tree.Root.Value == lastNode.Value);
                }
            }

            Assert.IsTrue(tree.Count == 1 &&
                          removedEverything &&
                          tree.Root.Value == elementsCount - 1);
        }
        /***************************************************************************
         * Splay tree function.
         * **********************************************************************/
        // splay key in the tree rooted at SplayTreeNode h. If a node with that key exists,
        //   it is splayed to the root of the tree. If it does not, the last node
        //   along the search path for the key is splayed to the root.
        private SplayTreeNode Splay(SplayTreeNode h, K key)
        {
            if (h == null)
            {
                return(null);
            }

            int cmp1 = key.CompareTo(h.Key);

            if (cmp1 < 0)
            {
                // key not in tree, so we're done
                if (h.lnode == null)
                {
                    return(h);
                }
                int cmp2 = key.CompareTo(h.lnode.Key);
                if (cmp2 < 0)
                {
                    h.lnode.lnode = Splay(h.lnode.lnode, key);
                    h             = rotateRight(h);
                }
                else if (cmp2 > 0)
                {
                    h.lnode.rnode = Splay(h.lnode.rnode, key);
                    if (h.lnode.rnode != null)
                    {
                        h.lnode = rotateLeft(h.lnode);
                    }
                }

                if (h.lnode == null)
                {
                    return(h);
                }
                else
                {
                    return(rotateRight(h));
                }
            }

            else if (cmp1 > 0)
            {
                // key not in tree, so we're done
                if (h.rnode == null)
                {
                    return(h);
                }

                int cmp2 = key.CompareTo(h.rnode.Key);
                if (cmp2 < 0)
                {
                    h.rnode.lnode = Splay(h.rnode.lnode, key);
                    if (h.rnode.lnode != null)
                    {
                        h.rnode = rotateRight(h.rnode);
                    }
                }
                else if (cmp2 > 0)
                {
                    h.rnode.rnode = Splay(h.rnode.rnode, key);
                    h             = rotateLeft(h);
                }

                if (h.rnode == null)
                {
                    return(h);
                }
                else
                {
                    return(rotateLeft(h));
                }
            }

            else
            {
                return(h);
            }
        }
Exemple #9
0
 public void Clear()
 {
     root  = null;
     count = 0;
     version++;
 }
Exemple #10
0
 public SplayTreeNode(T paData)
 {
     Parent = null;
     Data   = paData;
 }