Exemplo n.º 1
0
		// assumption: x.Parent != null
		SplayTree Zig (SplayTree x)
		{
			if (x.Parent.Left == x)
				return RightRotate (x.Parent);
			else
				return LeftRotate (x.Parent);
		}
Exemplo n.º 2
0
        // assumption: x != null && x.Right != null
        // return value: node that is where rotated node (x) was
        SplayTree LeftRotate(SplayTree x)
        {
            SplayTree y = x.Right;

            x.Right = y.Left;
            if (y.Left != null)
            {
                y.Left.Parent = x;
            }
            y.Parent = x.Parent;

            if (x.HasParent)
            {
                if (x == x.Parent.Left)
                {
                    x.Parent.Left = y;
                }
                else
                {
                    x.Parent.Right = y;
                }
            }

            y.Left   = x;
            x.Parent = y;

            return(y);
        }
Exemplo n.º 3
0
        // assumption: y != null && y.Left != null
        // return value: node that is where rotated node (y) was
        SplayTree RightRotate(SplayTree y)
        {
            SplayTree x = y.Left;

            y.Left = x.Right;
            if (x.Right != null)
            {
                x.Right.Parent = y;
            }
            x.Parent = y.Parent;

            if (y.HasParent)
            {
                if (y == y.Parent.Left)
                {
                    y.Parent.Left = x;
                }
                else
                {
                    y.Parent.Right = x;
                }
            }

            x.Right  = y;
            y.Parent = x;

            return(x);
        }
Exemplo n.º 4
0
		// assumption: x != null && x.Parent != null && x.GrandParent != null
		SplayTree ZigZig (SplayTree x)
		{
			if (x.Parent.Left == x) {
				RightRotate (x.GrandParent);
				x = RightRotate (x.Parent);
			} else {
				LeftRotate (x.GrandParent);
				x = LeftRotate (x.Parent);
			}
			return x;
		}
Exemplo n.º 5
0
 // assumption: x.Parent != null
 SplayTree Zig(SplayTree x)
 {
     if (x.Parent.Left == x)
     {
         return(RightRotate(x.Parent));
     }
     else
     {
         return(LeftRotate(x.Parent));
     }
 }
Exemplo n.º 6
0
 // assumption: x != null && x.Parent != null && x.GrandParent != null
 bool SameDirection(SplayTree x)
 {
     if (x.Parent.Left == x)
     {
         return(x.GrandParent.Left == x.Parent);
     }
     else
     {
         return(x.GrandParent.Right == x.Parent);
     }
 }
Exemplo n.º 7
0
 // assumption: x != null && x.Parent != null && x.GrandParent != null
 SplayTree ZigZag(SplayTree x)
 {
     if (x.Parent.Left == x)
     {
         x = RightRotate(x.Parent);
         x = LeftRotate(x.Parent);
     }
     else
     {
         x = LeftRotate(x.Parent);
         x = RightRotate(x.Parent);
     }
     return(x);
 }
Exemplo n.º 8
0
 // assumption: x != null && x.Parent != null && x.GrandParent != null
 SplayTree ZigZig(SplayTree x)
 {
     if (x.Parent.Left == x)
     {
         RightRotate(x.GrandParent);
         x = RightRotate(x.Parent);
     }
     else
     {
         LeftRotate(x.GrandParent);
         x = LeftRotate(x.Parent);
     }
     return(x);
 }
Exemplo n.º 9
0
        public void SplayToRoot()
        {
            SplayTree x = this;

            while (x.Parent != null)
            {
                if (x.HasGrandParent)
                {
                    if (SameDirection(x))
                    {
                        x = ZigZig(x);
                    }
                    else
                    {
                        x = ZigZag(x);
                    }
                }
                else if (x.HasParent)
                {
                    x = Zig(x);
                }
            }
        }
Exemplo n.º 10
0
		// assumption: x != null && x.Parent != null && x.GrandParent != null
		bool SameDirection (SplayTree x)
		{
			if (x.Parent.Left == x)
				return (x.GrandParent.Left == x.Parent);
			else
				return (x.GrandParent.Right == x.Parent);
		}
Exemplo n.º 11
0
		// assumption: y != null && y.Left != null
		// return value: node that is where rotated node (y) was
		SplayTree RightRotate (SplayTree y)
		{
			SplayTree x = y.Left;
		
			y.Left = x.Right;
			if (x.Right != null)
				x.Right.Parent = y;
			x.Parent = y.Parent;
		
			if (y.HasParent) {
				if (y == y.Parent.Left)
					y.Parent.Left = x;
				else
					y.Parent.Right = x;
			}
		
			x.Right = y;
			y.Parent = x;
	
			return x;
		}
Exemplo n.º 12
0
 public void AddAsRightChild(SplayTree Child)
 {
     this.Right   = Child;
     Child.Parent = this;
 }
Exemplo n.º 13
0
 public void AddAsLeftChild(SplayTree Child)
 {
     this.Left    = Child;
     Child.Parent = this;
 }
Exemplo n.º 14
0
		public void AddAsLeftChild (SplayTree Child)
		{
			this.Left = Child;
			Child.Parent = this;
		}
Exemplo n.º 15
0
		public void AddAsRightChild (SplayTree Child)
		{
			this.Right = Child;
			Child.Parent = this;
		}
Exemplo n.º 16
0
		// assumption: x != null && x.Parent != null && x.GrandParent != null
		SplayTree ZigZag (SplayTree x)
		{
			if (x.Parent.Left == x) {
				x = RightRotate (x.Parent);
				x = LeftRotate (x.Parent);
			} else {
				x = LeftRotate (x.Parent);
				x = RightRotate (x.Parent);
			}
			return x;
		}
Exemplo n.º 17
0
		// assumption: x != null && x.Right != null
		// return value: node that is where rotated node (x) was
		SplayTree LeftRotate (SplayTree x)
		{
			SplayTree y = x.Right;
	
			x.Right = y.Left;
			if (y.Left != null)
				y.Left.Parent = x;
			y.Parent = x.Parent;
	
			if (x.HasParent) {
				if (x == x.Parent.Left)
					x.Parent.Left = y;
				else
					x.Parent.Right = y;
			}
		
			y.Left = x;
			x.Parent = y;
			
			return y;
		}