-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTreeSort.cs
More file actions
91 lines (82 loc) · 3.15 KB
/
TreeSort.cs
File metadata and controls
91 lines (82 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System;
using System.Diagnostics;
namespace SortingAlgorithmTime
{
class TreeSort
{
private static Tree node; //private so only accessible in this class
private static int[] nums;
public static void Sort(int[] numbers)
{
nums = new int[numbers.Length];
for (int num = 0; num < numbers.Length; num++)
{
nums[num] = numbers[num];
}
Stopwatch StopWatch = new Stopwatch(); //create and start timer
StopWatch.Start();
node = new Tree(nums[0]); //assigns first array value as root node
for (int i = 1; i < nums.Length; i++)
{
AddNode(nums[i]); //adds a new node, 1 by 1
}
StopWatch.Stop(); //stops timer
TimeSpan time = StopWatch.Elapsed;
Console.Write("\n" + time.TotalMilliseconds + " "); //output time
//Traverse(node); //prints out values
}
private static void AddNode(int value)
{
Tree temp = node; //node to replace root node so it's not lost.
while (true)
{
if (value < temp.Value) //repeats until added
{
if (temp.Left == null) //checks not empty
{
temp.Left = new Tree(value);
return; //added, so now can return
}
else
{
temp = temp.Left; //repeates with left node
}
}
else
{ //greater than or equal
if (temp.Right == null)
{
temp.Right = new Tree(value);
return;
}
else
{
temp = temp.Right;
}
}
}
}
private static void Traverse(Tree tree)
{
if (tree.Left != null) //checkes left is assigned
{
Traverse(tree.Left); //then visits that node if not
}
Console.Write(tree.Value + " "); //then writes this value
if (tree.Right != null) //repeats with right side
{
Traverse(tree.Right);
}
}
}
class Tree
{
public int Value;
public Tree Left;
public Tree Right;
public Tree(int val)
{
Value = val;
}
}
}