Exemple #1
0
 /// <summary>
 /// Deletes all nodes from tree
 /// </summary>
 public void Clear()
 {
     BinaryTreeRecursiveHelper <T> .TraversalPostOrder(Root, node =>
     {
         Delete(node.Value);
     });
 }
Exemple #2
0
        /// <summary>
        /// Converts binary tree to its array representation.
        /// In this representation, for node with index i its children can be found at indices
        /// 2 * i + 1 (left child) and 2 * i + 2 (right child), while its parent - at index (i - 1) / 2.
        /// The result array size is equal to 2^(h + 1) - 1, where h is tree height
        /// </summary>
        /// <returns>Node values array</returns>
        public T[] ToArray()
        {
            if (CountNodes == 0)
            {
                return(Array.Empty <T>());
            }

            var array = new T[Convert.ToInt32(Math.Pow(2, Height + 1) - 1)];

            BinaryTreeRecursiveHelper <T> .ToArray(Root, array, 0);

            return(array);
        }
Exemple #3
0
 /// <summary>
 /// Visits all tree nodes in post order and performs specified action for each node
 /// </summary>
 /// <param name="action">Action to perform</param>
 public void TraversalPostOrder(Action <BinaryTreeNode <T> > action)
 {
     BinaryTreeRecursiveHelper <T> .TraversalPostOrder(Root, action);
 }
Exemple #4
0
 /// <summary>
 /// Visits all tree nodes in reverse order (from max to min node) and performs specified action for each node
 /// </summary>
 /// <param name="action">Action to perform</param>
 public void TraversalInOrderReverse(Action <BinaryTreeNode <T> > action)
 {
     BinaryTreeRecursiveHelper <T> .TraversalInOrderReverse(Root, action);
 }