During the winter vacation, I checked the data structure of the C # language description. I just want to be more familiar with computer languages. I want to use computer languages to communicate with computers like my mother tongue. It's strange that I didn't quite understand the algorithms that I used to learn the data structure of the C language version. But when I was reading this book, I felt very open. Are you writing well, improving my ability, or both of them?
Go to the topic.
The binary sorting tree is actually not very difficult. A binary search tree stores nodes with smaller values in the left subnode, while a node with larger values in the right subnode. The basic operations are as follows:
Insert
We insert nodes into this binary tree. If a binary tree does not have any nodes, the inserted node becomes the root node. Otherwise, you should traverse the tree to find a node, if the value of the inserted node is greater than this node, it becomes the right node with the inserted node. Otherwise, it is the left node. From this point of view, the root node itself is a tree node. Therefore, I first implemented a TreeNode type, as shown below:
1: public class TreeNode<T>:IComparable,IComparable<TreeNode<T>> {
2:
3: # region. ctor concatenate Constructor
4:
5: public TreeNode():this(default(T),null,null,0){
6: }
7:
8: public TreeNode(T value):this(value,null,null,1) {
9: }
10:
11: public TreeNode(T value,TreeNode<T> leftNode,TreeNode<T> rightNode):this(value,leftNode,rightNode,1) {
12: }
13:
14: public TreeNode(T value, TreeNode<T> leftNode, TreeNode<T> rightNode, int deepness) {
15: Value = value;
16: LeftNode = leftNode;
17: RightNode = rightNode;
18: Deepness = deepness;
19: IsLeaf = true;
20: }
21:
22: public override string ToString() {
23: return Value.ToString();
24: }
25:
26: #endregion
27:
28: #region Interface Members
29:
30: int IComparable.CompareTo(Object item) {
31: TreeNode<T> node = item as TreeNode<T>;
32: if (node == null)
33: throw new ArgumentException("Argument for comparing is not a object of TreeNode Type !!");
34: else {
35: if (this == item)
36: return 0;
37: else {
38: Comparer comparer = new Comparer(CultureInfo.CurrentCulture);
39: return comparer.Compare(this.Value, node.Value);
40: }
41: }
42: }