Advertisement
CGC_Codes

Binary search tree

Jun 2nd, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.92 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4.  
  5. namespace BinarySearchTree
  6. {
  7.     public class Program
  8.     {
  9.         public static void Main(string[] args)
  10.         {
  11.             Node root = null;
  12.             Tree bst = new Tree {Root = null};
  13.  
  14.             const int size = 19;
  15.             //int[] a = new int[size];
  16.             //const int valueToFindIndex = 31;
  17.             int valueToFind =0;
  18.  
  19.             Console.WriteLine($"Generating random array with {size} values ...");
  20.  
  21.             Random random = new Random();
  22.             Stopwatch watch = Stopwatch.StartNew();
  23.             int[] a = { 8, 4, 15, 2, 6, 11, 17, 1, 3, 5, 7, 9, 13, 16, 18, 10, 12, 14, 19 };
  24.  
  25.             //for (int i = 0; i < size; i++)
  26.             //{
  27.             //    int nextNumber = random.Next(19);
  28.             //    while (a.Contains(nextNumber))
  29.             //    {
  30.             //        nextNumber = random.Next(20);
  31.             //    }
  32.             //    a[i] = nextNumber;
  33.             //    //if (i == valueToFindIndex)
  34.             //    //{
  35.             //    //    valueToFind = a[i];
  36.             //    //}
  37.             //}
  38.  
  39.             watch.Stop();
  40.  
  41.             Console.WriteLine($"Done. Took {watch.ElapsedMilliseconds / 1000.0} seconds");
  42.             Console.WriteLine();
  43.             Console.WriteLine($"Filling the tree with {size} nodes ...");
  44.  
  45.             watch = Stopwatch.StartNew();
  46.  
  47.             for (int i = 0; i < size; i++)
  48.             {
  49.                bst.Root = bst.Insert(bst.Root, a[i], null);
  50.             }
  51.  
  52.             watch.Stop();
  53.  
  54.             Console.WriteLine($"Done. Took {watch.ElapsedMilliseconds / 1000.0} seconds");
  55.             Console.WriteLine();
  56.             Console.WriteLine($"Traversing all {size} nodes in the tree ...");
  57.  
  58.             watch = Stopwatch.StartNew();
  59.  
  60.             bst.Traverse(bst.Root);
  61.  
  62.             watch.Stop();
  63.  
  64.             Console.WriteLine($"Done. Took {watch.ElapsedMilliseconds / 1000.0} seconds");
  65.             Console.WriteLine();
  66.             Console.WriteLine($"Verifying if the binary tree is valid ...");
  67.  
  68.             watch = Stopwatch.StartNew();
  69.  
  70.             bool valid = bst.IsValid(bst.Root);
  71.  
  72.             watch.Stop();
  73.  
  74.             string validationResult = valid ? "valid" : "invalid";
  75.             Console.WriteLine($"Done. Took {watch.ElapsedMilliseconds / 1000.0} seconds to validate that the tree is {validationResult}");
  76.             Console.WriteLine();
  77.  
  78.             bst.RemoveNode(13);
  79.             //bst.RemoveNode(6);
  80.             //valueToFind = 1231231231;
  81.             Node foundNode = bst.FindNode(valueToFind, bst.Root);
  82.  
  83.             string findResult = foundNode != null
  84.                 ? $"Node found: value - {foundNode.Value}, left value {foundNode.Left?.Value}, right value {foundNode.Right?.Value} "
  85.                 : "Node not found";
  86.             Console.WriteLine(findResult);
  87.  
  88.             Console.ReadKey();
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement