Skip to content

Commit ccabd95

Browse files
Iterative traversal method (#13)
1 parent 6933be5 commit ccabd95

10 files changed

Lines changed: 386 additions & 531 deletions

File tree

DStruct/BinaryTrees/AVLTree.cs

Lines changed: 10 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System;
1+
using DStruct.Queues;
2+
using System;
23
using System.Collections.Generic;
34

45
// ReSharper disable RedundantAssignment
@@ -7,93 +8,10 @@ namespace DStruct.BinaryTrees
78
{
89
/// <summary>Represents a node-based, self-balancing <see cref="IBinarySearchTree{T}"/> enhanced to implement an efficient indexer.</summary>
910
/// <typeparam name="T">The type of the values stored in the <see cref="AVLTree{T}"/>.</typeparam>
10-
public class AVLTree<T> : IBinarySearchTree<T>
11+
public class AVLTree<T> : BinarySearchTreeBase<T>
1112
{
12-
private readonly IComparer<T> _comparer = Comparer<T>.Default;
13-
1413
private AVLTreeNode<T> _root;
1514

16-
/// <summary>Gets the number of elements stored in the <see cref="AVLTree{T}" />. <code>Complexity: O(1)</code></summary>
17-
public int Count { get; private set; }
18-
19-
/// <summary>Gets the minimum value element stored in the <see cref="AVLTree{T}"/>. <code>Complexity: O(LogN)</code></summary>
20-
/// <exception cref="InvalidOperationException"><see cref="AVLTree{T}"/> is empty.</exception>
21-
public T Min
22-
{
23-
get
24-
{
25-
if (_root == null)
26-
{
27-
throw new InvalidOperationException("The AVLTree is empty.");
28-
}
29-
30-
var curr = _root;
31-
while (curr.Left != null)
32-
{
33-
curr = curr.Left;
34-
}
35-
36-
return curr.Value;
37-
}
38-
}
39-
40-
/// <summary>Gets the maximum value element stored in the <see cref="AVLTree{T}" />. <code>Complexity: O(LogN)</code></summary>
41-
/// <exception cref="InvalidOperationException"><see cref="AVLTree{T}"/> is empty.</exception>
42-
public T Max
43-
{
44-
get
45-
{
46-
if (_root == null)
47-
{
48-
throw new InvalidOperationException("The AVLTree is empty.");
49-
}
50-
51-
var curr = _root;
52-
while (curr.Right != null)
53-
{
54-
curr = curr.Right;
55-
}
56-
57-
return curr.Value;
58-
}
59-
}
60-
61-
/// <summary>Gets the element at the specified index. <code>Complexity: O(LogN)</code></summary>
62-
/// <param name="index">The index of the element to get from the <see cref="AVLTree{T}"/>.</param>
63-
/// <returns>The element at the specified index.</returns>
64-
/// <exception cref="IndexOutOfRangeException"><paramref name="index"/> is out of the bounds of the <see cref="AVLTree{T}"/>.</exception>
65-
public T this[int index]
66-
{
67-
get
68-
{
69-
if (index < 0 || index >= Count)
70-
{
71-
throw new IndexOutOfRangeException("Index is outside the bounds of the AVLTree.");
72-
}
73-
74-
var curr = _root;
75-
int check = 0;
76-
77-
while (true)
78-
{
79-
if (check + curr.LeftChildren == index)
80-
{
81-
return curr.Value;
82-
}
83-
84-
if (check + curr.LeftChildren > index)
85-
{
86-
curr = curr.Left;
87-
}
88-
else
89-
{
90-
check += curr.LeftChildren + 1;
91-
curr = curr.Right;
92-
}
93-
}
94-
}
95-
}
96-
9715
/// <summary>Initializes a new instance of <see cref="AVLTree{T}"/> that is empty.</summary>
9816
public AVLTree()
9917
{
@@ -103,30 +21,24 @@ public AVLTree()
10321
/// <param name="collection">The collection of elements to add to the <see cref="AVLTree{T}"/>.</param>
10422
/// <exception cref="ArgumentNullException"><paramref name="collection"/> is <c>null</c>.</exception>
10523
public AVLTree(IEnumerable<T> collection)
24+
: base(collection)
10625
{
107-
if (collection == null)
108-
{
109-
throw new ArgumentNullException(nameof(collection));
110-
}
111-
112-
foreach (var value in collection)
113-
{
114-
Insert(value);
115-
}
11626
}
11727

11828
/// <summary>Initializes a new instance of <see cref="AVLTree{T}"/> that is empty and uses the specified <see cref="IComparer{T}"/>.</summary>
11929
/// <param name="comparer">The <see cref="IComparer{T}"/> that will be used for making comparisons.</param>
12030
/// <exception cref="ArgumentNullException"><paramref name="comparer"/> is <c>null</c>.</exception>
12131
public AVLTree(IComparer<T> comparer)
32+
: base(comparer)
12233
{
123-
_comparer = comparer ?? throw new ArgumentNullException(nameof(comparer));
12434
}
12535

36+
private protected override IBinarySearchTreeNode<T> Root => _root;
37+
12638
/// <summary>Inserts an element into the <see cref="AVLTree{T}" /> and returns its index. <code>Complexity: O(LogN)</code></summary>
12739
/// <param name="value">The element to add to the <see cref="AVLTree{T}"/>.</param>
12840
/// <returns>The index at which the element was placed.</returns>
129-
public int Insert(T value)
41+
public override int Insert(T value)
13042
{
13143
int position = 0;
13244

@@ -162,7 +74,7 @@ void InsertHelper(ref AVLTreeNode<T> node)
16274
/// <summary>Determines whether the <see cref="AVLTree{T}" /> contains a specific value. <code>Complexity: O(LogN)</code></summary>
16375
/// <param name="value">The element to locate in the <see cref="AVLTree{T}"/>.</param>
16476
/// <returns><c>true</c> if the <see cref="AVLTree{T}"/> contains <paramref name="value"/>; <c>false</c> otherwise.</returns>
165-
public bool Find(T value)
77+
public override bool Find(T value)
16678
{
16779
if (_root == null)
16880
{
@@ -188,7 +100,7 @@ public bool Find(T value)
188100
/// <summary>Removes one occurrence of a specific element from the <see cref="AVLTree{T}" />. <code>Complexity: O(LogN)</code></summary>
189101
/// <param name="value">The element to remove from the <see cref="AVLTree{T}"/>.</param>
190102
/// <returns><c>true</c> if the element was successfully removed from the <see cref="AVLTree{T}"/>; <c>false</c> otherwise.</returns>
191-
public bool Remove(T value)
103+
public override bool Remove(T value)
192104
{
193105
bool ret = true;
194106

@@ -246,82 +158,5 @@ AVLTreeNode<T> RemoveHelper(AVLTreeNode<T> node)
246158
if (ret) Count--;
247159
return ret;
248160
}
249-
250-
/// <summary>Returns the list of the elements stored in the <see cref="AVLTree{T}" /> in-order. <code>Complexity: O(N)</code></summary>
251-
/// <returns>List of in-order elements.</returns>
252-
public T[] InOrderTraverse()
253-
{
254-
var output = new T[Count];
255-
int i = 0;
256-
257-
void IOTHelper(AVLTreeNode<T> node)
258-
{
259-
if (node == null)
260-
{
261-
return;
262-
}
263-
264-
IOTHelper(node.Left);
265-
output[i++] = node.Value;
266-
IOTHelper(node.Right);
267-
}
268-
269-
IOTHelper(_root);
270-
271-
return output;
272-
}
273-
274-
/// <summary>Returns the list of the elements stored in the <see cref="AVLTree{T}" /> pre-order. <code>Complexity: O(N)</code></summary>
275-
/// <returns>List of pre-order elements.</returns>
276-
public T[] PreOrderTraverse()
277-
{
278-
var output = new T[Count];
279-
int i = 0;
280-
281-
void POTHelper(AVLTreeNode<T> node)
282-
{
283-
if (node == null)
284-
{
285-
return;
286-
}
287-
288-
output[i++] = node.Value;
289-
POTHelper(node.Left);
290-
POTHelper(node.Right);
291-
}
292-
293-
POTHelper(_root);
294-
295-
return output;
296-
}
297-
298-
/// <summary>Returns the list of the elements stored in the <see cref="AVLTree{T}" /> post-order. <code>Complexity: O(N)</code></summary>
299-
/// <returns>List of post-order elements.</returns>
300-
public T[] PostOrderTraverse()
301-
{
302-
var output = new T[Count];
303-
int i = 0;
304-
305-
void POTHelper(AVLTreeNode<T> node)
306-
{
307-
if (node == null)
308-
{
309-
return;
310-
}
311-
312-
POTHelper(node.Left);
313-
POTHelper(node.Right);
314-
output[i++] = node.Value;
315-
}
316-
317-
POTHelper(_root);
318-
319-
return output;
320-
}
321-
322-
private int Compare(T x, T y)
323-
{
324-
return _comparer.Compare(x, y);
325-
}
326161
}
327162
}

DStruct/BinaryTrees/AVLTreeNode.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
namespace DStruct.BinaryTrees
44
{
5-
class AVLTreeNode<T>
5+
internal class AVLTreeNode<T> : IBinarySearchTreeNode<T>
66
{
77
public AVLTreeNode<T> Left;
88
public AVLTreeNode<T> Right;
99

1010
public T Value { get; set; }
11+
1112
public int LeftChildren { get; set; }
1213
public int Height { get; set; }
1314

@@ -22,6 +23,9 @@ public int Balance
2223
}
2324
}
2425

26+
IBinarySearchTreeNode<T> IBinarySearchTreeNode<T>.Left => Left;
27+
IBinarySearchTreeNode<T> IBinarySearchTreeNode<T>.Right => Right;
28+
2529
public AVLTreeNode(T value, int height = 1, int leftChildren = 0)
2630
{
2731
Value = value;

DStruct/BinaryTrees/BSTNode.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
namespace DStruct.BinaryTrees
22
{
3-
class BSTNode<T>
3+
internal class BSTNode<T> : IBinarySearchTreeNode<T>
44
{
55
public BSTNode<T> Left;
66
public BSTNode<T> Right;
77

88
public T Value { get; set; }
99
public int LeftChildren { get; set; }
1010

11+
IBinarySearchTreeNode<T> IBinarySearchTreeNode<T>.Left => Left;
12+
IBinarySearchTreeNode<T> IBinarySearchTreeNode<T>.Right => Right;
13+
1114
public BSTNode()
1215
{
1316
}

0 commit comments

Comments
 (0)