protected int MaxChildHeight(AvlTreeNode <TNode> node)
        {
            if (node != null)
            {
                return(1 + Math.Max(MaxChildHeight(node.Left), MaxChildHeight(node.Right)));
            }

            return(0);
        }
        protected void RightRotation()
        {
            AvlTreeNode <TNode> newRoot = this.Left;

            ReplaceRoot(newRoot);

            this.Left = newRoot.Right;

            newRoot.Right = this;
        }
Exemple #3
0
        public void AddWithoutBalance(T value)
        {
            if (Head == null)
            {
                Head = new AvlTreeNode <T>(value, null, this);
            }
            else
            {
                _add(value, false);
            }

            Count++;
        }
Exemple #4
0
        public override sealed void Add(T value)
        {
            if (Head == null)
            {
                Head = new AvlTreeNode <T>(value, null, this);
            }
            else
            {
                _add(value, true);
            }

            Count++;
        }
Exemple #5
0
        protected override IEnumerable <T> PostOrderTraversal()
        {
            if (Head != null)
            {
                Stack <AvlTreeNode <T> > stack   = new Stack <AvlTreeNode <T> >();
                AvlTreeNode <T>          current = Head;

                bool goLeftNext  = true;
                bool goRightNext = true;

                stack.Push(current);

                while (stack.Count > 0)
                {
                    if (goLeftNext)
                    {
                        while (current.Left != null)
                        {
                            stack.Push(current);
                            current = current.Left;
                        }
                    }

                    if (current.Right != null && goRightNext)
                    {
                        stack.Push(current);

                        current = current.Right;

                        goLeftNext = true;
                    }
                    else
                    {
                        yield return(current.Value);

                        if (current.Parent != null && current.Parent.Right == current)
                        {
                            goRightNext = false;
                        }
                        else
                        {
                            goRightNext = true;
                        }

                        current = stack.Pop();

                        goLeftNext = false;
                    }
                }
            }
        }
        protected void ReplaceRoot(AvlTreeNode <TNode> newRoot)
        {
            if (this.Parent != null)
            {
                if (this.Parent.Right == this)
                {
                    this.Parent.Right = newRoot;
                }
                else if (this.Parent.Left == this)
                {
                    this.Parent.Left = newRoot;
                }
            }
            else
            {
                this.Tree.Head = newRoot;
            }

            newRoot.Parent = this.Parent;
            this.Parent    = newRoot;
        }
Exemple #7
0
        protected void _add(T value, bool balance)
        {
            AvlTreeNode <T> current = Head;

            while (true)
            {
                if (value.CompareTo(current.Value) < 0)
                {
                    if (current.Left == null)
                    {
                        current.Left = new AvlTreeNode <T>(value, current, this);
                        break;
                    }

                    current = current.Left;
                }
                else
                {
                    if (current.Right == null)
                    {
                        current.Right = new AvlTreeNode <T>(value, current, this);
                        break;
                    }

                    current = current.Right;
                }
            }

            if (balance)
            {
                while (current != null)
                {
                    current.Balance();
                    current = current.Parent;
                }
            }
        }
Exemple #8
0
        protected AvlTreeNode <T> Find(T value)
        {
            AvlTreeNode <T> current = Head;

            while (current != null)
            {
                int compareResult = current.CompareTo(value);

                if (compareResult > 0)
                {
                    current = current.Left;
                }
                else if (compareResult < 0)
                {
                    current = current.Right;
                }
                else
                {
                    break;
                }
            }

            return(current);
        }
 public AvlTreeNode(TNode value, AvlTreeNode <TNode> parent, AvlTree <TNode> tree)
     : base(value)
 {
     Parent = parent;
     Tree   = tree;
 }
Exemple #10
0
 public override sealed void Clear()
 {
     Head  = null;
     Count = 0;
 }
Exemple #11
0
        protected bool _remove(T value, bool balance)
        {
            AvlTreeNode <T> current = Find(value);

            if (current == null)
            {
                return(false);
            }

            AvlTreeNode <T> treeToBalance = current.Parent;

            Count--;

            if (current.Right == null)
            {
                if (current.Parent == null)
                {
                    Head = current.Left;

                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                }
                else
                {
                    int compareResult = current.Parent.CompareNode(current);

                    if (compareResult > 0)
                    {
                        current.Parent.Left = current.Left;
                    }
                    else if (compareResult < 0)
                    {
                        current.Parent.Right = current.Left;
                    }
                }
            }
            else if (current.Right.Left == null)
            {
                current.Right.Left = current.Left;

                if (current.Parent == null)
                {
                    Head = current.Right;

                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                }
                else
                {
                    int compareResult = current.Parent.CompareNode(current);

                    if (compareResult > 0)
                    {
                        current.Parent.Left = current.Right;
                    }
                    else if (compareResult < 0)
                    {
                        current.Parent.Right = current.Right;
                    }
                }
            }
            else
            {
                AvlTreeNode <T> leftmost = current.Right.Left;

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

                leftmost.Parent.Left = leftmost.Right;

                leftmost.Left  = current.Left;
                leftmost.Right = current.Right;

                if (current.Parent == null)
                {
                    Head = leftmost;

                    if (Head != null)
                    {
                        Head.Parent = null;
                    }
                }
                else
                {
                    int compareResult = current.Parent.CompareNode(current);

                    if (compareResult > 0)
                    {
                        current.Parent.Left = leftmost;
                    }
                    else if (compareResult < 0)
                    {
                        current.Parent.Right = leftmost;
                    }
                }
            }

            if (balance)
            {
                if (treeToBalance != null)
                {
                    treeToBalance.Balance();
                }
                else
                {
                    if (Head != null)
                    {
                        Head.Balance();
                    }
                }
            }

            return(true);
        }