Exemplo n.º 1
0
        private void StartBtn_Click(object sender, EventArgs e)
        {
            string expresion = ecTxt.Text; //Get the text from the form

            // expresion = InfToPref.Infijo2PrefijoTxt(expresion); //If user wants a prefix expresion, it can uncomment this and use it
            // expresion = infToPostParser.ConvertirPosFija(expresion).ToString();//If user wants a postfix expresion, it can uncomment this and use it

            TBinarySTree bt = generatesTree(expresion);                //Generates us a binary tree with binaryTree library

            Console.WriteLine(bt.drawTree() + "\n");                   //Writes to console the final tree on string form

            Console.WriteLine(treeToGraphviz(bt));                     //Writes to console the result gotten from the converter of tree to graphviz

            var fileName = "graph.txt";                                //Create a string for the txt file name

            SaveToFile(bt, fileName);                                  //We save the file

            System.Diagnostics.Process.Start("notepad.exe", fileName); //Opens a txt file with the graphviz result

            string path = Directory.GetCurrentDirectory();             //Gets the current path

            GenerateGraph(fileName, path);                             //Generates the actual jpg from the tree string

            var graphFileName = "graph.jpg";                           //Create a string for the jpg file name

            System.Diagnostics.Process.Start(graphFileName);           //Opens a jpg file with the graphviz generated tree
        }
Exemplo n.º 2
0
        //Method to save all data to a txt file
        private static void SaveToFile(TBinarySTree bt, string fileName)
        {
            TextWriter textWriter = new StreamWriter(fileName); //Create a Textwriter var

            textWriter.WriteLine(treeToGraphviz(bt));           //Writes to txt file the body content

            textWriter.Close();                                 //Close the TextWriter function
        }
Exemplo n.º 3
0
        // This method is used to transform the tree string into an actual graphviz
        public static string treeToGraphviz(TBinarySTree tree)
        {
            StringBuilder stringBuilder = new StringBuilder();         //Create a stringbuilder var

            stringBuilder.Append("digraph G {" + Environment.NewLine); //Append the graphviz prefix to initialize the txt file

            stringBuilder.Append(toGraphviz(tree.GetRoot()));          //Here we fill the actual content of the txt graphviz with the toGraphviz method

            stringBuilder.Append("}");                                 //Closing curly braces to indicate the end of a graphviz file

            return(stringBuilder.ToString());                          //return as string the result of our stringbuilder var
        }
Exemplo n.º 4
0
        //A method that gets the original expresion and parses it to a binary tree
        public static TBinarySTree generatesTree(string expresion)
        {
            TBinarySTree binaryTree = new TBinarySTree(); //Create a TbinarySTree var

            //Inserts each one of the expresion's values
            for (int i = 0; i < expresion.Length; i++)
            {
                binaryTree.insert("" + expresion[i], expresion[i]);
            }

            return(binaryTree);
        }
Exemplo n.º 5
0
        public List <StoredData> searchByDateA2(List <StoredData> sortedListToSearchA2, DateTime dateToFind)
        {
            int countOfRepetitions = 0; // use this to hold the number of repetitions your algorithm has to make to search the data
            //#####################################################################################################
            //Place your second search algorithm below here - the results must be in the List 'searchResults'
            //This will be displayed on the right-hand-side of the console window
            //#####################################################################################################
            List <StoredData> searchResults = new List <StoredData>();

            //Binary Tree
            //Average case: O(log n)
            //Worst case: O(n)



            TBinarySTree <DateTime> bt = new TBinarySTree <DateTime>();


            //insert the data into the tree
            for (int i = 0; i < sortedListToSearchA2.Count; i++)
            {
                bt.insert(sortedListToSearchA2[i].TxDate, i);
            }

            // Retrieve data from the tree with its index number
            TTreeNode symbol = bt.findSymbol(dateToFind);

            if (symbol != null)
            {
                searchResults.Add(sortedListToSearchA2[symbol.value]);
            }

            else if (symbol == null) //if date cannot be found, adds an empty StoredData object to results list
            {
                StoredData newObj = new StoredData("Not Found", DateTime.Now, 0, 0, 0, 0);
                searchResults.Add(newObj);
            }

            countOfRepetitions = SearchAlgorithms.GlobalCounter;

            //#####################################################################################################
            //Place your second search algorithm above here - the results must be in the List 'searchResults'
            //#####################################################################################################
            // *** your search result data should be placed in 'searchResults'
            searchResults.First().SearchTypeAndTime = "Using 'Binary Tree Search A2' search by date"; //rewrite the text to show user which algorithm you have used
            // place your total count of loops (countOfRepetitions) here
            searchResults.First().CountRepetitions = countOfRepetitions;                              //rewrite to show user the number of steps (loops) this algorithm has made to sort the data
            return(searchResults);
        }
Exemplo n.º 6
0
 public StringBinaryTree()
 {
     BT = new TBinarySTree <Type, TBinarySTree <string, TBinarySTree <string, List <Guid> > > >(compareType);
 }
Exemplo n.º 7
0
 public IntBinaryTree()
 {
     BT = new TBinarySTree <string, TBinarySTree <string, TBinarySTree <int, List <Guid> > > >(compareString);
 }
Exemplo n.º 8
0
        // This is the code that was used to generate the performance
        // graphs in the codeproject article.
        static void Main(string[] args)
        {
            Console.WriteLine("Start Tests");
            PerformanceTimer pt     = new PerformanceTimer();
            Random           random = new Random();
            TBinarySTree     bt;
            Hashtable        ht;

            int[] dataSizeArray = new int[22] {
                1000, 5000, 10000, 20000, 30000,
                40000, 50000, 60000, 80000,
                100000, 120000, 140000, 160000, 180000,
                200000, 250000, 300000, 400000, 500000,
                750000, 1000000, 1000000
            };
            const int numberOfIntervals = 22;

            string[]   values = new string[10000000];            // Max number, ever
            TextWriter tw     = new StreamWriter(@"D:\NET\eLearning\BinaryTree\test.txt");

            try {
                // Number of trials in a particular interval run
                int trials = 20000;

                tw.WriteLine("Start Tests, number of intervals: {0}\n", numberOfIntervals);
                tw.WriteLine("Number of trials in each interval: {0}\n", trials);
                for (int nn = 0; nn < numberOfIntervals; nn++)
                {
                    Console.WriteLine("\nNumber of symbols to insert {0}", dataSizeArray[nn]);

                    // Generate the keys that will stored in the tables
                    for (int i = 0; i < dataSizeArray[nn]; i++)
                    {
                        values[i] = randomString(random, 10, true);
                    }

                    double meanBt = 0;                      // Mean time for binary search tree
                    double meanHt = 0;                      // Mean time for hash table
                    int    index;

                    double[] timeBt = new double[trials];
                    double[] timeHt = new double[trials];

                    // Insert data into binary search tree
                    bt = new TBinarySTree();
                    for (int i = 0; i < dataSizeArray[nn]; i++)
                    {
                        bt.insert(values[i], i);
                    }

                    // Insert data into hash table
                    ht = new Hashtable();
                    for (int i = 0; i < dataSizeArray[nn]; i++)
                    {
                        ht.Add(values[i], i.ToString());
                    }

                    // Data in place, ready to time retrieval.

                    // Retrieve data from a tree/table 'trial times' Eg
                    // retrieve 20,000 times (trials) from a
                    // binary tree that contains 500,000 items (dataSizeArray)
                    for (int k = 0; k < trials; k++)
                    {
                        if (k % 4000 == 0)
                        {
                            Console.WriteLine("{0}", k.ToString());
                        }
                        // Pick a value at random from the value array, 0
                        // to the number of values in the dataSizeArray array.
                        // Eg, assume nn = 5th trial, dataSizeArray[4] = 30000
                        // We pick a number from a random location in values[0->30000]
                        // and time how long to takes to retrieve it.
                        // For each interval we store all the trial times, then
                        // comput their average and standard deviation.

                        // Binary Search Tree
                        index = random.Next(dataSizeArray[nn]);
                        pt.Start();
                        bt.findSymbol(values[index]);
                        pt.Stop();
                        timeBt[k] = pt.DurationSeconds;

                        // Hash Table
                        index = random.Next(dataSizeArray[nn]);
                        pt.Start();
                        ht[values[index]].ToString();
                        pt.Stop();
                        timeHt[k] = pt.DurationSeconds;
                    }
                    // Compute the mean time
                    for (int i = 0; i < trials; i++)
                    {
                        meanBt = meanBt + timeBt[i];
                        meanHt = meanHt + timeHt[i];
                    }
                    meanBt = meanBt / trials;
                    meanHt = meanHt / trials;

                    Console.WriteLine();
                    Console.WriteLine("\nAverage Time for Binary Tree = {0}", meanBt);
                    // Compute standard deviation
                    double sd = 0;
                    for (int i = 0; i < trials; i++)
                    {
                        sd = sd + (timeBt[i] - meanBt) * (timeBt[i] - meanBt);
                    }
                    sd = Math.Sqrt(sd / trials);
                    // CV = coefficient of variation
                    Console.WriteLine("Standard deviation = {0}, CV = {1}", sd, sd / meanBt);

                    Console.WriteLine("\nAverage time for Hash Table = {0}", meanHt);
                    sd = 0;
                    for (int i = 0; i < trials; i++)
                    {
                        sd = sd + (timeHt[i] - meanHt) * (timeHt[i] - meanHt);
                    }
                    sd = Math.Sqrt(sd / trials);
                    Console.WriteLine("Standard deviation = {0}, CV = {1}", sd, sd / meanHt);

                    tw.WriteLine("{0} {1} {2}", dataSizeArray[nn], meanBt, meanHt);
                }
            } finally {
                tw.Close();
            }

            // Deletion tests
            Console.WriteLine("Test Deletion method\n");

            bt = new TBinarySTree();
            bt.insert("50", 50);
            bt.insert("60", 60);
            bt.insert("40", 40);
            bt.insert("30", 30);
            bt.insert("20", 20);
            bt.insert("35", 35);
            bt.insert("45", 45);
            bt.insert("44", 44);
            bt.insert("46", 46);
            Console.WriteLine("Number of nodes in the tree = {0}\n", bt.count());

            Console.WriteLine("Original: " + bt.drawTree());
            bt.delete("40");
            Console.WriteLine("Delete node 40: " + bt.drawTree());
            bt.delete("45");
            Console.WriteLine("Delete node 45: " + bt.drawTree());

            Console.WriteLine("\nSimple one layered tree");
            bt = new TBinarySTree();
            bt.insert("50", 50);
            bt.insert("20", 20);
            bt.insert("90", 90);
            Console.WriteLine("\nOriginal: " + bt.drawTree());
            bt.delete("50");
            Console.WriteLine("Delete node 50: " + bt.drawTree());

            bt = new TBinarySTree();
            bt.insert("50", 50);
            bt.insert("20", 20);
            bt.insert("90", 90);
            Console.WriteLine("\nOriginal: " + bt.drawTree());
            bt.delete("20");
            Console.WriteLine("Delete node 20: " + bt.drawTree());

            bt = new TBinarySTree();
            bt.insert("50", 50);
            bt.insert("20", 20);
            bt.insert("90", 90);
            Console.WriteLine("\nOriginal: " + bt.drawTree());
            bt.delete("90");
            Console.WriteLine("Delete node 90: " + bt.drawTree());
            bt.delete("20");
            Console.WriteLine("Delete node 20: " + bt.drawTree());
            bt.delete("50");
            Console.WriteLine("Delete node 50: " + bt.drawTree());

            Console.WriteLine("\n");
            bt = new TBinarySTree();
            bt.insert("L", 1);
            bt.insert("D", 2);
            bt.insert("C", 3);
            bt.insert("A", 4);
            bt.insert("H", 5);
            bt.insert("F", 6);
            bt.insert("J", 7);
            bt.insert("P", 8);
            Console.WriteLine("Original: " + bt.drawTree());
            bt.delete("J");
            Console.WriteLine("Delete J: " + bt.drawTree());
            bt.delete("C");
            Console.WriteLine("Delete C: " + bt.drawTree());
            bt.delete("L");
            Console.WriteLine("Delete L: " + bt.drawTree());
            bt.delete("D");
            Console.WriteLine("Delete D: " + bt.drawTree());
            bt.delete("A");
            Console.WriteLine("Delete A: " + bt.drawTree());

            Console.ReadLine();
        }