namespace HelloDotNetGuide.Common Algorithms
{
public class Binary Search Tree Algorithm
{
public static void BinarySearchTreeRun()
{
var bst = new BinarySearchTree();
// Insert some values into the tree
(50);
(30);
(20);
(40);
(70);
(60);
(80);
(750);
("Medium-order traversal (prints ordered array):");;
();
("\n");
// Finding certain values
("Search for 40: " + (40)); // Output: True
("Search for 25: " + (25)); // Output: False
("\n");
// Delete a value
(50);
("After deleting 50:").
();
}
}
/// <summary>
/// Define the node structure of a binary search tree.
/// </summary>
public class TreeNode
{
public int Value;
public TreeNode Left;
public TreeNode Right;
public TreeNode(int value)
{
Value = value;
Left = null;
Right = null;
}
}
/// <summary>
/// Define the binary search tree class
/// </summary>
public class BinarySearchTree
{
private TreeNode root;
public BinarySearchTree()
{
root = null;
}
#region Insert node
/// <summary>
/// Insert the new value into the binary search tree.
/// </summary>
/// <param name="value">value</param>
public void Insert(int value)
{
if (root == null)
{
root = new TreeNode(value);
}
else
{
InsertRec(root, value);
}
}
private void InsertRec(TreeNode node, int value)
{
if (value < )
{
if ( == null)
{
= new TreeNode(value);
}
else
{
InsertRec(, value);
}
}
else if (value > )
{
if ( == null)
{
= new TreeNode(value);
}
else
{
InsertRec(, value);
}
}
else
{
//value already exists in the tree, no more insertions
return;
}
}
#endregion
#region Find node
/// <summary>
/// Finds if a value exists in a binary search tree.
/// </summary>
/// <param name="value">value</param>
/// <returns></returns>
public bool Search(int value)
{
return SearchRec(root, value);
}
private bool SearchRec(TreeNode node, int value)
{
// If the current node is empty, the target value was not found
if (node == null)
{
return false;
}
// return true if the target value is found
if ( == value)
{
return true;
}
// Recursively find left or right subtree
if (value < )
{
return SearchRec(, value);
}
else
{
return SearchRec(, value);
}
}
#endregion
#region Middle-order traversal
/// <summary>
/// Intermediate order traversal (prints an ordered array)
/// </summary>
public void InorderTraversal()
{
InorderTraversalRec(root);
}
private void InorderTraversalRec(TreeNode root)
{
if (root != null)
{
InorderTraversalRec();
();
InorderTraversalRec();
}
}
#endregion
#region Delete node
/// <summary>
/// Delete a value
/// </summary>
/// <param name="val">val</param>
public void Delete(int val)
{
root = DeleteNode(root, val);
}
private TreeNode DeleteNode(TreeNode node, int val)
{
if (node == null)
{
return null;
}
if (val < )
{
= DeleteNode(, val);
}
else if (val > )
{
= DeleteNode(, val);
}
else
{
// The node has two children
if ( != null && != null)
{
// Replace the current node with the smallest node in the right subtree
TreeNode minNode = FindMin();
= ;
= DeleteNode(, );
}
// The node has one child or no children.
else
{
TreeNode? temp = != null ? : ;
node = temp;
}
}
return node;
}
/// <summary>
/// Find the smallest node in the tree
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private TreeNode FindMin(TreeNode node)
{
while ( != null)
{
node = ;
}
return node;
}
#endregion
}
}
{
public class Binary Search Tree Algorithm
{
public static void BinarySearchTreeRun()
{
var bst = new BinarySearchTree();
// Insert some values into the tree
(50);
(30);
(20);
(40);
(70);
(60);
(80);
(750);
("Medium-order traversal (prints ordered array):");;
();
("\n");
// Finding certain values
("Search for 40: " + (40)); // Output: True
("Search for 25: " + (25)); // Output: False
("\n");
// Delete a value
(50);
("After deleting 50:").
();
}
}
/// <summary>
/// Define the node structure of a binary search tree.
/// </summary>
public class TreeNode
{
public int Value;
public TreeNode Left;
public TreeNode Right;
public TreeNode(int value)
{
Value = value;
Left = null;
Right = null;
}
}
/// <summary>
/// Define the binary search tree class
/// </summary>
public class BinarySearchTree
{
private TreeNode root;
public BinarySearchTree()
{
root = null;
}
#region Insert node
/// <summary>
/// Insert the new value into the binary search tree.
/// </summary>
/// <param name="value">value</param>
public void Insert(int value)
{
if (root == null)
{
root = new TreeNode(value);
}
else
{
InsertRec(root, value);
}
}
private void InsertRec(TreeNode node, int value)
{
if (value < )
{
if ( == null)
{
= new TreeNode(value);
}
else
{
InsertRec(, value);
}
}
else if (value > )
{
if ( == null)
{
= new TreeNode(value);
}
else
{
InsertRec(, value);
}
}
else
{
//value already exists in the tree, no more insertions
return;
}
}
#endregion
#region Find node
/// <summary>
/// Finds if a value exists in a binary search tree.
/// </summary>
/// <param name="value">value</param>
/// <returns></returns>
public bool Search(int value)
{
return SearchRec(root, value);
}
private bool SearchRec(TreeNode node, int value)
{
// If the current node is empty, the target value was not found
if (node == null)
{
return false;
}
// return true if the target value is found
if ( == value)
{
return true;
}
// Recursively find left or right subtree
if (value < )
{
return SearchRec(, value);
}
else
{
return SearchRec(, value);
}
}
#endregion
#region Middle-order traversal
/// <summary>
/// Intermediate order traversal (prints an ordered array)
/// </summary>
public void InorderTraversal()
{
InorderTraversalRec(root);
}
private void InorderTraversalRec(TreeNode root)
{
if (root != null)
{
InorderTraversalRec();
();
InorderTraversalRec();
}
}
#endregion
#region Delete node
/// <summary>
/// Delete a value
/// </summary>
/// <param name="val">val</param>
public void Delete(int val)
{
root = DeleteNode(root, val);
}
private TreeNode DeleteNode(TreeNode node, int val)
{
if (node == null)
{
return null;
}
if (val < )
{
= DeleteNode(, val);
}
else if (val > )
{
= DeleteNode(, val);
}
else
{
// The node has two children
if ( != null && != null)
{
// Replace the current node with the smallest node in the right subtree
TreeNode minNode = FindMin();
= ;
= DeleteNode(, );
}
// The node has one child or no children.
else
{
TreeNode? temp = != null ? : ;
node = temp;
}
}
return node;
}
/// <summary>
/// Find the smallest node in the tree
/// </summary>
/// <param name="node"></param>
/// <returns></returns>
private TreeNode FindMin(TreeNode node)
{
while ( != null)
{
node = ;
}
return node;
}
#endregion
}
}