Hi, for a project I have to read in a text file and using a "tree sort" i have to sort and count all of the words. I have the first part working but im not sure how to create this tree sort... all i know is that is similar to the quick sort.

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ReadIn
{
    class Program
    {
        static void Main(string[] args)
        {
            TextReader reader = new StreamReader("H:/Visual Studio 2005/Projects/ReadIn/ReadIn/bin/Debug/soccer.txt");//declares a streamreader to read text file

            string line;//declares string where text file will function at
            string words = "";//declares a blank string
            int length;//declares an int to count word length
            while ((line = reader.ReadLine()) != null)
            {
                int count = 0;
                length = line.Length;
                for (int n = 0; n < length; n++)
                {
                    int test;
                    test = (int)line[n];
                    if ((test<=90)&(test>=65)|(test<=122)&(test>=97)|(test==37)|(test==45))
                    {
                        words += line[n];
                        count += 1;
                    }
                    else
                    {
                        if ((words != "") & (count>1))
                        {
                            Console.WriteLine(words.ToLower());
                        }
                        words = "";
                    }
                }
                if (words != "")
                {
                    Console.WriteLine(words.ToLower());
                }
                words = "";
            } 
            reader.Close();
            
            Console.ReadKey();
        }
    }

    class Tree
    {
        string word;
        int count, left, right;
        public Tree()
        {
            word = null;
            count = 0;
        }
        public void initwords(Tree xBranch, string newWord)
        {
            Console.WriteLine("New Branch");
            Console.WriteLine("Word: {0,-10) New: (1,-10)", xBranch.word, newWord);
            if(words )??????????
            {??????????/
???????????
            }
        }
    }
}

Recommended Answers

All 9 Replies

You can use simpler code to count the number of words

string textData = File.ReadAllText(filePath);
int count = textData.Split(' ').Count;

Read in the algorithms and understand it well, you'll apply it smoothly... or you can search in the implementation (but I can't recommend that)

Thanks for responding but you didnt understand. I have to sort all the words I have seperated in the text file (thats in the first class). Then I have to sort all of these words and count how many times they appear in the text file. So if I had a text file saying:
"bobby crossed the road and fell on the road" the output would be:
and 1
bobby 1
crossed 1
fell 1
on 1
road 2
the 2

thanks

Something like this:

static void Main(string[] args)
        {
            string text = "bobby crossed the road and fell on the road"; //File.ReadAllText("C:\\SomeFile.txt");
            char[] separator = new char[1] { ' ' };
            string[] textData = text.Split(separator, StringSplitOptions.RemoveEmptyEntries); 
            Array.Sort(textData);
            textData = DoWordCounts(textData);
            foreach (string str in textData)
                 Console.WriteLine(str.ToLower());
            Console.ReadKey();
        }

        private string[] DoWordCounts(string[] value)
        {
            List<string> sb = new List<string>();
            string word = string.Empty;
            int cnt = 0;
            for (int i = 0; i < value.Length; i++)
            {
                if (value[i] != word)
                {
                    if (sb.Count > 0)
                        sb[sb.Count - 1] += " " + cnt.ToString();
                    word = value[i];
                    cnt = 1;
                    sb.Add(word);
                }
                else
                    cnt++;
            }
            if (sb.Count > 0)
                sb[sb.Count - 1] += " " + cnt.ToString();
            return sb.ToArray();
        }

// Jerry

I don't know really why people don't ask their questions in true meaning!!

"tree sort" i have to sort and count all of the words.

according to the question no one can debate my answer is wrong!!
To the asker: You should say count the repetition, duplication, recurrence for each word in the text file not to say the count of words I think the difference between two questions likes the difference between sky and earth.

Jerry, first thanks for great help. But Array.Sort() implements Quick Sort algorithm not Tree Sort algorithm.

Ramy,
I was under the same impression that you were, that he just wanted a count of all the words, his reply showed what he really wanted.

Agreed, that a Quick sort is not as efficient or reliable, but since we brought the whole text into a variable in both yours and my examples instead of streaming the data throug a Tree sort algo, we already shot efficiency and the memory foot-print in the foot.

Do you have a tree sort algo up your sleeve that can work with a stream ?

// Cheers, Jerry

I'm fairly new to C# and one of my projects is this very tree sort. I have not been able to find a tree sort algorithm anywhere, not even a pseudocode algorithm. Any help will be greatly appreciated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.