Skip to content

Commit

Permalink
Tree add and contain working
Browse files Browse the repository at this point in the history
  • Loading branch information
ehilst515 committed Sep 13, 2020
1 parent 096b71f commit a433d03
Show file tree
Hide file tree
Showing 5 changed files with 197 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Challenges/Arrays/BinarySearch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ namespace Challenges.tests.Arrays
{
public class BinarySearch
{
public static int Search(int[] array, int insertedValue)
public static int Search(int[] array, int insertedValue)
{
int low = 0;
int high = array.Length - 1;
Expand Down
70 changes: 70 additions & 0 deletions DataStructures.Tests/TreeTests.cs
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);
}



}
}
72 changes: 72 additions & 0 deletions DataStructures/Trees/BinarySearchTree.cs
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);
}
}
}
}
22 changes: 22 additions & 0 deletions DataStructures/Trees/Node.cs
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; }
}

}
32 changes: 32 additions & 0 deletions DataStructures/Trees/Tree.cs
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;
// }
//}
}


}

0 comments on commit a433d03

Please sign in to comment.