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
95 additions
and
56 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,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; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -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) |
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 |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
|
||
} |