示例#1
0
        //tree is right heavy
        public void RotateLeft(AVLnode <T> node) // 3
        {
            AVLnode <T> child = node.RightChild;

            node.RightChild = null;
            if (node == Root)
            {
                Root = child;
                Root.SetRoot();
            }
            else
            {
                child.Parent          = node.Parent;
                node.Parent.LeftChild = child;
                child.LeftChild       = node;
                node.Parent           = child;
            }

            if (child != null)
            {
                child.LeftChild = node;
            }



            //AVLnode<T> child = node.RightChild;
            //child.LeftChild = node;
            //Root = child;
            //node.RightChild = null;
            //Root.SetRoot();
        }
示例#2
0
        public void Add(T value)
        {
            //if there is no root, insert the BSTNode as the root
            if (Root == null)
            {
                Root = new AVLnode <T>(value);
                return;
            }

            //if there is a root
            if (Root != null)
            {
                Root.SetRoot();
                AVLnode <T>          current = Root;
                Stack <AVLnode <T> > stack   = new Stack <AVLnode <T> >();

                while (current != null)
                {
                    stack.Push(current);

                    //if the new bstNodes value is less than the root's value-> the new bstnode would be on left of root
                    if (current.Value.CompareTo(value) > 0)
                    {
                        if (current.LeftChild == null)
                        {
                            current.LeftChild = new AVLnode <T>(value);
                            break;
                        }

                        current = current.LeftChild;
                    }
                    //if the new bstNodes value is greater than that of the root-> the new bstnode would be on the right of root
                    else if (current.Value.CompareTo(value) <= 0)
                    {
                        if (current.RightChild == null)
                        {
                            current.RightChild = new AVLnode <T>(value);
                            break;
                        }

                        current = current.RightChild;
                    }
                }

                while (stack.IsEmpty == false)
                {
                    Fix(stack.Pop());
                }
            }
        }
示例#3
0
        //tree is left heavy
        public void RotateRight(AVLnode <T> node)
        {
            AVLnode <T> child = node.LeftChild;

            node.LeftChild = child.LeftChild;
            if (node == Root)
            {
                Root = child;
                Root.SetRoot();
            }
            else
            {
                child.Parent           = node.Parent;
                node.Parent.RightChild = child;
                child.RightChild       = node;
                node.Parent            = child;
            }
            if (child != null)
            {
                child.RightChild = node;
            }
        }