Created
May 29, 2013 01:29
-
-
Save serkansendur/5667374 to your computer and use it in GitHub Desktop.
C# BST implementation.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace ConsoleApplication2 | |
{ | |
class Program | |
{ | |
public class BSTNode<T> where T : IComparable<T> | |
{ | |
public T Data; | |
public BSTNode<T> left; | |
public BSTNode<T> right; | |
public BSTNode(T Data) | |
{ | |
this.Data = Data; | |
} | |
} | |
public class BSTree<T> where T : IComparable<T> | |
{ | |
public BSTNode<T> root; | |
public BSTNode<T> FindNodeByValue(T Data) | |
{ | |
BSTNode<T> cursor = root; | |
while (cursor != null) | |
{ | |
int result = cursor.Data.CompareTo(Data); | |
if (result == 0) | |
break; | |
if (result < 0) | |
cursor = cursor.right; | |
else | |
cursor = cursor.left; | |
} | |
return cursor; | |
} | |
public void Insert(BSTNode<T> node) | |
{ | |
if (root == null) | |
{ | |
root = node; | |
} | |
else | |
{ | |
BSTNode<T> cursor = root; | |
while (cursor != null) | |
{ | |
int result = cursor.Data.CompareTo(node.Data); | |
if (result == 0) | |
break; | |
if (result < 0) | |
{ | |
if (cursor.right != null) | |
cursor = cursor.right; | |
else | |
{ | |
cursor.right = node; | |
break; | |
} | |
} | |
else | |
{ | |
if (cursor.left != null) | |
cursor = cursor.left; | |
else | |
{ | |
cursor.left = node; | |
break; | |
} | |
} | |
} | |
} | |
} | |
public void PreOrderTraverse(BSTNode<T> node) | |
{ | |
if (node != null) | |
{ | |
Console.WriteLine(node.Data); | |
PreOrderTraverse(node.left); | |
PreOrderTraverse(node.right); | |
} | |
} | |
public void PostOrderTraverse(BSTNode<T> node) | |
{ | |
if (node != null) | |
{ | |
PreOrderTraverse(node.left); | |
PreOrderTraverse(node.right); | |
Console.WriteLine(node.Data); | |
} | |
} | |
public void InOrderTraverse(BSTNode<T> node) | |
{ | |
if (node != null) | |
{ | |
PreOrderTraverse(node.left); | |
Console.WriteLine(node.Data); | |
PreOrderTraverse(node.right); | |
} | |
} | |
} | |
static void Main(string[] args) | |
{ | |
BSTree<int> tree = new BSTree<int>(); | |
tree.Insert(new BSTNode<int>(5)); | |
tree.Insert(new BSTNode<int>(10)); | |
tree.Insert(new BSTNode<int>(8)); | |
tree.Insert(new BSTNode<int>(20)); | |
tree.Insert(new BSTNode<int>(2)); | |
tree.Insert(new BSTNode<int>(100)); | |
tree.Insert(new BSTNode<int>(1)); | |
tree.Insert(new BSTNode<int>(3)); | |
tree.Insert(new BSTNode<int>(80)); | |
tree.Insert(new BSTNode<int>(7)); | |
tree.Insert(new BSTNode<int>(19)); | |
tree.Insert(new BSTNode<int>(9)); | |
tree.Insert(new BSTNode<int>(110)); | |
Console.WriteLine("PreOrderTraverse"); | |
tree.PreOrderTraverse(tree.root); | |
Console.WriteLine("PostOrderTraverse"); | |
tree.PostOrderTraverse(tree.root); | |
Console.WriteLine("InOrderTraverse"); | |
tree.InOrderTraverse(tree.root); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment