/// <summary>
        /// join two tree T1 and T2, for each key in T1 &lt; each key in T2.
        /// first access the maximum key in T1, After this the root of T1 is the maximum key in T1. let the root of T1 is the new root of the join of T1 and T2
        /// return the new root of tree
        /// </summary>
        /// <param name="T1">T1.parent must be null</param>
        /// <param name="T2">T2.parent must be null</param>
        private SplayNode <K, V> Join(SplayNode <K, V> T1, SplayNode <K, V> T2)
        {
            if (T1 == null && T2 == null)
            {
                root = null;
                return(null);
            }

            if (T1 == null)
            {
                T2.Parent = null;
                root      = T2;
                return(T2);
            }

            if (T2 == null)
            {
                T1.Parent = null;
                root      = T1;
                return(T1);
            }

            T1.Parent = null;
            root      = T1;
            K maxKey = Maximum(T1).Item1;

            Search_(maxKey);
            root.Right = T2;
            T2.Parent  = root;
            return(root);
        }
        /// <summary>
        /// Search a node whose key is key, if found the node, then splay node.
        /// if can not found node, then return null & splay the most recently visited node
        /// Search is the heart of splay tree.
        /// Search should have the following property.
        /// after search key, the key in r
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public SplayNode <K, V> Search_(K key)
        {
            if (root == null)
            {
                return(null);
            }

            SplayNode <K, V> node = root, lastVisited = null;

            while (node != null)
            {
                lastVisited = node;
                int i = node.Key.CompareTo(key);
                if (i == 0)
                {
                    BottomUpSplay(node);
                    // now node is moved to the root
                    return(node);
                }
                else if (i > 0)
                {
                    node = node.Left;
                }
                else
                {
                    node = node.Right;
                }
            }

            // can not find the key, splay the most recently visited node, then return null
            BottomUpSplay(lastVisited);
            return(null);
        }
        /// <summary>
        /// right rotate
        /// </summary>
        /// <param name="node">node must have non null left child</param>
        private void RightRotate(SplayNode <K, V> node)
        {
            SplayNode <K, V> left = node.Left;

            node.Left = left.Right;

            if (left.Right != null)
            {
                left.Right.Parent = node;
            }

            left.Parent = node.Parent;

            if (node.Parent == null)
            {
                root = left;
            }
            else if (node == node.Parent.Left)
            {
                node.Parent.Left = left;
            }
            else
            {
                node.Parent.Right = left;
            }

            left.Right  = node;
            node.Parent = left;
        }
        /// <summary>
        /// left rotate
        /// </summary>
        /// <param name="node">node must have non null right node</param>
        private void LeftRotate(SplayNode <K, V> node)
        {
            SplayNode <K, V> right = node.Right;

            node.Right = right.Left;
            if (right.Left != null)
            {
                right.Left.Parent = node;
            }

            right.Parent = node.Parent;

            if (node.Parent == null)
            {
                root = right;
            }
            else if (node == node.Parent.Left)
            {
                node.Parent.Left = right;
            }
            else
            {
                node.Parent.Right = right;
            }

            right.Left  = node;
            node.Parent = right;
        }
        /// <summary>
        /// splay function move node to the root
        /// 0)if node's parent is null, then node is root, no need to do anything
        /// 1)if node has parent but doesn't have grand parent, if node is left child, then right rotate node's parent. if node is right child, then left rotate node's parent. After this, node move to root. no need to do something else.
        /// 2)if node has both parent (named P) and grand parent(named G). node and P are both left(right) child, then right(left) rotate P-G && P-Node.  node is left(right) child & parent is right(left) child, then right(left) rotate P-Node && left(right) rotate G-Node.
        /// </summary>
        /// <param name="node"></param>
        private void BottomUpSplay(SplayNode <K, V> node)
        {
            if (node.Parent == null)
            {
                //no need to do anything
                return;
            }

            if (node.Parent.Parent == null)
            {
                //node has parent, but node.parent doesn;t have parent
                if (node == node.Parent.Left)
                {
                    RightRotate(node.Parent);
                    //also no need to do anything
                }
                else
                {
                    LeftRotate(node.Parent);
                    //also no need to do anything
                }
            }
            else
            {
                if (node == node.Parent.Left)
                {
                    if (node.Parent == node.Parent.Parent.Left)
                    {
                        RightRotate(node.Parent.Parent);
                        RightRotate(node.Parent);
                        //recursive call
                        BottomUpSplay(node);
                    }
                    else
                    {
                        RightRotate(node.Parent);
                        LeftRotate(node.Parent);
                        BottomUpSplay(node);
                    }
                }
                else
                {
                    if (node.Parent == node.Parent.Parent.Right)
                    {
                        LeftRotate(node.Parent.Parent);
                        LeftRotate(node.Parent);
                        BottomUpSplay(node);
                    }
                    else
                    {
                        LeftRotate(node.Parent);
                        RightRotate(node.Parent);
                        BottomUpSplay(node);
                    }
                }
            }
        }
        /// <summary>
        /// inorder tree walk
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="node"></param>
        private void InorderTreeWalk_(ref List <SplayNode <K, V> > lists, SplayNode <K, V> node)
        {
            if (node == null)
            {
                return;
            }

            InorderTreeWalk_(ref lists, node.Left);
            lists.Add(node);
            InorderTreeWalk_(ref lists, node.Right);
        }
        /// <summary>
        /// inorder tree walk
        /// </summary>
        /// <param name="lists"></param>
        /// <param name="root"></param>
        private void InorderTreeWalk(ref List <Tuple <K, V> > lists, SplayNode <K, V> node)
        {
            if (node == null)
            {
                return;
            }

            InorderTreeWalk(ref lists, node.Left);
            lists.Add(new Tuple <K, V>(node.Key, node.Value));
            InorderTreeWalk(ref lists, node.Right);
        }
        /// <summary>
        /// return the Maximum one
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private SplayNode <K, V> Maximum_(SplayNode <K, V> node)
        {
            if (node == null)
            {
                return(null);
            }

            while (node.Right != null)
            {
                node = node.Right;
            }

            return(node);
        }
        /// <summary>
        /// return the Maximum one
        /// </summary>
        /// <returns></returns>
        private Tuple <K, V> Maximum(SplayNode <K, V> node)
        {
            if (node == null)
            {
                return(null);
            }

            while (node.Right != null)
            {
                node = node.Right;
            }

            return(new Tuple <K, V>(node.Key, node.Value));
        }
Ejemplo n.º 10
0
        /// <summary>
        /// return the minimum one
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        private SplayNode <K, V> Minimum_(SplayNode <K, V> node)
        {
            if (node == null)
            {
                return(null);
            }

            while (node.Left != null)
            {
                node = node.Left;
            }

            return(node);
        }
Ejemplo n.º 11
0
        /// <summary>
        /// determine whether a node is binary search tree node
        /// </summary>
        /// <param name="node"></param>
        /// <returns></returns>
        public bool IsBSTNode(SplayNode <K, V> node)
        {
            if (node == null)
            {
                return(true);
            }

            if (node.Left != null && node.Left.Key.CompareTo(node.Key) > 0)
            {
                return(false);
            }

            if (node.Right != null && node.Right.Key.CompareTo(node.Key) < 0)
            {
                return(false);
            }

            return(true);
        }
Ejemplo n.º 12
0
        /// <summary>
        /// delete the key from the tree.
        /// 1) search key in the tree.
        /// 2) join the left child and right child of the new root if k is in the tree
        /// </summary>
        /// <param name="key"></param>
        public void Delete(K key)
        {
            if (root == null)
            {
                return;
            }

            SplayNode <K, V> node = Search_(key);

            if (node == null)
            {
                //tree doesn't contain a key
                return;
            }

            count--;

            Join(node.Left, node.Right);
        }
Ejemplo n.º 13
0
        /// <summary>
        /// insert a new node to splay tree
        /// insert the new node to the splay tree, then splay the new node.
        /// </summary>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void Insert(K key, V val)
        {
            count++;
            SplayNode <K, V> inserted = new SplayNode <K, V>(key, val);

            if (root == null)
            {
                //insert the first node
                root = inserted;
                return;
            }

            SplayNode <K, V> node = root, lastVisited = null;
            bool             isRightChild = false;

            while (node != null)
            {
                lastVisited = node;
                int i = node.Key.CompareTo(key);
                if (i > 0)
                {
                    node         = node.Left;
                    isRightChild = false;
                }
                else
                {
                    node         = node.Right;
                    isRightChild = true;
                }
            }

            inserted.Parent = lastVisited;
            if (isRightChild)
            {
                lastVisited.Right = inserted;
            }
            else
            {
                lastVisited.Left = inserted;
            }

            BottomUpSplay(inserted);
        }
Ejemplo n.º 14
0
        /// <summary>
        /// Search a node whose key is key, if found the node, then splay node.
        /// if can not found node, then return null & splay the most recently visited node
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public Tuple <K, V> Search(K key)
        {
            SplayNode <K, V> node = Search_(key);

            return(node == null ? null : new Tuple <K, V>(node.Key, node.Value));
        }