From a433d03fcf0b6e322fa8abbc558ba837306379af Mon Sep 17 00:00:00 2001 From: Enrique Hilst Date: Sun, 13 Sep 2020 18:03:30 -0500 Subject: [PATCH] Tree add and contain working --- Challenges/Arrays/BinarySearch.cs | 2 +- DataStructures.Tests/TreeTests.cs | 70 +++++++++++++++++++++++ DataStructures/Trees/BinarySearchTree.cs | 72 ++++++++++++++++++++++++ DataStructures/Trees/Node.cs | 22 ++++++++ DataStructures/Trees/Tree.cs | 32 +++++++++++ 5 files changed, 197 insertions(+), 1 deletion(-) create mode 100644 DataStructures.Tests/TreeTests.cs create mode 100644 DataStructures/Trees/BinarySearchTree.cs create mode 100644 DataStructures/Trees/Node.cs create mode 100644 DataStructures/Trees/Tree.cs diff --git a/Challenges/Arrays/BinarySearch.cs b/Challenges/Arrays/BinarySearch.cs index 1deabfc..e1f2bec 100644 --- a/Challenges/Arrays/BinarySearch.cs +++ b/Challenges/Arrays/BinarySearch.cs @@ -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; diff --git a/DataStructures.Tests/TreeTests.cs b/DataStructures.Tests/TreeTests.cs new file mode 100644 index 0000000..9c190b8 --- /dev/null +++ b/DataStructures.Tests/TreeTests.cs @@ -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); + } + + + + } +} diff --git a/DataStructures/Trees/BinarySearchTree.cs b/DataStructures/Trees/BinarySearchTree.cs new file mode 100644 index 0000000..317b6c2 --- /dev/null +++ b/DataStructures/Trees/BinarySearchTree.cs @@ -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); + } + } + } +} diff --git a/DataStructures/Trees/Node.cs b/DataStructures/Trees/Node.cs new file mode 100644 index 0000000..776ebee --- /dev/null +++ b/DataStructures/Trees/Node.cs @@ -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; } + } + +} diff --git a/DataStructures/Trees/Tree.cs b/DataStructures/Trees/Tree.cs new file mode 100644 index 0000000..729807a --- /dev/null +++ b/DataStructures/Trees/Tree.cs @@ -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; + // } + //} + } + + +}