static void Main(string[] args) { BST b = new BST(); for (int i = 0; i < 10; i++) { b.Add(i); } }
//recursively remove one random string from words list and tree, then test alphabetic order static void RecursiveTest(BST tree, List<string> words) { if (words.Count > 0) { //test alphabetic order first, throws error if not alphabetized TestAlphabeticOrder(tree); //remove a random element from the tree and word list int random = new Random().Next(0, words.Count); string remove = words[random]; words.RemoveAt(random); tree.Delete(remove); RecursiveTest(tree, words); } }
static void Main(string[] args) { //create a tree //source: http://msdn.microsoft.com/en-us/library/aa289150(v=vs.71).aspx BST tree = new BST(); //split a phrase by space characters List<string> words = new List<string>("buy a motorcycle and go fishing".Split(' ')); words.Remove(String.Empty); //phrase is not alphabetized. insert into tree foreach (string word in words) { tree.Add(word); } try { RecursiveTest(tree, words); Console.WriteLine("Test succeeded"); } catch (Exception) { Console.WriteLine("Test failed"); } }
//compare ascending alpha order for every string, inorder traversal static void TestAlphabeticOrder(BST tree) { //traverse the tree in order List<string> sorted = new List<string>(InorderTraversal(tree.Root).Split('\n')); sorted.Remove(String.Empty); //test the alphabetic order //get the first sting to compare string previous = sorted[0]; //from the second to the end, compare against the previous for (int i = 1; i < sorted.Count; i++) { //get another string to compare string compare = sorted[i]; //if previous and current are out of order, throw an exception if (compare.CompareTo(previous) < 0) { throw new Exception("Failed test"); } //else, keep comparing and move the previous forward by one previous = compare; } }
static void Main(string[] args) { HuffmanTree<string> tree = new HuffmanTree<string>(); BST index = new BST(); //string rhyme = "Jack, be nimble, Jack, be quick, Jack, jump over the candlestick. Jack jumped high. Jack jumped low. Jack jumped over and burned his toe."; Dictionary<string,long> codes = new Dictionary<string,long>(); codes.Add("Jack", 0); codes.Add("jumped", 10); codes.Add("over", 110); codes.Add("be", 1110); codes.Add("nimble", 11110); codes.Add("quick", 111110); codes.Add("jumped", 1111110); codes.Add("the", 11111110); codes.Add("candlestick", 111111110); codes.Add("high", 1111111110); codes.Add("low", 11111111110); codes.Add("and", 111111111110); codes.Add("burned", 1111111111110); codes.Add("toe", 11111111111110); codes.Add("his", 111111111111110); }
public virtual object Clone() { BST clone = new BST(); clone.root = (Node) root.Clone(); clone.count = this.count; return clone; }