Untitled

 avatar
unknown
plain_text
4 years ago
3.3 kB
6
Indexable
 static void readFile(string fileName)
        {
            const int MAX_FILE_LINES = 50000;
            string[] AllLines = new string[MAX_FILE_LINES];

            AVLTree<String> tree = new AVLTree<string>();

            //reads from bin/DEBUG subdirectory of project directory
            AllLines = File.ReadAllLines(fileName);

            //Create a dictionary to store the unique words and how many times they occur.
            var wordDict = new Dictionary<string, int>();
                
            foreach (string line in AllLines)
            {
                //split words using space , . ?
                string[] words = line.Split(' ', ',', '.', '?', ';', ':', '!');
                foreach (string word in words)
                {
                    if (!string.IsNullOrEmpty(word))
                    {
                        //Remove leading and trailing spaces, and convert the word to lower case.
                        var lowerCaseWord = word.Trim().ToLower();

                        //  Console.WriteLine(word.ToLower());
                        tree.InsertItem(lowerCaseWord);

                        //If the word already exists, increase the count by 1, if not add the new word.
                        if (wordDict.ContainsKey(lowerCaseWord))
                        {
                            wordDict[lowerCaseWord] += 1;
                        }
                        else
                        { 
                            wordDict.Add(lowerCaseWord, 1);
                        }
                    }
                }
            }


class AVLTree<T> : BSTree<T> where T : IComparable
    {
        private void calculateBalance(ref Node<T> tree)
        {
            tree.BalanceFactor = Height(tree.Left) - Height(tree.Right);
        }
        public new void InsertItem(T item)
        {
            insertItem(item, ref root);
        }

        private void insertItem(T item, ref Node<T> tree)
        {
            if (tree == null)
                tree = new Node<T>(item);
            else if (item.CompareTo(tree.Data) < 0) 
                insertItem(item, ref tree.Left);
            else if (item.CompareTo(tree.Data) > 0)
                insertItem(item, ref tree.Right);
            tree.BalanceFactor = Height(tree.Left) - Height(tree.Right);
            if (tree.BalanceFactor <= -2)
                rotateLeft(ref tree);
            if (tree.BalanceFactor >= 2)
                rotateRight(ref tree);
        }

        private void rotateLeft(ref Node<T> tree)
        {
            if (tree.Right.BalanceFactor > 0)  //double rotate
                rotateRight(ref tree.Right);
            Node<T> newRoot = tree.Right;
            Node<T> tempNode = tree.Left;

            newRoot.Left = tree;
            tree.Right = tempNode;

            tree = newRoot;
        }



        private void rotateRight(ref Node<T> tree)
        {
            if (tree.Left.BalanceFactor < 0)
                rotateLeft(ref tree.Left);
            Node<T> newRoot = tree.Left;
            Node<T> tempNode = newRoot.Right;

            newRoot.Right = tree;
            tree.Left = tempNode;

            tree = newRoot;

        }
Editor is loading...