public void TestGetLowestCommonAncestor() { BinaryTree testTree = LowestCommonAncestor.GenerateTestTree(); // __[1]__ // / \ // / \ // [2] [9] // / \ \ // [3] [8] [10] // / \ / \ // [4] [6] [11] [12] // \ \ / \ \ // [5] [7] [13] [14] [16] // / / // [15] [17] Assert.AreEqual(testTree.GetLowestCommonAncestor(1, 1).GetVal(), 1, "Lowest common ancestor of the same nodes should be that node"); Assert.AreEqual(testTree.GetLowestCommonAncestor(3, 7).GetVal(), 3, "Lowest common ancestor of a pair of nodes a b where a is an ancestor of b should be a"); Assert.AreEqual(testTree.GetLowestCommonAncestor(13, 14).GetVal(), 11, "LCA of a pair of arbitrary nodes should return the correct result"); Assert.AreEqual(testTree.GetLowestCommonAncestor(17, 5).GetVal(), 1, "Method should still work if the LCA is the root"); }
public void TestInsertInBinaryTree() { BinaryTree testTree = new BinaryTree(null); testTree.Insert(5); Assert.AreEqual(testTree.GetRoot().GetVal(), 5, "A value inserted into a binary tree with no root should become the root"); testTree.Insert(6); Assert.AreEqual(testTree.GetRoot().GetLChild().GetVal(), 6, "Insertion for a node with no children should make it the lchild"); testTree.Insert(7); Assert.AreEqual(testTree.GetRoot().GetRChild().GetVal(), 7, "Insertion for a node with an lchild should make the new value the rchild"); testTree.Insert(8); Assert.AreEqual(testTree.GetRoot().GetLChild().GetLChild().GetVal(), 8, "Insertion to a tree that has a full bottom row should see the value inserted in the leftmost position on the next row"); // __[1]__ // / \ // / \ // [2] [9] // / \ \ // [3] [8] [10] // / \ / \ // [4] [6] [11] [12] // \ \ / \ \ // [5] [7] [13] [14] [16] // / / // [15] [17] testTree = LowestCommonAncestor.GenerateTestTree(); testTree.Insert(18); Assert.AreEqual(testTree.GetRoot().GetRChild().GetLChild().GetVal(), 18, "Insertion into a binary tree with random shape should go to the correct spot"); }