GithubHelp home page GithubHelp logo

binarysearchtree's Introduction

BinarySearchTree

This repo contains the Binary Search Tree data structure. It allows for fast lookup and addition of items, and can be used to implement either dynamic sets of items, or lookup tables that allow finding an item by its key (e.g., finding the phone number of a person by name).

Binary search tree is a rooted binary tree, whose internal nodes each store a value and each have two distinguished sub-trees, denoted Left and Right. The tree additionally satisfies the binary search tree property, which states that the value in each node must be greater than all values stored in the left sub-tree, and smaller than all values in the right sub-tree. (The leaves (final nodes) of the tree contain no value and have no structure to distinguish them from one another. Leaves are represented by null.)

Average Worst Case
Space Θ(n) O(n)
Search Θ(log n) O(n)
Insert Θ(log n) O(n)
Delete Θ(log n) O(n^2)

Example

 static Random random = new Random((int)DateTime.Now.Ticks);
 private void stringTest(int totalValues)
 {
     var tree = new BinaryNodeTree<string>();
     
     var stringArray = new string[totalValues];

     var rnd = new Random();

     for (int i = 0; i < totalValues; i++)
     {
         stringArray[i] = RandomString(rnd.Next(1,15));

         Debug.WriteLine(stringArray[i]);

         tree.Insert(stringArray[i]);
     }

     Debug.WriteLine("Tree ToString():");
     Debug.WriteLine(tree.ToString());

     for (int i = 0; i < totalValues; i++)
     {
         if (i > totalValues || i < 0)
             break;

         var indexToRemove = totalValues - i - 1;
         Debug.WriteLine("");
         Debug.WriteLine($"Remove {stringArray[indexToRemove]}");

         tree.Remove(stringArray[indexToRemove]);

         Debug.WriteLine("Tree ToString():");
         Debug.WriteLine(tree.ToString());
     }
 }

 private static void intTest(int totalValues)
 {
     var tree = new BinaryNodeTree<int>();

     int randomNumber;
     var intArray = new int[totalValues];

     var rnd = new Random();
     for (int i = 0; i < totalValues; i++)
     {
         randomNumber = rnd.Next(1, 100);

         intArray[i] = randomNumber;

         Debug.WriteLine(intArray[i]);

         tree.Insert(intArray[i]);
     }
     Debug.WriteLine("Tree ToString():");
     Debug.WriteLine(tree.ToString());

     for (int i = 0; i < totalValues; i++)
     {
         if (i > totalValues || i < 0)
             break;

         var indexToRemove = totalValues - i - 1;
         Debug.WriteLine("");
         Debug.WriteLine($"Remove {intArray[indexToRemove]}");

         tree.Remove(intArray[indexToRemove]);

         Debug.WriteLine("Tree ToString():");
         Debug.WriteLine(tree.ToString());
     }
 }

 private static string RandomString(int size)
 {
     StringBuilder builder = new StringBuilder();
     char ch;
     for (int i = 0; i < size; i++)
     {
         ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
         builder.Append(ch);
     }

     return builder.ToString();
 }

binarysearchtree's People

Contributors

brminnick avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

fredyfx pacohams

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.