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 newList = new BinarySearchTree<string>(); | |
newList.Add("purple"); | |
Console.WriteLine(newList.ToString() | |
); | |
} | |
} | |
public class Node<T> where T : IComparable<T> | |
{ | |
public T Value { get; set; } | |
public Node<T> Right { get; set; } | |
public Node<T> Left { get; set; } | |
public bool Printed { get; set; } | |
public int Count { get; set; } | |
} | |
public class BinarySearchTree<T> where T : IComparable<T> | |
{ | |
private Node<T> Root { get; set; } | |
public Node<T> Current { get; set; } | |
public void Add(T value) | |
{ | |
var pointer = Current; | |
//Need some way to use Add recursively. Must start at root, but must also be able to interact with a mutable node selector | |
//If this is a new tree, set the pointer at Root | |
if (Root == null) | |
{ | |
pointer = Root; | |
} | |
//If the pointer is not on a null node, compare the value being added to the value currently present | |
if (pointer != null) | |
{ | |
//This var will hold one of three options, as defined by IComparable: less than zero, zero, or greater than zero | |
var compared = Compared(value, pointer); | |
if (compared == 0) { Current.Count += 1; } | |
if (compared == -1) { Current = pointer.Left; } | |
if (compared == +1) { Current = pointer.Right; } | |
} | |
//If the pointer is on a null node, add data to that node. This should allow Add to be called recursively. | |
else | |
{ | |
pointer.Value = value; | |
} | |
} | |
public int Compared(T value, Node<T> node) | |
{ | |
return value.CompareTo(node.Value); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment