public void CheckLenghtListOfNodesAfterInsert()
 {
     RedBlackTree.RedBlackTree tree = new RedBlackTree.RedBlackTree(12);
     tree.InsertNode(8);
     tree.InsertNode(17);
     Assert.AreEqual(3, tree.GetAllNodes().Count);
 }
 public void CheckForNodeExistenceWhenDeleting()
 {
     RedBlackTree.RedBlackTree tree = new RedBlackTree.RedBlackTree(12);
     tree.InsertNode(8);
     tree.InsertNode(17);
     Assert.AreEqual(false, tree.DeleteNode(5));
     Assert.AreEqual(true, tree.DeleteNode(8));
 }
 public void DeleteOneNodeRoot()
 {
     RedBlackTree.RedBlackTree tree = new RedBlackTree.RedBlackTree(12);
     tree.DeleteNode(12);
     Assert.AreEqual(false, tree.DeleteNode(11));
     Assert.AreEqual(true, tree.InsertNode(14));
     Assert.AreEqual(1, tree.GetAllNodes().Count);
     Assert.AreEqual(tree.FindNodeByValue(14), tree.GetRoot());
 }
        public void CheckRedNodesAfterInsert()
        {
            RedBlackTree.RedBlackTree tree = new RedBlackTree.RedBlackTree(12);
            tree.InsertNode(8);
            tree.InsertNode(17);
            tree.InsertNode(1);
            tree.InsertNode(11);
            tree.InsertNode(15);
            tree.InsertNode(25);
            foreach (var node in tree.GetAllNodes())
            {
                var res = true;
                if (node.GetColor() == Color.Red)
                {
                    res = node.GetLeft().IsNil() && node.GetRight().IsNil() ||
                          node.GetRight().GetColor() == Color.Black && node.GetLeft().GetColor() == Color.Black;
                }

                Assert.AreEqual(true, res, "Red parent can have only black child");
            }
        }
Example #5
0
        static void Main(string[] args)
        {
            //int[] nums = { 21, 16, 22, 7, 5, 1, 12, 18, 2, 10, 0, 4, 23, 24, 8, 15, 19, 20, 6, 3, 13, 14, 9, 11, 17 };
            //int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 };
            int[] nums = { 24, 23, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
            //int[] nums = { 4, 6, 7, 15, 1, 8, 2, 18, 11, 20, 19, 3, 16, 14, 5, 9, 10, 13, 17, 12 };
            var tree = new RedBlackTree<int>();

            Console.Write("Wild list:\n    ");
            foreach (var n in nums) {
                tree.Add(n);
                Console.Write(n + " ");
            }

            Console.Write("\n\nInOrder:\n   ");
            foreach (var i in tree)
                Console.Write(" {0}", i);

            Console.Write("\n\nWrite tree:\n{0}", tree.ToText());

            Console.ReadKey();
        }
Example #6
0
 public static void OrderCheck(RedBlackTree <Guid, int> tree)
 {
     Console.WriteLine(tree.OrderCheck());
 }
Example #7
0
 public static void CheckRedNode(RedBlackTree <Guid, int> tree)
 {
     Console.WriteLine("红色节点测试{0}", tree.CheckRedNode());
 }
Example #8
0
 public static void BlackDeepCheck(RedBlackTree <Guid, int> tree)
 {
     Console.WriteLine("黑色深度测试{0}", tree.BlackDeepCheck());
 }
Example #9
0
 void InitTree()
 {
     tree = new RedBlackTree(3);
     tree.AddNode(5);
 }
Example #10
0
 public Enumerator(RedBlackTree <T> bTree)
 {
     this.bTree       = bTree;
     nodesToEnumerate = new Stack <Node <T> >();
     PushNode(bTree.root);
 }
Example #11
0
 public NodeAdder(RedBlackTree <TValue> tree)
 {
     this.tree = tree;
 }
Example #12
0
        static void Main(string[] args)
        {
            RedBlackTree <int> myRBT    = new RedBlackTree <int>();
            String             input    = "";
            RBTNode <int>      currNode = null;

            while (input != "done")
            {
                Console.WriteLine();
                Console.WriteLine("Enter a command");
                input = Console.ReadLine();

                switch (input)
                {
                case "add":
                    Console.WriteLine("Enter data to be added to tree");
                    myRBT.Add(new RBTNode <int>(int.Parse(Console.ReadLine())));
                    break;

                case "print":
                    myRBT.Print();
                    break;

                case "remove":
                    Console.WriteLine("Enter data to be removed from tree");
                    myRBT.Remove(new RBTNode <int>(int.Parse(Console.ReadLine())));
                    break;

                case "root":
                    currNode = (RBTNode <int>)myRBT.Root;
                    Console.WriteLine(currNode.Data);
                    break;

                case "right":
                    try
                    {
                        currNode = (RBTNode <int>)currNode.RightChild;
                    }
                    catch (NullReferenceException e) { Console.WriteLine(currNode.Data + " does not have a right child!"); }
                    Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    break;

                case "left":
                    try {
                        currNode = (RBTNode <int>)currNode.LeftChild;
                        Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    }
                    catch (NullReferenceException e) { Console.WriteLine(currNode.Data + " does not have a left child!"); }
                    break;

                case "parent":
                    try
                    {
                        currNode = (RBTNode <int>)currNode.Parent;
                        Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    }
                    catch (NullReferenceException e) { Console.WriteLine(currNode.Data + " does not have a parent!"); }
                    break;

                case "curr":
                    Console.WriteLine(currNode.Data + " " + (currNode as RBTNode <int>).Color);
                    break;
                }
            }
        }
Example #13
0
        static void Main(string[] args)
        {
            try
            {
                bool   flag          = true;
                string pathSource    = string.Empty;
                string newPathSource = string.Empty;
                string name          = string.Empty;

                RedBlackTree RBTree = new RedBlackTree();

                while (flag)
                {
                    //Console.Clear();
                    // Start menu:
                    Console.WriteLine("WELCOME TO RED_BLACK_TREE PROGRAMM");
                    Console.WriteLine("Menu:");
                    Console.WriteLine("'lex'  -  load example file");
                    Console.WriteLine("'delw' -  удалить слово из дерева");
                    Console.WriteLine("'addw' -  добавить слово в дерево");
                    Console.WriteLine("'addf' -  добавить слова в дерево из файла");
                    Console.WriteLine("'search' -  найти слово");
                    Console.WriteLine("'clear' - очистить дерево");
                    Console.WriteLine("'exit' - for exit");

                    string input = Console.ReadLine();

                    switch (input)
                    {
                    case "lex":
                    case "LEX":
                        pathSource = @"E:\Polytech\TGraph\Самобалансирующиеся деревья\__FILES\HellGirl.txt";

                        using (StreamReader sr = new StreamReader(pathSource, Encoding.Default))
                        {
                            try
                            {
                                List <string> Words = sr.Words();

                                RBTree = new RedBlackTree();

                                foreach (string w in Words)
                                {
                                    RBTree.AddData(w);
                                }

                                if (RBTree.Count < 50)
                                {
                                    RBTree.PrintTreeConsoleVertical();
                                }
                                else
                                {
                                    RBTree.PrintTreeConsoleHorizontal();
                                }

                                Console.WriteLine();
                            }
                            catch (Exception el)
                            {
                                if ((el as FormatException) != null)
                                {
                                    Console.WriteLine("Incorrect format in file");
                                }
                                else
                                {
                                    Console.WriteLine(el.Message);
                                }
                            }
                        }
                        Console.WriteLine("Press any key for continue....");
                        Console.ReadLine();
                        break;

                    case "delw":
                    case "DELW":
                        try
                        {
                            Console.WriteLine("Deletion. Input word: ");

                            string oldWord = Console.ReadLine();
                            oldWord = oldWord.ToLowerInvariant();

                            if (!RBTree.SearchWord(oldWord))
                            {
                                Console.WriteLine("Такого слова нет в словаре!");
                            }
                            else
                            {
                                RBTree.DeleteData(oldWord);
                            }

                            if (RBTree.Count < 50)
                            {
                                RBTree.PrintTreeConsoleVertical();
                            }
                            else
                            {
                                RBTree.PrintTreeConsoleHorizontal();
                            }

                            Console.WriteLine();
                        }
                        catch (Exception el)
                        {
                            Console.WriteLine(el.Message);
                        }
                        Console.WriteLine("Press any key for continue....");
                        Console.ReadLine();
                        break;

                    case "addw":
                    case "ADDW":
                        try
                        {
                            Console.WriteLine("Добавление слова: ");

                            string newWord = Console.ReadLine();
                            newWord = newWord.ToLowerInvariant();

                            if (RBTree.SearchWord(newWord))
                            {
                                Console.WriteLine("Слово уже есть в словаре!");
                            }
                            else
                            {
                                RBTree.AddData(newWord);
                            }

                            if (RBTree.Count < 50)
                            {
                                RBTree.PrintTreeConsoleVertical();
                            }
                            else
                            {
                                RBTree.PrintTreeConsoleHorizontal();
                            }

                            Console.WriteLine();
                        }
                        catch (Exception el)
                        {
                            Console.WriteLine(el.Message);
                        }
                        Console.WriteLine("Press any key for continue....");
                        Console.ReadLine();
                        break;

                    case "addf":
                    case "ADDF":
                        Console.WriteLine("Введите имя файла (из папки __FILES):");
                        string filename = Console.ReadLine();
                        newPathSource = @"E:\Polytech\TGraph\Самобалансирующиеся деревья\__FILES\" + filename;

                        using (StreamReader sr = new StreamReader(newPathSource, Encoding.Default))
                        {
                            try
                            {
                                List <string> Words = sr.Words();

                                //RBTree = new RedBlackTree();

                                foreach (string w in Words)
                                {
                                    RBTree.AddData(w);
                                }

                                if (RBTree.Count < 50)
                                {
                                    RBTree.PrintTreeConsoleVertical();
                                }
                                else
                                {
                                    RBTree.PrintTreeConsoleHorizontal();
                                }

                                Console.WriteLine();
                            }
                            catch (Exception el)
                            {
                                if ((el as FormatException) != null)
                                {
                                    Console.WriteLine("Incorrect format in file");
                                }
                                else
                                {
                                    Console.WriteLine(el.Message);
                                }
                            }
                        }
                        Console.WriteLine("Press any key for continue....");
                        Console.ReadLine();
                        break;

                    case "search":
                    case "SEARCH":
                        try
                        {
                            Console.WriteLine("Введите искомое слово: ");

                            string searchWord = Console.ReadLine();
                            searchWord = searchWord.ToLowerInvariant();


                            if (RBTree.SearchWord(searchWord))
                            {
                                Console.WriteLine("Слово найдено в словаре!");
                            }
                            else
                            {
                                Console.WriteLine("Слово НЕ найдено в словаре!");
                            }

                            if (RBTree.Count < 50)
                            {
                                RBTree.PrintTreeConsoleVertical();
                            }
                            else
                            {
                                RBTree.PrintTreeConsoleHorizontal();
                            }

                            Console.WriteLine();
                        }
                        catch (Exception el)
                        {
                            Console.WriteLine(el.Message);
                        }
                        Console.WriteLine("Press any key for continue....");
                        Console.ReadLine();
                        break;

                    case "clear":
                    case "CLEAR":
                        try
                        {
                            Console.WriteLine("Очистка словаря.");
                            RBTree.Clear();
                            Console.WriteLine("Дерево пусто!");
                            Console.WriteLine();
                        }
                        catch (Exception el)
                        {
                            Console.WriteLine(el.Message);
                        }
                        Console.WriteLine("Press any key for continue....");
                        Console.ReadLine();
                        break;

                    case "exit":
                    case "EXIT":
                        flag = false;
                        break;

                    default:
                        break;
                    }
                }  // End of 'while'
            }
            catch (Exception e) // ловим необработанные в ходе программы исключения
            {
                Console.WriteLine(e.Message + Environment.NewLine + e.StackTrace);
            }
        }