This article is purely entertaining. It originated from code sorting and found that binary trees have been traversed.
Although various collection classes in. NET/C # have achieved Optimal Sorting design, understanding the basic algorithm implementation helps to balance and choose among various types of software development.
For example, if you have sorted and searched the B + tree and serialized the tree node to the binary file block, you should have understood the basic design of various database indexes.
What is a binary tree?
Http://en.wikipedia.org/wiki/Binary_tree
Binary Tree node class definition
Copy codeThe Code is as follows: View Code
/// <Summary>
/// Binary Tree node
/// </Summary>
/// <Typeparam name = "T"> The item type </typeparam>
Public class BinaryTreeNode <T>
{
# Region Constructors
/// <Summary>
/// Binary Tree node
/// </Summary>
Public BinaryTreeNode ()
{
}
/// <Summary>
/// Binary Tree node
/// </Summary>
/// <Param name = "value"> value in the node </param>
Public BinaryTreeNode (T value)
{
This. Value = value;
}
/// <Summary>
/// Binary Tree node
/// </Summary>
/// <Param name = "value"> value in the node </param>
/// <Param name = "parent"> parent node of the node </param>
Public BinaryTreeNode (T value, BinaryTreeNode <T> parent)
{
This. Value = value;
This. Parent = parent;
}
/// <Summary>
/// Binary Tree node
/// </Summary>
/// <Param name = "value"> value in the node </param>
/// <Param name = "parent"> parent node of the node </param>
/// <Param name = "left"> left node of the node </param>
/// <Param name = "right"> right node of the node </param>
Public BinaryTreeNode (T value,
BinaryTreeNode <T> parent,
BinaryTreeNode <T> left,
BinaryTreeNode <T> right)
{
This. Value = value;
This. Right = right;
This. Left = left;
This. Parent = parent;
}
# Endregion
# Region Properties
/// <Summary>
/// Node Value
/// </Summary>
Public T Value {get; set ;}
/// <Summary>
/// Parent node
/// </Summary>
Public BinaryTreeNode <T> Parent {get; set ;}
/// <Summary>
/// Left Node
/// </Summary>
Public BinaryTreeNode <T> Left {get; set ;}
/// <Summary>
/// Right Node
/// </Summary>
Public BinaryTreeNode <T> Right {get; set ;}
/// <Summary>
/// Whether it is the root node
/// </Summary>
Public bool IsRoot {get {return Parent = null ;}}
/// <Summary>
/// Whether it is a leaf node
/// </Summary>
Public bool IsLeaf {get {return Left = null & Right = null ;}}
/// <Summary>
/// Whether it is accessible
/// </Summary>
Internal bool Visited {get; set ;}
# Endregion
# Region Public Overridden Functions
/// <Summary>
/// Returns a <see cref = "System. String"/> that represents this instance.
/// </Summary>
/// <Returns>
/// A <see cref = "System. String"/> that represents this instance.
/// </Returns>
Public override string ToString ()
{
Return Value. ToString ();
}
# Endregion
}
Binary Tree implementationCopy codeThe Code is as follows: View Code
/// <Summary>
/// Binary Tree
/// </Summary>
/// <Typeparam name = "T"> node content type in a binary tree </typeparam>
[SuppressMessage ("Microsoft. Naming", "CA1710: IdentifiersShouldHaveCorrectSuffix")]
Public class BinaryTree <T>: ICollection <T>, IEnumerable <T> where T: IComparable <T>
{
# Region Constructor
/// <Summary>
/// Binary Tree
/// </Summary>
Public BinaryTree ()
{
NumberOfNodes = 0;
}
/// <Summary>
/// Binary Tree
/// </Summary>
/// <Param name = "root"> binary tree root node </param>
Public BinaryTree (BinaryTreeNode <T> root)
: This ()
{
This. Root = root;
}
# Endregion
# Region Properties
/// <Summary>
/// Root node of the tree
/// </Summary>
Public BinaryTreeNode <T> Root {get; set ;}
/// <Summary>
/// Number of nodes in the tree
/// </Summary>
Protected int NumberOfNodes {get; set ;}
/// <Summary>
/// Whether the tree is empty
/// </Summary>
Public bool IsEmpty {get {return Root = null ;}}
/// <Summary>
/// Obtain the minimum value of a node in the tree
/// </Summary>
Public T MinValue
{
Get
{
If (IsEmpty)
Return default (T );
BinaryTreeNode <T> minNode = Root;
While (minNode. Left! = Null)
MinNode = minNode. Left;
Return minNode. Value;
}
}
/// <Summary>
/// Obtain the maximum value of a node in the tree
/// </Summary>
Public T MaxValue
{
Get
{
If (IsEmpty)
Return default (T );
BinaryTreeNode <T> maxNode = Root;
While (maxNode. Right! = Null)
MaxNode = maxNode. Right;
Return maxNode. Value;
}
}
# Endregion
# Region IEnumerable <T> Members
/// <Summary>
/// Returns an enumerator that iterates through the collection.
/// </Summary>
/// <Returns>
/// A <see cref = "T: System. Collections. Generic. IEnumerator '1"> </see>
/// That can be used to iterate through the collection.
/// </Returns>
Public IEnumerator <T> GetEnumerator ()
{
Foreach (BinaryTreeNode <T> node in Traverse (Root ))
{
Yield return node. Value;
}
}
# Endregion
# Region IEnumerable Members
/// <Summary>
/// Returns an enumerator that iterates through a collection.
/// </Summary>
/// <Returns>
/// An <see cref = "T: System. Collections. IEnumerator"/>
/// Object that can be used to iterate through the collection.
/// </Returns>
IEnumerator IEnumerable. GetEnumerator ()
{
Foreach (BinaryTreeNode <T> node in Traverse (Root ))
{
Yield return node. Value;
}
}
# Endregion
# Region ICollection <T> Members
/// <Summary>
/// Add a node
/// </Summary>
/// <Param name = "item"> The object to add to
/// <See cref = "T: System. Collections. Generic. ICollection '1"> </see>. </param>
/// <Exception cref = "T: System. NotSupportedException">
/// <See cref = "T: System. Collections. Generic. ICollection '1"> </see>
/// Is read-only. </exception>
Public void Add (T item)
{
If (Root = null)
{
Root = new BinaryTreeNode <T> (item );
++ NumberOfNodes;
}
Else
{
Insert (item );
}
}
/// <Summary>
/// Clear the tree
/// </Summary>
Public void Clear ()
{
Root = null;
}
/// <Summary>
/// Whether the node is included in the tree
/// </Summary>
/// <Param name = "item"> The object to locate in
/// <See cref = "T: System. Collections. Generic. ICollection '1"> </see>. </param>
/// <Returns>
/// True if item is found in
/// <See cref = "T: System. Collections. Generic. ICollection '1"> </see>; otherwise, false.
/// </Returns>
Public bool Contains (T item)
{
If (IsEmpty)
Return false;
BinaryTreeNode <T> currentNode = Root;
While (currentNode! = Null)
{
Int comparedValue = currentNode. Value. CompareTo (item );
If (comparedValue = 0)
Return true;
Else if (comparedValue <0)
CurrentNode = currentNode. Left;
Else
CurrentNode = currentNode. Right;
}
Return false;
}
/// <Summary>
/// Copy the node in the tree to the target array
/// </Summary>
/// <Param name = "array"> The array. </param>
/// <Param name = "arrayIndex"> Index of the array. </param>
Public void CopyTo (T [] array, int arrayIndex)
{
T [] tempArray = new T [NumberOfNodes];
Int counter = 0;
Foreach (T value in this)
{
TempArray [counter] = value;
++ Counter;
}
Array. Copy (tempArray, 0, array, arrayIndex, Count );
}
/// <Summary>
/// Number of nodes in the tree
/// </Summary>
Public int Count
{
Get {return NumberOfNodes ;}
}
/// <Summary>
/// Whether the tree is read-only
/// </Summary>
Public bool IsReadOnly
{
Get {return false ;}
}
/// <Summary>
/// Remove a node
/// </Summary>
/// <Param name = "item"> node value </param>
/// <Returns> whether the removal is successful </returns>
Public bool Remove (T item)
{
BinaryTreeNode <T> node = Find (item );
If (node = null)
Return false;
List <T> values = new List <T> ();
Foreach (BinaryTreeNode <T> l in Traverse (node. Left ))
{
Values. Add (l. Value );
}
Foreach (BinaryTreeNode <T> r in Traverse (node. Right ))
{
Values. Add (r. Value );
}
If (node. Parent. Left = node)
{
Node. Parent. Left = null;
}
Else
{
Node. Parent. Right = null;
}
Node. Parent = null;
Foreach (T v in values)
{
This. Add (v );
}
Return true;
}
# Endregion
# Region Private Functions
/// <Summary>
/// Find the node with the specified value
/// </Summary>
/// <Param name = "value"> specified value </param>
/// <Returns>
/// Specify the value of the node
/// </Returns>
Protected BinaryTreeNode <T> Find (T value)
{
Foreach (BinaryTreeNode <T> node in Traverse (Root ))
If (node. Value. Equals (value ))
Return node;
Return null;
}
/// <Summary>
/// Traverse the tree
/// </Summary>
/// <Param name = "node"> traverse the Start node of the search </param>
/// <Returns>
/// The individual items from the tree
/// </Returns>
[SuppressMessage ("Microsoft. Design", "CA1006: DoNotNestGenericTypesInMemberSignatures")]
Protected IEnumerable <BinaryTreeNode <T> Traverse (BinaryTreeNode <T> node)
{
// Traverse the left subtree
If (node. Left! = Null)
{
Foreach (BinaryTreeNode <T> left in Traverse (node. Left ))
Yield return left;
}
// Traverse the binary tree in the middle order. The order is the left subtree, root, and right subtree.
Yield return node;
// Traverse the right subtree
If (node. Right! = Null)
{
Foreach (BinaryTreeNode <T> right in Traverse (node. Right ))
Yield return right;
}
}
/// <Summary>
/// Insert a node
/// </Summary>
/// <Param name = "value"> inserted node value </param>
Protected void Insert (T value)
{
// Start from the root node
BinaryTreeNode <T> currentNode = Root;
While (true)
{
If (currentNode = null)
Throw new InvalidProgramException ("The current tree node cannot be null .");
// Compare the values of the current node and the new node
Int comparedValue = currentNode. Value. CompareTo (value );
If (comparedValue <0)
{
// The current node value is smaller than the new node Value
If (currentNode. Left = null)
{
CurrentNode. Left = new BinaryTreeNode <T> (value, currentNode );
++ NumberOfNodes;
Return;
}
Else
{
CurrentNode = currentNode. Left;
}
}
Else if (comparedValue> 0)
{
// The current node value is greater than the new node Value
If (currentNode. Right = null)
{
CurrentNode. Right = new BinaryTreeNode <T> (value, currentNode );
++ NumberOfNodes;
Return;
}
Else
{
CurrentNode = currentNode. Right;
}
}
Else
{
// The current node value is equal to the new node Value
CurrentNode = currentNode. Right;
}
}
}
# Endregion
}
ExampleCopy codeThe Code is as follows: class Program
{
Static void Main (string [] args)
{
Console. ForegroundColor = ConsoleColor. Green;
BinaryTree <string> tree = new BinaryTree <string> ();
Tree. Add ("Dennis ");
Tree. Add ("Gao ");
Tree. Add ("Is ");
Tree. Add ("");
Tree. Add ("C #");
Tree. Add ("Programmer ");
Console. WriteLine ("Root Node Is:" + tree. Root. ToString ());
Console. WriteLine ();
Foreach (var node in tree)
{
Console. WriteLine (node );
}
Console. ReadKey ();
}
}
Traversing a binary tree in ascending order