Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
aalhour committed Aug 27, 2015
2 parents ce82d45 + c267488 commit 9b0b562
Show file tree
Hide file tree
Showing 14 changed files with 929 additions and 83 deletions.
3 changes: 2 additions & 1 deletion DataStructures/DataStructures.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
<Compile Include="Trees\AVLTreeNode.cs" />
<Compile Include="Trees\BinarySearchTree.cs" />
<Compile Include="Trees\BinarySearchTreeNode.cs" />
<Compile Include="Trees\IAVLTree.cs" />
<Compile Include="Trees\IBinarySearchTree.cs" />
<Compile Include="Heaps\IMaxHeap.cs" />
<Compile Include="Heaps\IMinHeap.cs" />
Expand All @@ -70,6 +69,8 @@
<Compile Include="Heaps\KeyedPriorityQueue.cs" />
<Compile Include="Heaps\MinPriorityQueue.cs" />
<Compile Include="Common\Comparers.cs" />
<Compile Include="Trees\RedBlackTree.cs" />
<Compile Include="Trees\RedBlackTreeNode.cs" />
<Compile Include="Trees\TreeDrawer.cs" />
<Compile Include="Dictionaries\ChainedHashTable.cs" />
<Compile Include="Lists\DLinkedList_KeyValue.cs" />
Expand Down
8 changes: 4 additions & 4 deletions DataStructures/Trees/AVLTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AVLTree<T> : BinarySearchTree<T> where T : IComparable<T>
public new AVLTreeNode<T> Root
{
get { return (AVLTreeNode<T>)base.Root; }
set { base.Root = value; }
internal set { base.Root = value; }
}

public AVLTree()
Expand Down Expand Up @@ -317,7 +317,7 @@ public override void Insert(List<T> collection)
/// </summary>
public override void Remove(T item)
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

// Get the node from the tree
Expand Down Expand Up @@ -354,7 +354,7 @@ public override void Remove(T item)
/// </summary>
public override void RemoveMin()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

// Get the node from the tree
Expand All @@ -374,7 +374,7 @@ public override void RemoveMin()
/// </summary>
public override void RemoveMax()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

// Get the node from the tree
Expand Down
35 changes: 19 additions & 16 deletions DataStructures/Trees/AugmentedBinarySearchTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,22 @@ public class AugmentedBinarySearchTree<T> : BinarySearchTree<T> where T : ICompa

public AugmentedBinarySearchTree() : base() { }

/// <summary>
/// Returns the height of the tree.
/// </summary>
/// <returns>Hight</returns>
public override int Height
{
get
{
if (IsEmpty)
return 0;

var currentNode = this.Root;
return this._getTreeHeight(currentNode);
}
}

/// <summary>
/// Returns the Subtrees size for a tree node if node exists; otherwise 0 (left and right nodes of leafs).
/// This is used in the recursive function UpdateSubtreeSize.
Expand Down Expand Up @@ -177,19 +193,6 @@ protected int _getTreeHeight(BSTRankedNode<T> node)
return 0;
}

/// <summary>
/// Returns the height of the tree.
/// </summary>
/// <returns>Hight</returns>
public override int Height()
{
if (IsEmpty())
return 0;

var currentNode = this.Root;
return this._getTreeHeight(currentNode);
}

/// <summary>
/// Inserts an element to the tree
/// </summary>
Expand Down Expand Up @@ -246,7 +249,7 @@ public override void Insert(List<T> collection)
/// <param name="item">item to remove.</param>
public override void Remove(T item)
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

var node = (BSTRankedNode<T>)base._findNode(this.Root, item);
Expand All @@ -263,7 +266,7 @@ public override void Remove(T item)
/// </summary>
public override void RemoveMin()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

var node = (BSTRankedNode<T>)_findMinNode(this.Root);
Expand All @@ -279,7 +282,7 @@ public override void RemoveMin()
/// </summary>
public override void RemoveMax()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

var node = (BSTRankedNode<T>)_findMaxNode(this.Root);
Expand Down
45 changes: 28 additions & 17 deletions DataStructures/Trees/BinarySearchTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public enum TraversalMode
/// </summary>
/// <returns></returns>
protected int _count { get; set; }
private BSTNode<T> _root { get; set; }
protected virtual BSTNode<T> _root { get; set; }

public virtual BSTNode<T> Root
{
get { return this._root; }
set { this._root = value; }
internal set { this._root = value; }
}

public BinarySearchTree()
Expand Down Expand Up @@ -331,32 +331,35 @@ protected virtual void _inOrderTraverse(BSTNode<T> currentNode, ref List<T> list
/// Return the number of elements in this tree
/// </summary>
/// <returns></returns>
public virtual int Count()
public virtual int Count
{
return _count;
get { return _count; }
}

/// <summary>
/// Checks if tree is empty.
/// </summary>
/// <returns></returns>
public virtual bool IsEmpty()
public virtual bool IsEmpty
{
return (_count == 0);
get { return (_count == 0); }
}

/// <summary>
/// Returns the height of the tree.
/// Time-complexity: O(n), where n = number of nodes.
/// </summary>
/// <returns>Hight</returns>
public virtual int Height()
public virtual int Height
{
if (IsEmpty())
return 0;
get
{
if (IsEmpty)
return 0;

var currentNode = Root;
return _getTreeHeight(currentNode);
var currentNode = Root;
return _getTreeHeight(currentNode);
}
}

/// <summary>
Expand Down Expand Up @@ -411,7 +414,7 @@ public virtual void Insert(List<T> collection)
/// <param name="item">item to remove.</param>
public virtual void Remove(T item)
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

var node = _findNode(Root, item);
Expand All @@ -427,7 +430,7 @@ public virtual void Remove(T item)
/// </summary>
public virtual void RemoveMin()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

var node = _findMinNode(Root);
Expand All @@ -439,7 +442,7 @@ public virtual void RemoveMin()
/// </summary>
public virtual void RemoveMax()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

var node = _findMaxNode(Root);
Expand All @@ -455,13 +458,21 @@ public virtual void Clear()
_count = 0;
}

/// <summary>
/// Checks for the existence of an item
/// </summary>
public virtual bool Contains(T item)
{
return (_findNode(_root, item) != null);
}

/// <summary>
/// Finds the minimum in tree
/// </summary>
/// <returns>Min</returns>
public virtual T FindMin()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

return _findMinNode(Root).Value;
Expand Down Expand Up @@ -501,7 +512,7 @@ public virtual T FindNextLarger(T item)
/// <returns>Max</returns>
public virtual T FindMax()
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

return _findMaxNode(Root).Value;
Expand All @@ -514,7 +525,7 @@ public virtual T FindMax()
/// <returns>Item.</returns>
public virtual T Find(T item)
{
if (IsEmpty())
if (IsEmpty)
throw new Exception("Tree is empty.");

var node = _findNode(Root, item);
Expand Down
38 changes: 0 additions & 38 deletions DataStructures/Trees/IAVLTree.cs

This file was deleted.

9 changes: 6 additions & 3 deletions DataStructures/Trees/IBinarySearchTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ namespace DataStructures.Trees
public interface IBinarySearchTree<T> where T : System.IComparable<T>
{
/// Returns the number of elements in the Tree
int Count();
int Count { get; }

// Checks if the tree is empty.
bool IsEmpty();
bool IsEmpty { get; }

// Returns the height of the tree.
int Height();
int Height { get; }

// Inserts an element to the tree
void Insert(T item);
Expand All @@ -31,6 +31,9 @@ public interface IBinarySearchTree<T> where T : System.IComparable<T>
// Remove an element from tree
void Remove(T item);

// Check for the existence of an item
bool Contains(T item);

// Finds the minimum element.
T FindMin();

Expand Down
Loading

0 comments on commit 9b0b562

Please sign in to comment.