-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathRandomElement.cs
177 lines (147 loc) · 5.94 KB
/
RandomElement.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
using System;
using PracticeQuestionsSharp.DataStructures;
namespace PracticeQuestionsSharp.Exercises.Binary_Tree
{
//Create a Binary Tree class from scratch with Insert, Remove and Find methods.
//In addition, add a GetRandom() method that returns a random node. All nodes should be equally likely to be chosen.
public class RandomElement<T> where T : IComparable
{
public RandomElement<T> Insert(T data)
{
if (data == null) return null;
if (root == null) { root = new BinaryTreeNodeWithSize<T>(data); }
else Insert(data, root);
Count++;
return this;
}
private void Insert(T data, BinaryTreeNodeWithSize<T> origin)
{
int comparisonResult = data.CompareTo(origin.Data);
if (comparisonResult == 0)
throw new InvalidOperationException("No duplicate data allowed in BST.");
if (comparisonResult < 0)
{
if (origin.Left == null) origin.Left = new BinaryTreeNodeWithSize<T>(data);
else Insert(data, origin.Left);
}
else
{
if (origin.Right == null) origin.Right = new BinaryTreeNodeWithSize<T>(data);
else Insert(data, origin.Right);
}
origin.UpdateSize();
}
public void Remove(T data)
{
if ((root = Remove(data, root)) != null) Count--;
if (root == null) Count = 0;
}
private BinaryTreeNodeWithSize<T> Remove(T data, BinaryTreeNodeWithSize<T> origin)
{
if (origin == null || data == null) return null;
int comparisonResult = data.CompareTo(origin.Data);
if (comparisonResult < 0)
{
if (origin.Left == null) throw new InvalidOperationException($"Couldn't find {data} to remove!");
origin.Left = Remove(data, origin.Left);
origin.UpdateSize();
return origin;
}
if (comparisonResult > 0)
{
if (origin.Right == null) throw new InvalidOperationException($"Couldn't find {data} to remove!");
origin.Right = Remove(data, origin.Right);
origin.UpdateSize();
return origin;
}
//Match found
//If we have two children, replace my data with rights min, then remove starting from right
if (origin.Left != null && origin.Right != null)
{
var minimum = Minimum(origin.Right);
origin.Data = minimum;
origin.Right = Remove(minimum, origin.Right);
origin.UpdateSize();
return origin;
}
if (origin.Left == null) return origin.Right;
else return origin.Left;
}
public bool Find(T data)
{
BinaryTreeNodeWithSize<T> curr = root;
while (curr != null)
{
int comparisonResult = data.CompareTo(curr.Data);
if (comparisonResult < 0) curr = curr.Left;
else if (comparisonResult > 0) curr = curr.Right;
else if (comparisonResult == 0) return true;
}
return false;
}
T Minimum(BinaryTreeNodeWithSize<T> origin)
{
while (origin.Left != null) origin = origin.Left;
return origin.Data;
}
//Gets a random number from 0 through Count - 1 and travels to that node (in-order)
//Takes n time in the worst case where n = Count
public T GetRandom()
{
return TraverseToIndex(r.Next(0, Count));
}
//Gets a random number by comparing sub-tree sizes to determine the probability
//of choosing that node. Worst case takes time equal to the depth of the longest path.
public T GetRandom2()
{
int rIndex = r.Next(1, Count + 1);
BinaryTreeNodeWithSize<T> n = root;
while (n != null)
{
if (n.Size == rIndex) return n.Data;
if (n.Left != null && rIndex <= n.Left.Size) n = n.Left;
else
{
//Left side was not chosen, update our rIndex because we are no longer considering it
rIndex -= n.Left?.Size ?? 0;
n = n.Right;
}
}
throw new InvalidOperationException("Tree empty!");
}
public T TraverseToIndex(int index)
{
BinaryTreeNodeWithSize<T> n = root;
var stack = new Stack<T>();
RandTarget = index;
RandIndex = -1;
Traversal(root, stack);
return stack.Pop();
}
private void Traversal(BinaryTreeNodeWithSize<T> origin, Stack<T> stack)
{
if (RandIndex == RandTarget) return; //reached the random target, stop searching
if (origin.Left != null) Traversal(origin.Left, stack);
if (RandIndex == RandTarget) return;
stack.Push(origin.Data);
RandIndex++;
if (RandIndex == RandTarget) return;
if (origin.Right != null) Traversal(origin.Right, stack);
}
public int RandIndex { get; set; }
public int RandTarget { get; set; }
private readonly Random r = new Random();
public int Count { get; set; }
public bool IsEmpty => root == null;
private BinaryTreeNodeWithSize<T> root;
}
class BinaryTreeNodeWithSize<T>
{
public BinaryTreeNodeWithSize(T data) { Data = data; Size = 1; }
public T Data { get; set; }
public BinaryTreeNodeWithSize<T> Left { get; set; }
public BinaryTreeNodeWithSize<T> Right { get; set; }
public int Size { get; set; }
public void UpdateSize() => Size = (Right?.Size ?? 0) + (Left?.Size ?? 0) + 1;
}
}