Ejemplo n.º 1
0
        public void InOrder_IntegerTest()
        {
            //arrange
            AvlTree <int, int> avl = new AvlTree <int, int>();

            //act
            for (int i = 0; i < intInput.Length; i++)
            {
                avl.Insert(intInput[i], i);
            }
            string result   = avl.InOrder();
            string expected = "6 7 11 12 14 15 16 18 26 46 58 62 63 65 66 ";

            //assert
            Assert.Equal(expected, result);
        }
Ejemplo n.º 2
0
        public void AvlTree_Balance_Test()
        {
            AvlTree <int> Tree = new AvlTree <int>((a, b) => { return(a < b); });

            Tree.Add(5);
            Tree.Add(4);
            Tree.Add(3);
            Tree.Add(2);
            Tree.Add(1);
            Tree.Add(0);
            Tree.Dump();

            AvlTree <int> .NodeCheck
                RootCheck           = new AvlTree <int> .NodeCheck(),
                Check_Left1         = new AvlTree <int> .NodeCheck(),
                Check_Right1        = new AvlTree <int> .NodeCheck(),
                Check_Left1_Left2   = new AvlTree <int> .NodeCheck(),
                Check_Right1_Left2  = new AvlTree <int> .NodeCheck(),
                Check_Right1_Right2 = new AvlTree <int> .NodeCheck()
            ;

            // This is an explicit structural test
            RootCheck.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(2, nodeCheck.Value);

                Assert.AreEqual(0, nodeCheck.ParentId);
                Assert.AreNotEqual(0, nodeCheck.LeftId);
                Assert.AreNotEqual(0, nodeCheck.RightId);
                return(true);
            };
            RootCheck.NextCheck = Check_Left1;

            Check_Left1.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(1, nodeCheck.Value);

                Assert.AreEqual(RootCheck.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(RootCheck.LeftId, nodeCheck.ThisId);
                Assert.Greater(RootCheck.Value, nodeCheck.Value);
                Assert.AreNotEqual(0, nodeCheck.LeftId); // should be left node
                Assert.AreEqual(0, nodeCheck.RightId);   // but no right node.
                return(true);
            };
            Check_Left1.NextCheck = Check_Left1_Left2;

            Check_Left1_Left2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(0, nodeCheck.Value);

                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.AreEqual(Check_Left1.ThisId, nodeCheck.ParentId);
                Assert.Greater(Check_Left1.Value, nodeCheck.Value);
                return(true);
            };
            Check_Left1_Left2.NextCheck = Check_Right1;

            Check_Right1.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(4, nodeCheck.Value);

                Assert.AreEqual(RootCheck.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(RootCheck.RightId, nodeCheck.ThisId);
                Assert.AreNotEqual(0, nodeCheck.LeftId);
                Assert.AreNotEqual(0, nodeCheck.RightId);
                Assert.Less(RootCheck.Value, nodeCheck.Value);
                return(true);
            };
            Check_Right1.NextCheck = Check_Right1_Left2;

            Check_Right1_Left2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(3, nodeCheck.Value);

                Assert.AreEqual(Check_Right1.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(Check_Right1.LeftId, nodeCheck.ThisId);
                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.Greater(Check_Right1.Value, nodeCheck.Value);
                return(true);
            };
            Check_Right1_Left2.NextCheck = Check_Right1_Right2;

            Check_Right1_Right2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(5, nodeCheck.Value);

                Assert.AreEqual(Check_Right1.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(Check_Right1.RightId, nodeCheck.ThisId);
                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.Less(Check_Right1.Value, nodeCheck.Value);
                return(false); // end of tree
            };

            Tree.HierarchyCheck(RootCheck);
            Console.WriteLine("\r\n: Tree Looks Like:");
            Tree.InOrder((num) => { Console.WriteLine("> {0}", num); });
        }
Ejemplo n.º 3
0
        public void AvlTree_Balance_Test()
        {
            AvlTree<int> Tree = new AvlTree<int>((a, b) => { return a < b; });

            Tree.Add(5);
            Tree.Add(4);
            Tree.Add(3);
            Tree.Add(2);
            Tree.Add(1);
            Tree.Add(0);
            Tree.Dump();

            AvlTree<int>.NodeCheck
                RootCheck = new AvlTree<int>.NodeCheck(),
                Check_Left1 = new AvlTree<int>.NodeCheck(),
                Check_Right1 = new AvlTree<int>.NodeCheck(),
                Check_Left1_Left2 = new AvlTree<int>.NodeCheck(),
                Check_Right1_Left2 = new AvlTree<int>.NodeCheck(),
                Check_Right1_Right2 = new AvlTree<int>.NodeCheck()
                ;
            // This is an explicit structural test
            RootCheck.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(2, nodeCheck.Value);

                Assert.AreEqual(0, nodeCheck.ParentId);
                Assert.AreNotEqual(0, nodeCheck.LeftId);
                Assert.AreNotEqual(0, nodeCheck.RightId);
                return true;
            };
            RootCheck.NextCheck = Check_Left1;

            Check_Left1.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(1, nodeCheck.Value);

                Assert.AreEqual(RootCheck.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(RootCheck.LeftId, nodeCheck.ThisId);
                Assert.Greater(RootCheck.Value, nodeCheck.Value);
                Assert.AreNotEqual(0, nodeCheck.LeftId); // should be left node
                Assert.AreEqual(0, nodeCheck.RightId); // but no right node.
                return true;
            };
            Check_Left1.NextCheck = Check_Left1_Left2;

            Check_Left1_Left2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(0, nodeCheck.Value);

                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.AreEqual(Check_Left1.ThisId, nodeCheck.ParentId);
                Assert.Greater(Check_Left1.Value, nodeCheck.Value);
                return true;
            };
            Check_Left1_Left2.NextCheck = Check_Right1;

            Check_Right1.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(4, nodeCheck.Value);

                Assert.AreEqual(RootCheck.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(RootCheck.RightId, nodeCheck.ThisId);
                Assert.AreNotEqual(0, nodeCheck.LeftId);
                Assert.AreNotEqual(0, nodeCheck.RightId);
                Assert.Less(RootCheck.Value, nodeCheck.Value);
                return true;
            };
            Check_Right1.NextCheck = Check_Right1_Left2;

            Check_Right1_Left2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(3, nodeCheck.Value);

                Assert.AreEqual(Check_Right1.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(Check_Right1.LeftId, nodeCheck.ThisId);
                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.Greater(Check_Right1.Value, nodeCheck.Value);
                return true;
            };
            Check_Right1_Left2.NextCheck = Check_Right1_Right2;

            Check_Right1_Right2.Verify = (nodeCheck) =>
            {
                Console.WriteLine(nodeCheck);
                Assert.AreEqual(5, nodeCheck.Value);

                Assert.AreEqual(Check_Right1.ThisId, nodeCheck.ParentId);
                Assert.AreEqual(Check_Right1.RightId, nodeCheck.ThisId);
                Assert.AreEqual(0, nodeCheck.LeftId);
                Assert.AreEqual(0, nodeCheck.RightId);
                Assert.Less(Check_Right1.Value, nodeCheck.Value);
                return false; // end of tree
            };

            Tree.HierarchyCheck(RootCheck);
            Console.WriteLine("\r\n: Tree Looks Like:");
            Tree.InOrder((num) => { Console.WriteLine("> {0}", num); });
        }
Ejemplo n.º 4
0
        private static void TestAVLTree(int[] myArray)
        {
            AvlTree <int, int> avl = new AvlTree <int, int>();

            Console.WriteLine("\nBALANCED! Time O(log n) Space O(n)");

            for (int i = 0; i < myArray.Length; i++)
            {
                avl.Insert(myArray[i], i);
            }

            Console.WriteLine("\nIn Order:\n" + avl.InOrder());
            Console.WriteLine("\nPos Order:\n" + avl.PosOrder());
            Console.WriteLine("\nPre Order:\n" + avl.PreOrder());

            Node <int, int> node   = null;
            int             objInt = 0;// "nada";

            if ((node = avl.Search(objInt)) != null)
            {
                Console.WriteLine("\nThe Value of " + objInt + " key: " + node.Key + " synonymous of " + objInt + ": " + node.Value + " index: " + node.Index);
            }
            else
            {
                Console.WriteLine("\nIt was not possible to find the " + objInt + " key in the dataset.");
            }

            objInt = 9992;//"nada";
            if ((node = avl.Search(objInt)) != null)
            {
                Console.WriteLine("\nThe Value of " + objInt + " key: " + node.Value);
            }
            else
            {
                Console.WriteLine("\nIt was not possible to find the " + objInt + " key in the dataset. Did you remove it?\n");
            }

            Console.WriteLine("\nkey " + objInt + ": " + avl.AvlLog.ToString());

            //NODE IS A LEAF
            if (avl.Remove(objInt))
            {
                Console.WriteLine("\nkey " + objInt + " was successfully removed.");
            }
            else
            {
                Console.WriteLine("\nkey " + objInt + " was not found.");
            }
            if ((node = avl.Search(objInt)) != null)
            {
                Console.WriteLine("\nThe Value of " + objInt + " key: " + node.Value + " your remove has failed.");
            }
            else
            {
                Console.WriteLine("\nIt was not possible to find the " + objInt + " key in the dataset. Did you remove it?\n");
            }

            //NODE HAS 1 SUBTREE
            objInt = 9204; //"arroz";//
            if (avl.Remove(objInt))
            {
                Console.WriteLine("\nkey " + objInt + " was successfully removed.");
            }
            else
            {
                Console.WriteLine("\nkey " + objInt + " was not found.");
            }
            if ((node = avl.Search(objInt)) != null)
            {
                Console.WriteLine("\nThe Value of " + objInt + " key: " + node.Value + " your remove has failed.");
            }
            else
            {
                Console.WriteLine("\nIt was not possible to find the " + objInt + " key in the dataset. Did you remove it?\n");
            }

            //NODE HAS 2 SUBTREE
            objInt = 18;// "muito";//
            if (avl.Remove(objInt))
            {
                Console.WriteLine("\nkey " + objInt + " was successfully removed.");
            }
            else
            {
                Console.WriteLine("\nkey " + objInt + " was not found.");
            }

            if ((node = avl.Search(objInt)) != null)
            {
                Console.WriteLine("\nThe Value of " + objInt + " key: " + node.Value + " your remove has failed.");
            }
            else
            {
                Console.WriteLine("\nIt was not possible to find the " + objInt + " key in the dataset. Did you remove it?\n");
            }

            //NODE HAS 2 SUBTREE AND IS ROOT
            objInt = 15; //"mamão";//
            if (avl.Remove(objInt))
            {
                Console.WriteLine("\nkey " + objInt + " was successfully removed.");
            }
            else
            {
                Console.WriteLine("\nkey " + objInt + " was not found.");
            }

            if ((node = avl.Search(objInt)) != null)
            {
                Console.WriteLine("\nThe Value of " + objInt + " key: " + node.Value + " your remove has failed.");
            }
            else
            {
                Console.WriteLine("\nIt was not possible to find the " + objInt + " key in the dataset. Did you remove it?\n");
            }

            Console.WriteLine("\nIn Order:\n" + avl.InOrder());
        }