Skip to content

Commit

Permalink
set bst count
Browse files Browse the repository at this point in the history
  • Loading branch information
justcoding121 committed Oct 26, 2018
1 parent dcee2b6 commit 9ffeab5
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 46 deletions.
22 changes: 17 additions & 5 deletions src/Advanced.Algorithms/DataStructures/Tree/AvlTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public AVLTree(IEnumerable<T> sortedKeys, bool enableNodeLookUp = false)
var nodes = sortedKeys.Select(x => new AVLTreeNode<T>(null, x)).ToArray();
Root = (AVLTreeNode<T>)ToBST(nodes);
recomputeHeight(Root);
assignCount(Root);
Count = nodes.Length;
}

Expand Down Expand Up @@ -136,6 +137,7 @@ private void insert(AVLTreeNode<T> node, T value)
updateHeight(node);
balance(node);

node.UpdateCounts();
}


Expand Down Expand Up @@ -173,6 +175,7 @@ private void delete(AVLTreeNode<T> node, T value)
{
throw new Exception("Item do not exist");
}

delete(node.Right, value);
}
//node is less than the search value so move left to find the deletion node
Expand Down Expand Up @@ -204,8 +207,8 @@ private void delete(AVLTreeNode<T> node, T value)
{
node.Parent.Right = null;
}
baseCase = true;

baseCase = true;
}
else
{
Expand Down Expand Up @@ -233,8 +236,8 @@ private void delete(AVLTreeNode<T> node, T value)

node.Left.Parent = node.Parent;
}
baseCase = true;

baseCase = true;
}
//case two - left tree is null (move sub tree up)
else if (node.Right != null && node.Left == null)
Expand All @@ -257,11 +260,11 @@ private void delete(AVLTreeNode<T> node, T value)
{
node.Parent.Right = node.Right;
}
node.Right.Parent = node.Parent;

node.Right.Parent = node.Parent;
}
baseCase = true;

baseCase = true;
}
//case three - two child trees
//replace the node value with maximum element of left subtree (left max node)
Expand All @@ -285,16 +288,17 @@ private void delete(AVLTreeNode<T> node, T value)

if (baseCase)
{
node.Parent.UpdateCounts();
updateHeight(node.Parent);
balance(node.Parent);
}
else
{
node.UpdateCounts();
updateHeight(node);
balance(node);
}


}

/// <summary>
Expand Down Expand Up @@ -482,6 +486,10 @@ private void rightRotate(AVLTreeNode<T> node)

updateHeight(newRoot);

newRoot.Left.UpdateCounts();
newRoot.Right.UpdateCounts();
newRoot.UpdateCounts();

if (prevRoot == Root)
{
Root = newRoot;
Expand Down Expand Up @@ -524,6 +532,10 @@ private void leftRotate(AVLTreeNode<T> node)

updateHeight(newRoot);

newRoot.Left.UpdateCounts();
newRoot.Right.UpdateCounts();
newRoot.UpdateCounts();

if (prevRoot == Root)
{
Root = newRoot;
Expand Down
50 changes: 20 additions & 30 deletions src/Advanced.Algorithms/DataStructures/Tree/BST.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public BST(IEnumerable<T> sortedKeys) : this()
ValidateCollection(sortedKeys);
var nodes = sortedKeys.Select(x => new BSTNode<T>(null, x)).ToArray();
Root = (BSTNode<T>)ToBST(nodes);
assignCount(Root);
Count = nodes.Length;
}

Expand Down Expand Up @@ -89,7 +90,8 @@ public void Insert(T value)
return;
}

insert(Root, value);
var newNode = insert(Root, value);
newNode.UpdateCounts(true);
Count++;
}

Expand Down Expand Up @@ -144,22 +146,9 @@ public void Delete(T value)
throw new Exception("Empty BST");
}

delete(Root, value);
Count--;
}

internal BSTNode<T> DeleteAndReturnParent(T value)
{
if (Root == null)
{
throw new Exception("Empty BST");
}

var parentNode = delete(Root, value);

var deleted = delete(Root, value);
deleted.UpdateCounts(true);
Count--;

return parentNode;
}

//worst O(n) for unbalanced tree
Expand All @@ -177,44 +166,45 @@ private BSTNode<T> delete(BSTNode<T> node, T value)
node = node.Right ?? throw new Exception("Item do not exist");
continue;
}
//node is less than the search value so move left to find the deletion node

//node is less than the search value so move left to find the deletion node
if (compareResult > 0)
{
node = node.Left ?? throw new Exception("Item do not exist");
continue;
}
}

if (node == null)
{
return null;
}


//node is a leaf node
if (node != null && node.IsLeaf)
if (node.IsLeaf)
{
deleteLeaf(node);
return node.Parent;
return node;
}

//case one - right tree is null (move sub tree up)
if (node?.Left != null && node.Right == null)
if (node.Left != null && node.Right == null)
{
deleteLeftNode(node);
return node.Parent;
return node;
}
//case two - left tree is null (move sub tree up)

if (node?.Right != null && node.Left == null)
//case two - left tree is null (move sub tree up)
if (node.Right != null && node.Left == null)
{
deleteRightNode(node);
return node.Parent;
return node;
}

//case three - two child trees
//replace the node value with maximum element of left subtree (left max node)
//and then delete the left max node

if (node == null)
{
continue;
}

var maxLeftNode = FindMax(node.Left);

node.Value = maxLeftNode.Value;
Expand Down
14 changes: 13 additions & 1 deletion src/Advanced.Algorithms/DataStructures/Tree/SplayTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public SplayTree(IEnumerable<T> collection) : this()
ValidateCollection(collection);
var nodes = collection.Select(x => new SplayTreeNode<T>(null, x)).ToArray();
Root = (SplayTreeNode<T>)ToBST(nodes);
assignCount(Root);
Count = nodes.Length;
}

Expand Down Expand Up @@ -74,7 +75,6 @@ public void Insert(T value)
}

var newNode = insert(Root, value);

splay(newNode);
Count++;
}
Expand Down Expand Up @@ -320,6 +320,8 @@ private SplayTreeNode<T> find(SplayTreeNode<T> parent, T value)

private void splay(SplayTreeNode<T> x)
{
x.UpdateCounts();

while (x.Parent != null)
{
if (x.Parent.Parent == null)
Expand Down Expand Up @@ -352,6 +354,8 @@ private void splay(SplayTreeNode<T> x)
leftRotate(x.Parent);
x = rightRotate(x.Parent);
}

x.UpdateCounts();
}
}

Expand Down Expand Up @@ -391,6 +395,10 @@ private SplayTreeNode<T> rightRotate(SplayTreeNode<T> currentRoot)
newRoot.Right.Left.Parent = newRoot.Right;
}

newRoot.Left.UpdateCounts();
newRoot.Right.UpdateCounts();
newRoot.UpdateCounts();

if (prevRoot == Root)
{
Root = newRoot;
Expand Down Expand Up @@ -436,6 +444,10 @@ private SplayTreeNode<T> leftRotate(SplayTreeNode<T> currentRoot)
newRoot.Left.Right.Parent = newRoot.Left;
}

newRoot.Left.UpdateCounts();
newRoot.Right.UpdateCounts();
newRoot.UpdateCounts();

if (prevRoot == Root)
{
Root = newRoot;
Expand Down
13 changes: 13 additions & 0 deletions src/Advanced.Algorithms/DataStructures/Tree/TreapTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public TreapTree(IEnumerable<T> collection) : this()
var nodes = collection.Select(x => new TreapTreeNode<T>(null, x, rndGenerator.Next())).ToArray();
Root = (TreapTreeNode<T>)ToBST(nodes);
Count = nodes.Length;
assignCount(Root);
heapify(Root);
}
/// <summary>
Expand Down Expand Up @@ -195,6 +196,8 @@ private void delete(TreapTreeNode<T> node, T value)

break;
}

node.UpdateCounts(true);
}

private void deleteLeaf(TreapTreeNode<T> node)
Expand Down Expand Up @@ -335,6 +338,7 @@ private void heapify(TreapTreeNode<T> node)
{
while (node.Parent != null)
{
node.UpdateCounts();
if (node.Priority < node.Parent.Priority)
{
node = node.IsLeftChild ? rightRotate(node.Parent) : leftRotate(node.Parent);
Expand All @@ -345,6 +349,7 @@ private void heapify(TreapTreeNode<T> node)
}
}

node.UpdateCounts(true);
}

/// <summary>
Expand Down Expand Up @@ -383,6 +388,10 @@ private TreapTreeNode<T> rightRotate(TreapTreeNode<T> currentRoot)
newRoot.Right.Left.Parent = newRoot.Right;
}

newRoot.Left.UpdateCounts();
newRoot.Right.UpdateCounts();
newRoot.UpdateCounts();

if (prevRoot == Root)
{
Root = newRoot;
Expand Down Expand Up @@ -429,6 +438,10 @@ private TreapTreeNode<T> leftRotate(TreapTreeNode<T> currentRoot)
newRoot.Left.Right.Parent = newRoot.Left;
}

newRoot.Left.UpdateCounts();
newRoot.Right.UpdateCounts();
newRoot.UpdateCounts();

if (prevRoot == Root)
{
Root = newRoot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public void AVLTree_Smoke_Test()
}

[TestMethod]
public void AVLTree_AccuracyTest()
public void AVLTree_Accuracy_Test()
{
var nodeCount = 1000;

Expand All @@ -110,8 +110,8 @@ public void AVLTree_AccuracyTest()
tree.Insert(sortedNumbers[i]);

Assert.IsTrue(tree.HasItem(sortedNumbers[i]));

Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
tree.Root.VerifyCount();

var actualHeight = tree.GetHeight();

Expand All @@ -133,6 +133,7 @@ public void AVLTree_AccuracyTest()
{
tree.Delete(sortedNumbers[i]);

tree.Root.VerifyCount();
Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));

var actualHeight = tree.GetHeight();
Expand All @@ -159,10 +160,13 @@ public void AVLTree_BulkInit_Test()
Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));
Assert.AreEqual(tree.Count, tree.Count());

tree.Root.VerifyCount();

for (int i = 0; i < nodeCount; i++)
{
tree.Delete(randomNumbers[i]);

tree.Root.VerifyCount();
Assert.IsTrue(tree.Root.IsBinarySearchTree(int.MinValue, int.MaxValue));

var actualHeight = tree.GetHeight();
Expand All @@ -179,7 +183,7 @@ public void AVLTree_BulkInit_Test()
}

[TestMethod]
public void AVLTree_StressTest()
public void AVLTree_Stress_Test()
{
var nodeCount = 1000 * 10;

Expand Down
Loading

0 comments on commit 9ffeab5

Please sign in to comment.