Exemplo n.º 1
0
 private int GetTreeDepth(BinaryTreeNode <T> parent)
 {
     return(parent == null ? 0 : Math.Max(GetTreeDepth(parent.Left), GetTreeDepth(parent.Right)) + 1);
 }
Exemplo n.º 2
0
        /// <summary>
        /// Find the predecessor and successor values in the tree.
        /// </summary>
        /// <param name="data">The </param>
        /// <param name="predecessor"></param>
        /// <param name="successor"></param>
        public void FindPredecessorSuccessor(T data, out BinaryTreeNode <T> predecessor, out BinaryTreeNode <T> successor)
        {
            var root = this.Root;

            predecessor = null;
            successor   = null;

            while (root != null)
            {
                if (root.Data.Equals(data))
                {
                    if (root.Right != null)
                    {
                        successor = root.Right;
                        while (successor.Left != null)
                        {
                            successor = successor.Left;
                        }
                    }
                    if (root.Left != null)
                    {
                        predecessor = root.Left;
                        while (predecessor.Right != null)
                        {
                            predecessor = predecessor.Right;
                        }
                    }
                    return;
                }
                else
                {
                    var compare = _comparer.Compare(root.Data, data);
                    if (compare < 0)
                    {
                        predecessor = root;
                        root        = root.Right;
                    }
                    else
                    {
                        successor = root;
                        root      = root.Left;
                    }
                }
            }
        }
Exemplo n.º 3
0
 /// <summary>
 /// Remove a value from the tree.
 /// </summary>
 /// <param name="value">The value to remove.</param>
 public void Remove(T value)
 {
     this.Root = Remove(this.Root, value);
 }