-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathBinarySearchTreeNode.cs
154 lines (133 loc) · 3.87 KB
/
BinarySearchTreeNode.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using System;
namespace Algorithms.DataStructures.Tree
{
/// <summary>
/// Binary search tree node.
/// Contains left node which content less than this.Content
/// Contains right node which content >= this.Content
/// </summary>
/// <typeparam name="T">Type of content</typeparam>
public class BinarySearchTreeNode<T> : ITreeNode<T, BinarySearchTreeNode<T>> where T : IComparable<T>
{
public T Content { get; set; }
public BinarySearchTreeNode<T> Parent { get; set; }
public BinarySearchTreeNode<T> Left { get; set; }
public BinarySearchTreeNode<T> Right { get; set; }
public BinarySearchTreeNode(T content)
{
this.Content = content;
}
/// <summary>
/// Has left child
/// </summary>
public virtual bool HasLeft
{
get { return this.Left != null; }
}
/// <summary>
/// Has right child
/// </summary>
public virtual bool HasRight
{
get { return this.Right != null; }
}
/// <summary>
/// Is this node - left child of its parent
/// </summary>
public bool IsLeftChild
{
get { return this.Parent != null && this.Parent.Left == this; }
}
/// <summary>
/// Is this node - right child of its parent
/// </summary>
public bool IsRightChild
{
get { return this.Parent != null && this.Parent.Right == this; }
}
/// <summary>
/// Is this node terminate (leaf)
/// </summary>
public bool IsTerminate
{
get { return !this.HasRight && !this.HasLeft; }
}
/// <summary>
/// Is this node has parent
/// </summary>
public virtual bool HasParent
{
get { return this.Parent != null; }
}
/// <summary>
/// Count of node's subtree (+ current node)
/// </summary>
public int Count
{
get { return 1 + (this.Left?.Count ?? 0) + (this.Right?.Count ?? 0); }
}
/// <summary>
/// Height of the node (0 for leaf)
/// </summary>
public virtual int Height
{
get { return this.GetHeight(this); }
set { }
}
/// <summary>
/// Degree of outgoing nodes
/// </summary>
public int Degree
{
get { return (this.HasLeft ? 1 : 0) + (this.HasRight ? 1 : 0); }
}
public int Depth
{
get
{
if (this.Parent == null)
{
return 0;
}
return 1 + this.Parent.Depth;
}
}
public int Level
{
get { return this.Depth + 1; }
}
public BinarySearchTreeNode<T> Sibling
{
get
{
return this.IsRightChild
? this.Parent.Left
: this.Parent?.Right;
}
}
public BinarySearchTreeNode<T> Uncle
{
get { return this.Parent?.Sibling; }
}
public BinarySearchTreeNode<T> Grandparent
{
get { return this.Parent?.Parent; }
}
private int GetHeight(BinarySearchTreeNode<T> node)
{
if (node.IsTerminate)
{
return 0;
}
var leftHeight = node.Left?.Height ?? 0;
var rightHeight = node.Right?.Height ?? 0;
return 1 + Math.Max(leftHeight, rightHeight);
}
public int CompareTo(ITreeNode<T, BinarySearchTreeNode<T>> other)
{
return this.Content != null && other != null
? this.Content.CompareTo(other.Content)
: -1;
}
}
}