-
-
Save sefatanam/ff6cbeef617e1cb88d5d1b0208427dca to your computer and use it in GitHub Desktop.
Binary search tree implementation in C#
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.Diagnostics; | |
namespace BinarySearchTree | |
{ | |
class Node | |
{ | |
public int value; | |
public Node left; | |
public Node right; | |
} | |
class Tree | |
{ | |
public Node insert(Node root, int v) | |
{ | |
if (root == null) | |
{ | |
root = new Node(); | |
root.value = v; | |
} | |
else if (v < root.value) | |
{ | |
root.left = insert(root.left, v); | |
} | |
else | |
{ | |
root.right = insert(root.right, v); | |
} | |
return root; | |
} | |
public void traverse(Node root) | |
{ | |
if (root == null) | |
{ | |
return; | |
} | |
traverse(root.left); | |
traverse(root.right); | |
} | |
} | |
class BinarySearchTree | |
{ | |
static void Main(string[] args) | |
{ | |
Node root = null; | |
Tree bst = new Tree(); | |
int SIZE = 2000000; | |
int[] a = new int[SIZE]; | |
Console.WriteLine("Generating random array with {0} values...", SIZE); | |
Random random = new Random(); | |
Stopwatch watch = Stopwatch.StartNew(); | |
for (int i = 0; i < SIZE; i++) | |
{ | |
a[i] = random.Next(10000); | |
} | |
watch.Stop(); | |
Console.WriteLine("Done. Took {0} seconds", (double)watch.ElapsedMilliseconds / 1000.0); | |
Console.WriteLine(); | |
Console.WriteLine("Filling the tree with {0} nodes...", SIZE); | |
watch = Stopwatch.StartNew(); | |
for (int i = 0; i < SIZE; i++) | |
{ | |
root = bst.insert(root, a[i]); | |
} | |
watch.Stop(); | |
Console.WriteLine("Done. Took {0} seconds", (double)watch.ElapsedMilliseconds / 1000.0); | |
Console.WriteLine(); | |
Console.WriteLine("Traversing all {0} nodes in tree...", SIZE); | |
watch = Stopwatch.StartNew(); | |
bst.traverse(root); | |
watch.Stop(); | |
Console.WriteLine("Done. Took {0} seconds", (double)watch.ElapsedMilliseconds / 1000.0); | |
Console.WriteLine(); | |
Console.ReadKey(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment