Пример #1
0
        /// <summary>
        /// Fix tree balance after insert operation
        /// </summary>
        /// <param name="node"></param>
        /// <param name="balance"></param>
        private void InsertBalance(AvlNode <T> node, Int32 balance)
        {
            while (node != null)
            {
                balance = (node.Balance += balance);

                if (balance == 0)
                {
                    return;
                }
                else if (balance == 2)
                {
                    if (node.Left.Balance == 1)
                    {
                        this.RotateRight(node);
                    }
                    else
                    {
                        this.RotateLeftRight(node);
                    }

                    return;
                }
                else if (balance == -2)
                {
                    if (node.Right.Balance == -1)
                    {
                        this.RotateLeft(node);
                    }
                    else
                    {
                        this.RotateRightLeft(node);
                    }

                    return;
                }

                AvlNode <T> parent = node.Parent;

                if (parent != null)
                {
                    balance = parent.Left == node ? 1 : -1;
                }

                node = parent;
            }
        }
Пример #2
0
        private static void Replace(AvlNode <T> target, AvlNode <T> source)
        {
            AvlNode <T> left  = source.Left;
            AvlNode <T> right = source.Right;

            target.Balance = source.Balance;
            target.Info    = source.Info;
            target.Left    = left;
            target.Right   = right;

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

            if (right != null)
            {
                right.Parent = target;
            }
        }
Пример #3
0
        public Boolean MoveNext()
        {
            switch (_action)
            {
            case Action.Right:
                current = right;

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

                right   = current.Right;
                _action = right != null ? Action.Right : Action.Parent;

                return(true);

            case Action.Parent:
                while (current.Parent != null)
                {
                    AvlNode <T> previous = current;

                    current = current.Parent;

                    if (current.Left == previous)
                    {
                        right   = current.Right;
                        _action = right != null ? Action.Right : Action.Parent;

                        return(true);
                    }
                }

                _action = Action.End;

                return(false);

            default:
                return(false);
            }
        }
Пример #4
0
 public void Reset()
 {
     right   = root;
     _action = root == null ? Action.End : Action.Right;
 }
Пример #5
0
 public AvlNodeEnumerator(AvlNode <T> root)
 {
     right   = this.root = root;
     _action = this.root == null ? Action.End : Action.Right;
 }
Пример #6
0
 /// <summary>
 /// Removes all nodes from the tree
 /// </summary>
 public void Clear()
 {
     this.root = null;
     Count     = 0;
 }
Пример #7
0
        /// <summary>
        /// Insert the given node in the tree
        /// </summary>
        /// <param name="info"></param>
        public void Insert(T info)
        {
            if (this.root == null)
            {
                this.root = new AvlNode <T>
                {
                    Info = info
                };
            }
            else
            {
                AvlNode <T> node = this.root;

                while (node != null)
                {
                    Int32 compare = info.CompareTo(node.Info);

                    if (compare < 0)
                    {
                        AvlNode <T> left = node.Left;

                        if (left == null)
                        {
                            node.Left = new AvlNode <T> {
                                Info = info, Parent = node
                            };

                            this.InsertBalance(node, 1);

                            return;
                        }
                        else
                        {
                            node = left;
                        }
                    }
                    else if (compare > 0)
                    {
                        AvlNode <T> right = node.Right;

                        if (right == null)
                        {
                            node.Right = new AvlNode <T> {
                                Info = info, Parent = node
                            };

                            this.InsertBalance(node, -1);

                            return;
                        }
                        else
                        {
                            node = right;
                        }
                    }
                    else
                    {
                        node.Info = info;

                        return;
                    }
                }
            }
            Count++;
        }
Пример #8
0
        /// <summary>
        /// Return false if the tree dosn't contain a node with given info.
        /// Otherwise delete the node and return true.
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public Boolean Delete(T info)
        {
            AvlNode <T> node = this.root;

            while (node != null)
            {
                Int32 compare = info.CompareTo(node.Info);

                if (compare < 0)
                {
                    node = node.Left;
                }
                else if (compare > 0)
                {
                    node = node.Right;
                }
                else
                {
                    AvlNode <T> left  = node.Left;
                    AvlNode <T> right = node.Right;

                    if (left == null)
                    {
                        if (right == null)
                        {
                            if (node == root)
                            {
                                this.root = null;
                            }
                            else
                            {
                                AvlNode <T> parent = node.Parent;

                                if (parent.Left == node)
                                {
                                    parent.Left = null;

                                    this.DeleteBalance(parent, -1);
                                }
                                else
                                {
                                    parent.Right = null;

                                    this.DeleteBalance(parent, 1);
                                }
                            }
                        }
                        else
                        {
                            Replace(node, right);

                            this.DeleteBalance(node, 0);
                        }
                    }
                    else if (right == null)
                    {
                        Replace(node, left);

                        this.DeleteBalance(node, 0);
                    }
                    else
                    {
                        AvlNode <T> successor = right;

                        if (successor.Left == null)
                        {
                            AvlNode <T> parent = node.Parent;

                            successor.Parent  = parent;
                            successor.Left    = left;
                            successor.Balance = node.Balance;

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

                            if (node == root)
                            {
                                this.root = successor;
                            }
                            else
                            {
                                if (parent.Left == node)
                                {
                                    parent.Left = successor;
                                }
                                else
                                {
                                    parent.Right = successor;
                                }
                            }

                            this.DeleteBalance(successor, 1);
                        }
                        else
                        {
                            while (successor.Left != null)
                            {
                                successor = successor.Left;
                            }

                            AvlNode <T> parent          = node.Parent;
                            AvlNode <T> successorParent = successor.Parent;
                            AvlNode <T> successorRight  = successor.Right;

                            if (successorParent.Left == successor)
                            {
                                successorParent.Left = successorRight;
                            }
                            else
                            {
                                successorParent.Right = successorRight;
                            }

                            if (successorRight != null)
                            {
                                successorRight.Parent = successorParent;
                            }

                            successor.Parent  = parent;
                            successor.Left    = left;
                            successor.Balance = node.Balance;
                            successor.Right   = right;
                            right.Parent      = successor;

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

                            if (node == root)
                            {
                                root = successor;
                            }
                            else
                            {
                                if (parent.Left == node)
                                {
                                    parent.Left = successor;
                                }
                                else
                                {
                                    parent.Right = successor;
                                }
                            }

                            this.DeleteBalance(successorParent, -1);
                        }
                    }
                    Count--;
                    return(true);
                }
            }

            return(false);
        }