Skip to content

Commit

Permalink
Added BST README
Browse files Browse the repository at this point in the history
  • Loading branch information
ehilst515 committed Sep 14, 2020
1 parent df5b078 commit ad0720e
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 56 deletions.
Binary file added Assets/BTFindMax.PNG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
24 changes: 24 additions & 0 deletions DataStructures.Tests/TreeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -133,5 +133,29 @@ public void PostOrder_returns_values_as_strings()
//Assert
Assert.Equal("1 3 2 9 20 10 7 6", result.Remove(0, 1));
}

[Fact]
public void FindFindMaxValue_returns_max_int()
{
// Arrange
BinaryTree tree = new BinaryTree();
tree.Add(6);
tree.Add(2);
tree.Add(7);
tree.Add(3);
tree.Add(10);
tree.Add(1);
tree.Add(9);
tree.Add(20);

// Act
int result = tree.FindMaxValue(tree.Root);

// Assert
Assert.Equal(20, result);

}


}
}
28 changes: 28 additions & 0 deletions DataStructures/Trees/BinaryTree.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.Transactions;

namespace DataStructures.Trees
{
public class BinaryTree : BinarySearchTree
{
public int FindMaxValue(Node current)
{
if (current == null)
return 0;

int result = current.Value;

int leftResult = FindMaxValue(current.Left);
int rightResult = FindMaxValue(current.Right);

if (leftResult > result)
result = leftResult;
if (rightResult > result)
result = rightResult;

return result;
}
}
}
51 changes: 41 additions & 10 deletions DataStructures/Trees/README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,41 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace DataStructures.Trees
{
class README
{
}
}
# DSA Code Challenges

## Binary Tree
*Author: Enrique Hilst*

---

### Problem Domain

- Create a Node class that has properties for the value stored in the node, the left child node, and the right child node.

- Create a BinaryTree class

- Define a method for each of the depth first traversals called preOrder, inOrder, and postOrder which returns an array of the values, ordered appropriately.

- Any exceptions or errors that come from your code should be semantic, capturable errors. For example, rather than a default error thrown by your language, your code should raise/throw a custom, semantic error that describes what went wrong in calling the methods you wrote for this lab.

- Create a BinarySearchTree class

- Define a method named add that accepts a value, and adds a new node with that value in the correct location in the binary search tree.

- Define a method named contains that accepts a value, and returns a boolean indicating whether or not the value is in the tree at least once.

- Write an instance method called find-maximum-value. Without utilizing any of the built-in methods available to your language, return the maximum value stored in the tree. You can assume that the values stored in the Binary Tree will be numeric.

---

### Big O

| Time | Space |
| :----------- | :----------- |
| O(n) | O(n) |

---

### Visual
[Binary Search Tree](../../Assets/BST.PNG)

[BST Add](../../Assets/BSTAdd.PNG)

[Find Max](../../Assets/BTFindMax.PNG)
48 changes: 2 additions & 46 deletions DataStructures/Trees/Tree.cs
Original file line number Diff line number Diff line change
@@ -1,58 +1,14 @@
using System;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Reflection;
using System.Text;

namespace DataStructures.Trees
{
class Tree
class Tree: BinarySearchTree
{
public Node Root { get; set; }
public int[] PreOrder(Node node)
{
int[] result = new int[20];
int i = 0;
if(node != null)
{
result[i] = node.Value;
i++;
PreOrder(node.Left);
PreOrder(node.Right);
}

return result;
}

public int[] InOrder(Node node)
{
int[] result = new int[20];
int i = 0;
if (node != null)
{
PreOrder(node.Left);
result[i] = node.Value;
i++;
PreOrder(node.Right);
}

return result;
}

public int[] PostOrder(Node node)
{
int[] result = new int[20];
int i = 0;
if (node != null)
{
PreOrder(node.Left);
PreOrder(node.Right);
result[i] = node.Value;
i++;
}

return result;
}
}


}

0 comments on commit ad0720e

Please sign in to comment.