-
-
Save polatengin/10279a044c0b672d5e14ea375b16422c to your computer and use it in GitHub Desktop.
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
C# Binary Search Tree örneği |
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
public void Add(params T[] values) | |
{ | |
if (values == null || !values.Any()) | |
{ | |
throw new ArgumentOutOfRangeException("Please fill values parameter, or it'll throw an exception!"); | |
} | |
var _values = new List<T>(values); | |
var middle = _values.OrderBy(e => e).Skip(_values.Count / 2).First(); | |
_values.Remove(middle); | |
_values.Insert(0, middle); | |
foreach (var value in values) | |
{ | |
if (this.Top == null) | |
{ | |
Top = new Node<T>() { Value = value }; | |
continue; | |
} | |
var current = this.Top; | |
var added = false; | |
do | |
{ | |
if (Comparer<T>.Default.Compare(value, current.Value) == -1) | |
{ | |
// go for left | |
if (current.Left == null) | |
{ | |
current.Left = new Node<T>() { Value = value }; | |
added = true; | |
} | |
else | |
{ | |
current = current.Left; | |
} | |
} | |
else | |
{ | |
// go for right | |
if (current.Right == null) | |
{ | |
current.Right = new Node<T>() { Value = value }; | |
added = true; | |
} | |
else | |
{ | |
current = current.Right; | |
} | |
} | |
} while (!added); | |
} | |
} |
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
// Add(5, 8, 3); | |
public void Add(params T[] value) | |
{ | |
} |
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
// Add(5); | |
public void Add(T value) | |
{ | |
} |
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
// Add(new int[] { 5, 8, 3 }); | |
public void Add(T[] value) | |
{ | |
} |
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
dotnet new console |
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
foreach (var value in values) | |
{ | |
if (this.Top == null) | |
{ | |
Top = new Node<T>() { Value = value }; | |
continue; | |
} | |
var current = this.Top; | |
var added = false; | |
do | |
{ | |
if (Comparer<T>.Default.Compare(value, current.Value) == -1) | |
{ | |
// go for left | |
if (current.Left == null) | |
{ | |
current.Left = new Node<T>() { Value = value }; | |
added = true; | |
} | |
else | |
{ | |
current = current.Left; | |
} | |
} | |
else | |
{ | |
// go for right | |
if (current.Right == null) | |
{ | |
current.Right = new Node<T>() { Value = value }; | |
added = true; | |
} | |
else | |
{ | |
current = current.Right; | |
} | |
} | |
} while (!added); | |
} |
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
public class Node | |
{ | |
public int Value { get; set; } | |
public Node Left { get; set; } | |
public Node Right { get; set; } | |
} |
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
public class Node<T> | |
{ | |
public T Value { get; set; } | |
public Node<T> Left { get; set; } | |
public Node<T> Right { get; set; } | |
} |
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
var _values = new List<T>(values); | |
var middle = _values.OrderBy(e => e).Skip(_values.Count / 2).First(); | |
_values.Remove(middle); | |
_values.Insert(0, middle); |
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
public class Tree<T> | |
{ | |
public Node<T> Top { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment