/// <summary>
 /// Traverses tree in inverse-post-order manner.
 /// </summary>
 /// <param name="action">An action to take while traversing the tree.</param>
 /// <param name="parent">Parent in which traversal will be started from.</param>
 /// <exception cref="InvalidOperationException">Binary Search Tree is empty.</exception>
 /// <exception cref="ArgumentNullException">Action cannot be null.</exception>
 /// <exception cref="Exception">'Parent' doesn't exist in tree.</exception>
 public void InversePostOrderTraversal(Action <T> action, BinarySearchTreeNode <T> parent = null)
 {
     if (_count == 0)
     {
         throw new InvalidOperationException("Binary Search Tree is empty!");
     }
     if (action == null)
     {
         throw new ArgumentNullException(nameof(action), "Action cannot be null");
     }
     if (parent != null && !Contains(parent.Value))
     {
         throw new Exception(string.Format("Node with value: {0} doesn't exist!", parent.Value));
     }
     if (parent == null)
     {
         parent = _root;
     }
     InversePostOrderTraversal(parent, action);
 }
コード例 #2
0
 public BinarySearchTreeNode(T val, BinarySearchTreeNode <T> parent, BinarySearchTreeNode <T> left, BinarySearchTreeNode <T> right)
 {
     this.Value      = val;
     this.LeftChild  = left;
     this.RightChild = right;
     this.Parent     = parent;
 }
 /// <summary>
 /// Clears all nodes in tree.
 /// </summary>
 public void Clear()
 {
     _count = 0;
     _root  = null;
 }
        /// <summary>
        /// Removes a node by specifying its value.
        /// </summary>
        /// <param name="value">Any generic type that implements IComparable of T interface.</param>
        /// <returns>True if deleted. Otherwise false.</returns>
        /// <exception cref="InvalidOperationException">Binary Search Tree is empty.</exception>
        public bool Remove(T value)
        {
            if (_count == 0)
            {
                throw new InvalidOperationException("Binary Search Tree is empty!");
            }
            BinarySearchTreeNode <T> parent = null;
            BinarySearchTreeNode <T> node   = FindNodeAndParent(value, out parent);

            if (node == null)
            {
                return(false);
            }
            //decrement first!
            _count--;

            if (node.Right == null)
            {
                //it's a root!!
                if (parent == null)
                {
                    _root = node.Left;
                }
                else
                {
                    //it's left
                    if (parent.CompareTo(value) > 0)
                    {
                        parent.Left = node.Left;
                    }
                    else
                    {
                        parent.Right = node.Left;
                    }
                }
            }
            else if (node.Right.Left == null)
            {
                node.Right.Left = node.Left;
                if (parent == null)
                {
                    _root = node.Right;
                }
                else
                {
                    if (parent.CompareTo(value) > 0)
                    {
                        parent.Left = node.Right;
                    }
                    else
                    {
                        parent.Right = node.Right;
                    }
                }
            }
            else
            {
                BinarySearchTreeNode <T> parentLeftEnd = node.Right;
                BinarySearchTreeNode <T> leftEnd       = node.Right.Left;

                while (leftEnd.Left != null)
                {
                    parentLeftEnd = leftEnd;
                    leftEnd       = leftEnd.Left;
                }

                parentLeftEnd.Left = leftEnd.Right;
                leftEnd.Left       = node.Left;
                leftEnd.Right      = node.Right;

                if (parent == null)
                {
                    _root = leftEnd;
                }
                else
                {
                    if (parent.CompareTo(value) > 0)
                    {
                        parent.Left = leftEnd;
                    }
                    else
                    {
                        parent.Right = leftEnd;
                    }
                }
            }
            node = null;
            return(true);
        }