Example #1
0
        public void Remove(int x)
        {
            var node = Find(x); //splay of x

            //merge left and right childs
            if (node != null)
            {
                node.LeftChild.Parent  = null;
                node.RightChild.Parent = null;
                var resultTree = Merge(new SplayTree {
                    Root = node.LeftChild
                }, new SplayTree {
                    Root = node.RightChild
                });
                Root = resultTree.Root;
            }
        }
Example #2
0
 private void Splay(SplayTreeNode v)
 {
     while (v.Parent != null)
     {
         if (v.Value == v.Parent.LeftChild.Value)
         {
             if (v.Parent.Parent == null)
             {
                 RotateRight(v.Parent);
             }
             else if (v.Parent.Value == v.Parent.Parent.LeftChild.Value)
             {
                 RotateRight(v.Parent.Parent);
                 RotateRight(v.Parent);
             }
             else
             {
                 RotateRight(v.Parent);
                 RotateLeft(v.Parent);
             }
         }
         else
         {
             if (v.Parent.Parent == null)
             {
                 RotateLeft(v.Parent);
             }
             else if (v.Parent.Value == v.Parent.Parent.RightChild.Value)
             {
                 RotateLeft(v.Parent.Parent);
                 RotateLeft(v.Parent);
             }
             else
             {
                 RotateLeft(v.Parent);
                 RotateRight(v.Parent);
             }
         }
     }
     Root = v;
 }
Example #3
0
 public SplayTree(int[] list)
 {
     foreach (var item in list)
     {
         var current = SearchCurrent(item, Root);
         if (current == null)
         {
             var root = new SplayTreeNode(item);
             Root = root;
         }
         else
         {
             var node = new SplayTreeNode(item, current);
             if (current.Value < item)
             {
                 current.RightChild = node;
             }
             else
             {
                 current.LeftChild = node;
             }
         }
     }
 }
Example #4
0
 public SplayTreeNode(int val, SplayTreeNode parent = null, SplayTreeNode left = null, SplayTreeNode right = null)
 {
     Value      = val;
     Parent     = parent;
     LeftChild  = left;
     RightChild = right;
 }