Concepts required to understand the essence of a set 04-Binary Search Tree, 04-Binary Search Tree
Unlike linked lists, stacks, and queues, the binary search tree is not a linear data structure, but a two-dimensional data structure. Each node contains a LeftNode and a RightNode. The Binary Search Tree places data smaller than the node data items on the LeftNode, and the data larger than the node data items on the RightNode.
Class about nodes.
public class TreeNode<T>
{
public T Element { get; set; }
public TreeNode<T> LeftNode { get; set; }
public TreeNode<T> RightNode { get; set; }
public TreeNode(T element)
{
this.Element = element;
LeftNode = RightNode = null;
}
public override string ToString()
{
string nodeString = "[" + this.Element + " ";
if (this.LeftNode == null && this.RightNode == null)
{
NodeString + = "(leaf node )";
}
if (this.LeftNode != null)
{
NodeString + = "Left node:" + this. LeftNode. ToString ();
}
if (this.RightNode != null)
{
NodeString + = "right node:" + this. RightNode. ToString ();
}
nodeString += "]";
return nodeString;
}
}
In the preceding example, the node where the data is located is smaller than the node data Element is assigned to LeftNode, And the node where the data is located is larger than the node data Element is assigned to RightNode.
Create a generic binary tree search class, maintain a root node, and provide various operation methods for nodes.
public class BinarySearchTree<T>
{
public TreeNode<T> Root { get; set; }
public BinarySearchTree()
{
this.Root = null;
}
// Insert a data entry into a binary tree
public void Insert(T x)
{
this.Root = Insert(x, this.Root);
}
// Delete a data item from the Binary Tree
public void Remove(T x)
{
this.Root = Remove(x, this.Root);
}
// Delete the smallest data item in the binary tree
public void RemoveMin()
{
this.Root = RemoveMin(this.Root);
}
// Obtain the minimum data item in the binary tree
public T FindMin()
{
return ElemntAt(FindMin(this.Root));
}
// Obtain the maximum data item in a binary tree
public T FindMax()
{
return ElemntAt(FindMax(this.Root));
}
// Obtain a data item in a binary tree
public T Find(T x)
{
return ElemntAt(Find(x, this.Root));
}
// Clear
public void MakeEmpty()
{
this.Root = null;
}
// Determine whether a binary tree is empty or not
public bool IsEmpty()
{
return this.Root == null;
}
// Obtain the data items of a node
private T ElemntAt(TreeNode<T> t)
{
return t == null ? default(T) : t.Element;
}
/// <summary>
/// Search for nodes
/// </summary>
/// <Param name = "x"> query data items </param>
/// <Param name = "t"> existing node </param>
/// <Returns> return node </returns>
private TreeNode<T> Find(T x, TreeNode<T> t)
{
While (t! = Null) // when no matching data item is found, the search range is constantly adjusted, that is, the value of t.
{
if ((x as IComparable).CompareTo(t.Element) < 0)
{
t = t.LeftNode;
}
else if ((x as IComparable).CompareTo(t.Element) > 0)
{
t = t.RightNode;
}
Else // if the data item is found, the current t value is returned.
{
return t;
}
}
return null;
}
// Obtain the smallest node,
private TreeNode<T> FindMin(TreeNode<T> t)
{
if (t != null)
{
While (t. LeftNode! = Null) // The left half edge tree of the continuously repeating Binary Tree
{
T = t. LeftNode; // continuously set the t value
}
}
return t;
}
// Obtain the largest Node
private TreeNode<T> FindMax(TreeNode<T> t)
{
if (t != null)
{
while (t.RightNode != null)
{
t = t.RightNode;
}
}
return t;
}
/// <summary>
/// Insert a node
/// </summary>
/// <Param name = "x"> data item to be inserted </param>
/// <Param name = "t"> existing node </param>
/// <Returns> returns an existing node </returns>
protected TreeNode<T> Insert(T x, TreeNode<T> t)
{
if (t == null)
{
t = new TreeNode<T>(x);
}
else if ((x as IComparable).CompareTo(t.Element) < 0)
{
// T. LeftNode on the right of the equal sign is null, so a TreeNode instance is created for t. LeftNode
t.LeftNode = Insert(x, t.LeftNode);
}
else if ((x as IComparable).CompareTo(t.Element) > 0)
{
t.RightNode = Insert(x, t.RightNode);
}
else
{
Throw new Exception ("the same element is inserted ~~ ");
}
return t;
}
// Delete the smallest Node
// Return the current Root Node
protected TreeNode<T> RemoveMin(TreeNode<T> t)
{
if (t == null)
{
Throw new Exception ("the node does not exist ~~ ");
}
else if (t.LeftNode != null)
{
// Recursively set t. LeftNode until t. LeftNode = null
t.LeftNode = RemoveMin(t.LeftNode);
return t;
}
Else // when t. LeftNode = null, t. RightNode is returned as the minimum node.
{
return t.RightNode;
}
}
// Delete a data item and return the current Root Node
protected TreeNode<T> Remove(T x, TreeNode<T> t)
{
if (t == null)
{
Throw new Exception ("the node does not exist ~~ ");
}
else if((x as IComparable).CompareTo(t.Element) < 0)
{
t.LeftNode = Remove(x, t.LeftNode);
}
else if ((x as IComparable).CompareTo(t.Element) > 0)
{
t.RightNode = Remove(x, t.RightNode);
}
else if (t.LeftNode != null && t.RightNode != null)
{
t.Element = FindMin(t.RightNode).Element;
t.RightNode = RemoveMin(t.RightNode);
}
else
{
t = (t.LeftNode != null) ? t.LeftNode : t.RightNode;
}
return t;
}
public override string ToString()
{
return this.Root.ToString();
}
}
The client creates a binary search tree instance and calls the instance method to insert random data.
BinarySearchTree<int> intTree = new BinarySearchTree<int>();
Random r = new Random(DateTime.Now.Millisecond);
string trace = "";
// Insert 5 random numbers
for (int i = 0; i < 5; i++)
{
int randomInt = r.Next(1, 500);
intTree.Insert(randomInt);
trace += randomInt + " ";
}
Console. WriteLine ("Maximum node:" + intTree. FindMax ());
Console. WriteLine ("minimum node:" + intTree. FindMin ());
Console. WriteLine ("Root node:" + intTree. Root. Element );
Console. WriteLine ("the sequence of inserted nodes is:" + trace );
Console. WriteLine ("Print tree:" + intTree );
Console.ReadKey();
References:
Binary Search Trees (BSTs) in C #
The series "concepts that must be known to understand the essence of a set" includes:
Concepts that must be known to understand the essence of a set 01-concepts that must be known to understand the essence of a set 02-concepts that must be known to understand the essence of a set 03-what must be known to understand the essence of a set in a queue concept 04-Binary Search Tree
What is the difference between a binary search tree and a binary search tree? Tutorial
What is the difference between a binary search tree and a binary search tree?
In terms of average time performance, the query on the binary sorting tree is similar to that on the binary query tree.
In terms of table order maintenance, the binary sorting tree does not need to move nodes. You only need to modify the pointer to complete the insert and delete operations, and its average execution time is O (lgn), which is more effective. Binary Search involves an ordered table as a vector. If a node is inserted or deleted, O (n) is used to maintain the order of the table ). When an ordered table is a static query table, vector should be used as its storage structure, and binary search should be used for the query operation. If the ordered table is dynamically searched, the binary sorting tree should be selected as its storage structure.
Is the Binary Decision tree for semi-query a Complete Binary Tree? Why?
Not necessarily, The Complete Binary Tree is the same as the serial number of the full binary tree. The binary decision tree does not necessarily have the same sequence number, or a very small number is the same!