Last active
February 25, 2016 19:47
-
-
Save DeepSky8/147bcd0955ebe368685f to your computer and use it in GitHub Desktop.
BreadthTraverse gets stuck somewhere. When I use the debug feature, Visual Studio hits an error and closes after line 173.
This file contains hidden or 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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace BinaryTree | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var newBST = new BSTNode<string>(); | |
newBST.Add("purple"); | |
newBST.Add("green"); | |
newBST.Add("blue"); | |
} | |
} | |
public class BSTNode<T> where T : IComparable<T> | |
{ | |
public T Value { get; set; } | |
public BSTNode<T> Right { get; set; } | |
public BSTNode<T> Left { get; set; } | |
public bool Printed { get; set; } | |
public int Count { get; set; } = 1; | |
public void Add(T newValue) | |
{ | |
//If this node does not hold a value, set it to the value currently being added | |
if (Value == null) | |
{ | |
Value = newValue; | |
} | |
//If there is a value in the node, compare the new value to the current node value. Either increment the count by 1, or create a child node (left or right) based on how the new value compares to the current node value. | |
else | |
{ | |
//This var will hold one of three options, as defined by IComparable: less than zero, zero, or greater than zero | |
var compared = Value.CompareTo(newValue); | |
if (compared == 0) { Count += 1; } | |
if (compared == -1) { Left = new BSTNode<T>(); Left.Add(newValue); } | |
if (compared == +1) { Right = new BSTNode<T>(); Right.Add(newValue); } | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment