- Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBinarySearchTreeMapNode.cs
137 lines (118 loc) · 3.82 KB
/
BinarySearchTreeMapNode.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
usingSystem;
namespaceDataStructures.Trees
{
/// <summary>
/// The Binary Search Tree Map node.
/// </summary>
publicclassBSTMapNode<TKey,TValue>:IComparable<BSTMapNode<TKey,TValue>>whereTKey:IComparable<TKey>
{
privateTKey_key;
privateTValue_value;
privateBSTMapNode<TKey,TValue>_parent;
privateBSTMapNode<TKey,TValue>_left;
privateBSTMapNode<TKey,TValue>_right;
publicBSTMapNode(){}
publicBSTMapNode(TKeykey):this(key,default(TValue),0,null,null,null){}
publicBSTMapNode(TKeykey,TValuevalue):this(key,value,0,null,null,null){}
publicBSTMapNode(TKeykey,TValuevalue,intsubTreeSize,BSTMapNode<TKey,TValue>parent,BSTMapNode<TKey,TValue>left,BSTMapNode<TKey,TValue>right)
{
Key=key;
Value=value;
Parent=parent;
LeftChild=left;
RightChild=right;
}
publicvirtualTKeyKey
{
get{returnthis._key;}
set{this._key=value;}
}
publicvirtualTValueValue
{
get{returnthis._value;}
set{this._value=value;}
}
publicvirtualBSTMapNode<TKey,TValue>Parent
{
get{returnthis._parent;}
set{this._parent=value;}
}
publicvirtualBSTMapNode<TKey,TValue>LeftChild
{
get{returnthis._left;}
set{this._left=value;}
}
publicvirtualBSTMapNode<TKey,TValue>RightChild
{
get{returnthis._right;}
set{this._right=value;}
}
/// <summary>
/// Checks whether this node has any children.
/// </summary>
publicvirtualboolHasChildren
{
get{return(this.ChildrenCount>0);}
}
/// <summary>
/// Checks whether this node has left child.
/// </summary>
publicvirtualboolHasLeftChild
{
get{return(this.LeftChild!=null);}
}
/// <summary>
/// Checks whether this node has right child.
/// </summary>
publicvirtualboolHasRightChild
{
get{return(this.RightChild!=null);}
}
/// <summary>
/// Checks whether this node is the left child of it's parent.
/// </summary>
publicvirtualboolIsLeftChild
{
get{return(this.Parent!=null&&this.Parent.LeftChild==this);}
}
/// <summary>
/// Checks whether this node is the left child of it's parent.
/// </summary>
publicvirtualboolIsRightChild
{
get{return(this.Parent!=null&&this.Parent.RightChild==this);}
}
/// <summary>
/// Checks whether this node is a leaf node.
/// </summary>
publicvirtualboolIsLeafNode
{
get{return(this.ChildrenCount==0);}
}
/// <summary>
/// Returns number of direct descendents: 0, 1, 2 (none, left or right, or both).
/// </summary>
/// <returns>Number (0,1,2)</returns>
publicvirtualintChildrenCount
{
get
{
intcount=0;
if(this.HasLeftChild)
count++;
if(this.HasRightChild)
count++;
returncount;
}
}
/// <summary>
/// Compares to.
/// </summary>
publicvirtualintCompareTo(BSTMapNode<TKey,TValue>other)
{
if(other==null)
return-1;
returnthis.Key.CompareTo(other.Key);
}
}//end-of-bstnode
}