Skip to content

Commit 5ea702c

Browse files
committed
IEnumerable implementations
1 parent d075c7e commit 5ea702c

30 files changed

+576
-342
lines changed

Advanced.Algorithms.Tests/DataStructures/LinkedList/CircularLinkedList_Tests.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Advanced.Algorithms.DataStructures;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Linq;
34

45
namespace Advanced.Algorithms.Tests.DataStructures
56
{
@@ -20,7 +21,6 @@ public void CircularLinkedList_Test()
2021
list.Insert("c");
2122

2223
Assert.AreEqual(list.Count(), 4);
23-
Assert.AreEqual(list.GetAllNodes().Count, 4);
2424

2525
list.Delete("a");
2626
Assert.AreEqual(list.Count(), 3);
@@ -46,7 +46,6 @@ public void CircularLinkedList_Test()
4646
list.Insert("c");
4747

4848
Assert.AreEqual(list.Count(), 4);
49-
Assert.AreEqual(list.GetAllNodes().Count, 4);
5049

5150
list.Delete("a");
5251
Assert.AreEqual(list.Count(), 3);

Advanced.Algorithms.Tests/DataStructures/LinkedList/DoublyLinkedList_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Advanced.Algorithms.DataStructures;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Linq;
34

45
namespace Advanced.Algorithms.Tests.DataStructures
56
{

Advanced.Algorithms.Tests/DataStructures/LinkedList/SinglyLinkedList_Tests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Advanced.Algorithms.DataStructures;
22
using Microsoft.VisualStudio.TestTools.UnitTesting;
3+
using System.Linq;
34

45
namespace Advanced.Algorithms.Tests.DataStructures
56
{

Advanced.Algorithms/DataStructures/Dictionary/OpenAddressDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private void setValue(TK key, TV value)
213213

214214
if (hashArray[index] == null)
215215
{
216-
throw new Exception("Item not found");
216+
Add(key, value);
217217
}
218218
else
219219
{

Advanced.Algorithms/DataStructures/Dictionary/SeparateChainingDictionary.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private void setValue(K key, V value)
149149

150150
if (hashArray[index] == null)
151151
{
152-
throw new Exception("Item not found");
152+
Add(key, value);
153153
}
154154
else
155155
{

Advanced.Algorithms/DataStructures/Dictionary/SortedDictionary.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ public TV this[TK key]
6060
}
6161
set
6262
{
63-
var node = binarySearchTree.FindNode(new SortedDictionaryNode<TK, TV>(key, default(TV)));
64-
if (node == null)
63+
if(ContainsKey(key))
6564
{
66-
throw new Exception("Key not found.");
65+
Remove(key);
6766
}
68-
69-
node.Value.Value = value;
67+
68+
Add(key, value);
7069
}
7170
}
7271

@@ -84,9 +83,9 @@ public void Remove(TK key)
8483
/// Time complexity: O(log(n)).
8584
/// </summary>
8685
/// <returns>Null if the given key does'nt exist or next key does'nt exist.</returns>
87-
public KeyValuePair<TK, TV> Next(TK key)
86+
public KeyValuePair<TK, TV> NextHigher(TK key)
8887
{
89-
var next = binarySearchTree.Next(new SortedDictionaryNode<TK, TV>(key, default(TV)));
88+
var next = binarySearchTree.NextHigher(new SortedDictionaryNode<TK, TV>(key, default(TV)));
9089

9190
if(next == null)
9291
{
@@ -101,9 +100,9 @@ public KeyValuePair<TK, TV> Next(TK key)
101100
/// Time complexity: O(log(n)).
102101
/// </summary>
103102
/// <returns>Null if the given key does'nt exist or previous key does'nt exist.</returns>
104-
public KeyValuePair<TK, TV> Previous(TK key)
103+
public KeyValuePair<TK, TV> NextLower(TK key)
105104
{
106-
var prev = binarySearchTree.Previous(new SortedDictionaryNode<TK, TV>(key, default(TV)));
105+
var prev = binarySearchTree.NextLower(new SortedDictionaryNode<TK, TV>(key, default(TV)));
107106

108107
if (prev == null)
109108
{

Advanced.Algorithms/DataStructures/HashSet/OpenAddressHashSet.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public bool Contains(TV value)
6262

6363
public void Add(TV value)
6464
{
65-
6665
grow();
6766

6867
var hashCode = getHash(value);

Advanced.Algorithms/DataStructures/HashSet/SortedHashSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public void Remove(V value)
5959
/// <returns>Null if the given value does'nt exist or next value does'nt exist.</returns>
6060
public V Next(V value)
6161
{
62-
return binarySearchTree.Next(value);
62+
return binarySearchTree.NextHigher(value);
6363
}
6464

6565
/// <summary>
@@ -69,7 +69,7 @@ public V Next(V value)
6969
/// <returns>Null if the given value does'nt exist or previous value does'nt exist.</returns>
7070
public V Previous(V value)
7171
{
72-
return binarySearchTree.Previous(value);
72+
return binarySearchTree.NextLower(value);
7373
}
7474

7575
/// <summary>

Advanced.Algorithms/DataStructures/Heap/Max/BMaxHeap.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45

56
namespace Advanced.Algorithms.DataStructures
67
{
7-
public class BMaxHeap<T> where T : IComparable
8+
public class BMaxHeap<T> : IEnumerable<T> where T : IComparable
89
{
910
private T[] heapArray;
1011

@@ -245,5 +246,15 @@ private void doubleArray()
245246

246247
heapArray = biggerArray;
247248
}
249+
250+
public IEnumerator<T> GetEnumerator()
251+
{
252+
return GetEnumerator();
253+
}
254+
255+
IEnumerator IEnumerable.GetEnumerator()
256+
{
257+
return new ArrayListEnumerator<T>(heapArray, Count);
258+
}
248259
}
249260
}

Advanced.Algorithms/DataStructures/Heap/Min/BMinHeap.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using System.Collections;
23
using System.Collections.Generic;
34
using System.Linq;
45

56
namespace Advanced.Algorithms.DataStructures
67
{
7-
public class BMinHeap<T> where T : IComparable
8+
public class BMinHeap<T> : IEnumerable<T> where T : IComparable
89
{
910
private T[] heapArray;
1011
private readonly IComparer<T> comparer;
@@ -306,5 +307,15 @@ private void doubleArray()
306307

307308
heapArray = biggerArray;
308309
}
310+
311+
public IEnumerator<T> GetEnumerator()
312+
{
313+
return GetEnumerator();
314+
}
315+
316+
IEnumerator IEnumerable.GetEnumerator()
317+
{
318+
return new ArrayListEnumerator<T>(heapArray, Count);
319+
}
309320
}
310321
}

Advanced.Algorithms/DataStructures/LinkedList/CircularLinkedList.cs

Lines changed: 41 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,18 @@
44

55
namespace Advanced.Algorithms.DataStructures
66
{
7-
//define the generic node
8-
public class CircularLinkedListNode<T>
9-
{
10-
public CircularLinkedListNode<T> Prev;
11-
public CircularLinkedListNode<T> Next;
12-
13-
public T Data;
14-
15-
public CircularLinkedListNode(T data)
16-
{
17-
this.Data = data;
18-
}
19-
}
20-
217
/// <summary>
22-
/// A singly linked list implementation
8+
/// A circular linked list implementation.
239
/// </summary>
24-
/// <typeparam name="T"></typeparam>
2510
public class CircularLinkedList<T> : IEnumerable<T>
2611
{
2712
public CircularLinkedListNode<T> ReferenceNode;
2813

29-
//marks this data as the new head (kinda insert first assuming current reference node as head)
30-
//cost O(1)
14+
/// <summary>
15+
/// Marks this data as the new reference node after insertion.
16+
/// Like insert first assuming that current reference node as head.
17+
/// Time Complexity: O(1).
18+
/// </summary>
3119
public CircularLinkedListNode<T> Insert(T data)
3220
{
3321
var newNode = new CircularLinkedListNode<T>(data);
@@ -56,9 +44,9 @@ public CircularLinkedListNode<T> Insert(T data)
5644
return newNode;
5745
}
5846

59-
60-
61-
//O(1) delete this item
47+
/// <summary>
48+
/// Time complexity: O(1)
49+
/// </summary>
6250
public void Delete(CircularLinkedListNode<T> current)
6351
{
6452
if (ReferenceNode.Next == ReferenceNode)
@@ -82,8 +70,10 @@ public void Delete(CircularLinkedListNode<T> current)
8270
}
8371
}
8472

85-
//search and delete
86-
//cost O(n) in worst case O(nlog(n) average?
73+
/// <summary>
74+
/// search and delete.
75+
/// Time complexity:O(n).
76+
/// </summary>
8777
public void Delete(T data)
8878
{
8979
if (ReferenceNode == null)
@@ -137,41 +127,18 @@ public void Delete(T data)
137127
}
138128
}
139129

140-
//O(n) always
141-
public int Count()
142-
{
143-
//re-implement
144-
//no need to iterate
145-
var i = 0;
146-
147-
var current = ReferenceNode;
148-
149-
if (current == null)
150-
{
151-
return 0;
152-
}
153-
else
154-
{
155-
i++;
156-
}
157-
158-
while (current.Next != ReferenceNode)
159-
{
160-
i++;
161-
current = current.Next;
162-
}
163-
164-
return i;
165-
}
166-
167-
//O(1) always
130+
/// <summary>
131+
/// Time complexity: O(1).
132+
/// </summary>
168133
public bool IsEmpty()
169134
{
170135
return ReferenceNode == null;
171136
}
172137

173-
//O(1) always
174-
public void DeleteAll()
138+
/// <summary>
139+
/// Time complexity: O(1).
140+
/// </summary>
141+
public void Clear()
175142
{
176143
if (ReferenceNode == null)
177144
{
@@ -182,27 +149,16 @@ public void DeleteAll()
182149

183150
}
184151

185-
//O(n) time complexity
186-
public List<T> GetAllNodes()
152+
/// <summary>
153+
/// Time complexity: O(1).
154+
/// </summary>
155+
public void Union(CircularLinkedList<T> newList)
187156
{
188-
var result = new List<T>();
189-
190-
var current = ReferenceNode;
191-
192-
if (current == null)
193-
{
194-
return result;
195-
}
196-
197-
result.Add(current.Data);
198-
199-
while (current.Next != ReferenceNode)
200-
{
201-
result.Add(current.Data);
202-
current = current.Next;
203-
}
157+
ReferenceNode.Prev.Next = newList.ReferenceNode;
158+
ReferenceNode.Prev = newList.ReferenceNode.Prev;
204159

205-
return result;
160+
newList.ReferenceNode.Prev.Next = ReferenceNode;
161+
newList.ReferenceNode.Prev = ReferenceNode.Prev;
206162
}
207163

208164
IEnumerator IEnumerable.GetEnumerator()
@@ -215,22 +171,22 @@ public IEnumerator<T> GetEnumerator()
215171
return new CircularLinkedListEnumerator<T>(ref ReferenceNode);
216172
}
217173

218-
/// <summary>
219-
/// O(1) time complexity
220-
/// </summary>
221-
/// <param name="newList"></param>
222-
internal void Union(CircularLinkedList<T> newList)
223-
{
224-
ReferenceNode.Prev.Next = newList.ReferenceNode;
225-
ReferenceNode.Prev = newList.ReferenceNode.Prev;
174+
}
226175

227-
newList.ReferenceNode.Prev.Next = ReferenceNode;
228-
newList.ReferenceNode.Prev = ReferenceNode.Prev;
176+
public class CircularLinkedListNode<T>
177+
{
178+
public CircularLinkedListNode<T> Prev;
179+
public CircularLinkedListNode<T> Next;
180+
181+
public T Data;
182+
183+
public CircularLinkedListNode(T data)
184+
{
185+
this.Data = data;
229186
}
230187
}
231188

232-
// implement IEnumerator.
233-
public class CircularLinkedListEnumerator<T> : IEnumerator<T>
189+
internal class CircularLinkedListEnumerator<T> : IEnumerator<T>
234190
{
235191
internal CircularLinkedListNode<T> referenceNode;
236192
internal CircularLinkedListNode<T> currentNode;
@@ -273,14 +229,7 @@ public T Current
273229
{
274230
get
275231
{
276-
try
277-
{
278-
return currentNode.Data;
279-
}
280-
catch (IndexOutOfRangeException)
281-
{
282-
throw new InvalidOperationException();
283-
}
232+
return currentNode.Data;
284233
}
285234
}
286235
public void Dispose()

0 commit comments

Comments
 (0)