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); }); }
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); }); }