-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathBinarySearchTree.cs
More file actions
149 lines (110 loc) · 3.96 KB
/
BinarySearchTree.cs
File metadata and controls
149 lines (110 loc) · 3.96 KB
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
using System;
using System.Collections.Generic;
using AlgorithmsAndDataStructures.DataStructures.Common;
namespace AlgorithmsAndDataStructures.DataStructures.BinarySearchTrees;
public class BinarySearchTree<T> where T : IComparable<T>
{
private BinaryTreeNode<T> root;
public void Insert(T value)
{
if (root == null)
{
root = new BinaryTreeNode<T> { Value = value };
return;
}
var current = root;
while (true)
{
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation
if (current.Value.Equals(value))
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
return;
if (current.Value.CompareTo(value) > 0)
{
if (current.Left == null)
{
current.Left = new BinaryTreeNode<T> { Value = value };
return;
}
current = current.Left;
}
if (current.Value.CompareTo(value) < 0)
{
if (current.Right == null)
{
current.Right = new BinaryTreeNode<T> { Value = value };
return;
}
current = current.Right;
}
}
}
public void Delete(T value)
{
DeleteInternal(root, value);
}
private static BinaryTreeNode<T> DeleteInternal(BinaryTreeNode<T> currentSubtreeRoot, T value)
{
if (currentSubtreeRoot == null) return null;
if (currentSubtreeRoot.Value.CompareTo(value) < 0)
{
currentSubtreeRoot.Left = DeleteInternal(currentSubtreeRoot.Left, value);
}
else if (currentSubtreeRoot.Value.CompareTo(value) > 0)
{
currentSubtreeRoot.Right = DeleteInternal(currentSubtreeRoot.Right, value);
}
else
{
//Node has no children.
if (currentSubtreeRoot.Left == null && currentSubtreeRoot.Right == null) return null;
//Node has only one child.
if (currentSubtreeRoot.Left == null) return currentSubtreeRoot.Right;
if (currentSubtreeRoot.Right == null) return currentSubtreeRoot.Left;
var minNode = FindNodeWithMinValue(currentSubtreeRoot.Right);
currentSubtreeRoot.Value = minNode.Value;
DeleteInternal(currentSubtreeRoot.Right, minNode.Value);
}
return currentSubtreeRoot;
}
private static BinaryTreeNode<T> FindNodeWithMinValue(BinaryTreeNode<T> right)
{
var current = right;
while (current.Left != null) current = current.Left;
return current;
}
public BinaryTreeNode<T> Search(T value)
{
var current = root;
while (current != null)
{
#pragma warning disable HAA0601 // Value type to reference type conversion causing boxing allocation
if (current.Value.Equals(value))
#pragma warning restore HAA0601 // Value type to reference type conversion causing boxing allocation
return current;
if (current.Value.CompareTo(value) > 0) current = current.Left;
if (current.Value.CompareTo(value) < 0) current = current.Right;
}
return null;
}
public List<T> DepthFirstTraversalInOrder()
{
var result = new List<T>();
DepthFirstTraversalInOrder(root, result);
return result;
}
private static void DepthFirstTraversalInOrder(BinaryTreeNode<T> node, ICollection<T> traversalPath)
{
if (node.Left != null) DepthFirstTraversalInOrder(node.Left, traversalPath);
traversalPath.Add(node.Value);
if (node.Right != null) DepthFirstTraversalInOrder(node.Right, traversalPath);
}
public bool IsEmpty()
{
return root is null;
}
public BinaryTreeNode<T> GetRoot()
{
return root;
}
}