forked from ehilst515/dotnet-data-structures-and-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using Challenges.tests.Arrays; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using Xunit; | ||
using DataStructures.Trees; | ||
using System.Transactions; | ||
using System.ComponentModel.Design.Serialization; | ||
|
||
namespace DataStructures.Tests | ||
{ | ||
public class TreeTests | ||
{ | ||
[Fact] | ||
public void Add_Binary_Tree_can_add_to_empty_tree() | ||
{ | ||
// Arrange | ||
BinaryTree tree = new BinaryTree(); | ||
|
||
// Act | ||
tree.Add(1); | ||
|
||
//Assert | ||
Assert.Equal(1, tree.Root.Value); | ||
} | ||
|
||
[Fact] | ||
public void Add_can_add_single_Left_and_Right_nodes() | ||
{ | ||
// Arrange | ||
BinaryTree tree = new BinaryTree(); | ||
|
||
// Act | ||
tree.Add(6); | ||
tree.Add(2); | ||
tree.Add(7); | ||
|
||
|
||
//Assert | ||
Assert.Equal(6, tree.Root.Value); | ||
Assert.Equal(2, tree.Root.Left.Value); | ||
Assert.Equal(7, tree.Root.Right.Value); | ||
} | ||
|
||
[Fact] | ||
public void Contains_can_find_Node_value() | ||
{ | ||
// Arrange | ||
BinaryTree tree = new BinaryTree(); | ||
tree.Add(6); | ||
tree.Add(2); | ||
tree.Add(7); | ||
tree.Add(3); | ||
tree.Add(10); | ||
tree.Add(1); | ||
|
||
|
||
// Act | ||
bool trueResult = tree.Contains(tree.Root, 10); | ||
bool falseResult = tree.Contains(tree.Root, 11); | ||
|
||
//Assert | ||
Assert.True(trueResult); | ||
Assert.False(falseResult); | ||
} | ||
|
||
|
||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel.Design.Serialization; | ||
using System.Text; | ||
|
||
|
||
namespace DataStructures.Trees | ||
{ | ||
public class BinaryTree | ||
{ | ||
|
||
public Node Root { get; set; } | ||
|
||
public void Add(int value) | ||
{ | ||
Node newNode = new Node(value); | ||
|
||
if (Root == null) | ||
Root = newNode; | ||
|
||
else | ||
{ | ||
Node current = Root; | ||
Node Prev; | ||
while (true) | ||
{ | ||
Prev = current; | ||
|
||
if (value < current.Value) //go left | ||
{ | ||
current = current.Left; | ||
|
||
if (current == null) //add new node if left is null | ||
{ | ||
Prev.Left = newNode; | ||
return; | ||
} | ||
} | ||
|
||
else //go right | ||
{ | ||
current = current.Right; | ||
|
||
if (current == null) //add new node if Right is null | ||
{ | ||
Prev.Right = newNode; | ||
return; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
public bool Contains(Node current, int value) | ||
{ | ||
if (Root == null || current == null) | ||
return false; | ||
|
||
else | ||
{ | ||
if (current.Value == value) | ||
return true; | ||
|
||
else if (value < current.Value) | ||
return Contains(current.Left, value); | ||
|
||
else | ||
return Contains(current.Right, value); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace DataStructures.Trees | ||
{ | ||
|
||
public class Node | ||
{ | ||
public Node(int value) | ||
{ | ||
Value = value; | ||
} | ||
// Value of this node | ||
public int Value { get; set; } | ||
// Pointer to the next node in the list | ||
public Node Left { get; set; } | ||
|
||
public Node Right { get; set; } | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using System.Text; | ||
|
||
namespace DataStructures.Trees | ||
{ | ||
class Tree | ||
{ | ||
|
||
|
||
//public int[] PreOreder() | ||
//{ | ||
// int[] result; | ||
|
||
// Node newNode = new Node(newValue); | ||
// Node current = Root; | ||
// while (current.Next != null) | ||
// { | ||
// if (current.Value == value) | ||
// { | ||
// newNode.Next = current.Next; | ||
// current.Next = newNode; | ||
// break; | ||
// } | ||
// current = current.Next; | ||
// } | ||
//} | ||
} | ||
|
||
|
||
} |