public void Question_4_6_BasicCases() { // Single Node tree tests. var tree = TreeHelpers.CreateBinaryTree(1, null, null); Validate(null, tree); // Small tree tests. tree = TreeHelpers.CreateBinaryTree(1, 0, 2); Validate(2, tree); Validate(1, tree.Left); Validate(null, tree.Right); // Large tree tests. tree = TreeHelpers.CreateBinaryTree( data: 4, left: TreeHelpers.CreateBinaryTree( data: 2, left: TreeHelpers.CreateBinaryTree(1, 0, -1), right: new BinaryTreeNode <int>(3)), right: TreeHelpers.CreateBinaryTree(6, 5, 7)); Validate(5, tree); Validate(3, tree.Left); Validate(-1, tree.Left.Left); Validate(4, tree.Left.Right); Validate(7, tree.Right); Validate(6, tree.Right.Left); Validate(null, tree.Right.Right); }
public void Question_4_4_BasicCases() { // One node tree var tree = TreeHelpers.CreateBinaryTree(1, null, null); Validate(true, tree); // Two level tree tree = TreeHelpers.CreateBinaryTree(1, 2, 0); Validate(true, tree); // Two node uneven tree. tree = TreeHelpers.CreateBinaryTree( 1, TreeHelpers.CreateBinaryTree(2, new BinaryTreeNode <int>(3), null), null); Validate(false, tree); // Non BST very uneven. var temp1 = TreeHelpers.CreateBinaryTree(0, 1, 2); var temp2 = TreeHelpers.CreateBinaryTree(4, 5, 6); var temp3 = TreeHelpers.CreateBinaryTree(11, temp1, temp2); tree = TreeHelpers.CreateBinaryTree(8, temp3, null); Validate(false, tree); }
public void Question_4_8_BasicCases() { // Single Node tree tests. var tree = TreeHelpers.CreateBinaryTree(1, null, null); Validate(tree, tree, tree); // Small tree tests. tree = TreeHelpers.CreateBinaryTree(1, 0, 2); Validate(tree, tree, tree.Left); Validate(tree, tree, tree.Right); Validate(tree, tree.Left, tree.Right); Validate(tree, tree.Right, tree.Left); Validate(tree.Right, tree.Right, tree.Right); Validate(tree.Left, tree.Left, tree.Left); // Large tree tests. tree = TreeHelpers.CreateBinaryTree( data: 4, left: TreeHelpers.CreateBinaryTree( data: 2, left: TreeHelpers.CreateBinaryTree(1, 0, -1), right: TreeHelpers.CreateBinaryTree(10, 11, 12)), right: TreeHelpers.CreateBinaryTree(6, 5, 7)); Validate(tree, tree.Left.Left, tree.Right.Right); Validate(tree, tree.Left.Left.Left, tree.Right.Right); Validate(tree, tree.Left.Right, tree.Right.Left); Validate(tree.Left, tree.Left.Left.Left, tree.Left.Right.Right); }
public void Question_4_12_BasicCases() { // Empty tree. BinaryTreeNode <int> tree = null; Validate(0, tree, 2); Validate(0, tree, 0); // Single Node tree tests. tree = TreeHelpers.CreateBinaryTree(0, null, null); Validate(0, tree, 1); Validate(1, tree, 0); // Small tree; tree = TreeHelpers.CreateBinaryTree(1, 0, 2); Validate(2, tree, 1); Validate(1, tree, 0); Validate(1, tree, 2); Validate(1, tree, 3); Validate(0, tree, 4); Validate(0, tree, -1); // Awkward lopsided tree. // 0 // \ // 1 // \ // 2 // ... tree = TreeHelpers.CreateBinaryTreeFromArray(0, 1, 2, 3, 4, 5); Validate(2, tree, 1); Validate(2, tree, 5); Validate(2, tree, 15); Validate(3, tree, 3); Validate(1, tree, 4); Validate(0, tree, 8); // Bigger tree // 3 // / \ // 1 5 // / \ / \ // 2 0 4 6 tree = TreeHelpers.CreateBinaryTreeFromArray <int>(3, 1, 0, 2, 5, 4, 6); Validate(1, tree, 5); Validate(2, tree, 6); Validate(1, tree, 12); Validate(3, tree, 4); Validate(1, tree, 14); Validate(2, tree, 1); Validate(1, tree, 2); }
public void Question_4_5_BasicCases() { // One node tree var tree = TreeHelpers.CreateBinaryTree(1, null, null); Validate(true, tree); // Invalid two node tree tree = TreeHelpers.CreateBinaryTree(1, new BinaryTreeNode <int>(2), null); Validate(false, tree); // Valid two node tree tree = TreeHelpers.CreateBinaryTree(1, null, new BinaryTreeNode <int>(2)); Validate(true, tree); // Valid three node tree tree = TreeHelpers.CreateBinaryTree(1, 0, 2); Validate(true, tree); // Larger invalid tree tree = TreeHelpers.CreateBinaryTree( 1, TreeHelpers.CreateBinaryTree(0, -1, 2), new BinaryTreeNode <int>(3)); Validate(false, tree); // Larger valid tree tree = TreeHelpers.CreateBinaryTree( 1, TreeHelpers.CreateBinaryTree(-1, -2, 0), new BinaryTreeNode <int>(3)); Validate(true, tree); // Even larger invalid tree. var temp1 = TreeHelpers.CreateBinaryTree(1, 0, 2); var temp2 = TreeHelpers.CreateBinaryTree(5, 4, 6); var temp3 = TreeHelpers.CreateBinaryTree(7, temp1, temp2); tree = TreeHelpers.CreateBinaryTree(8, temp3, null); Validate(false, tree); // Even larger valid tree. temp1 = TreeHelpers.CreateBinaryTree(1, 0, 2); temp2 = TreeHelpers.CreateBinaryTree(6, 5, 7); temp3 = TreeHelpers.CreateBinaryTree(4, temp1, temp2); tree = TreeHelpers.CreateBinaryTree(8, temp3, null); Validate(true, tree); }
public void Question_4_10_BasicCases() { // Null tree var t1 = (BinaryTreeNode <int>)null; var t2 = t1; Validate(t1, t2, expected: true); t2 = new BinaryTreeNode <int>(1); Validate(t1, t2, expected: false); // Single Node tree tests. t1 = TreeHelpers.CreateBinaryTree(1, null, null); t2 = (BinaryTreeNode <int>)null; Validate(t1, t2, expected: true); t2 = new BinaryTreeNode <int>(1); Validate(t1, t2, expected: true); t2 = new BinaryTreeNode <int>(2); Validate(t1, t2, expected: false); // Small tree tests. t1 = TreeHelpers.CreateBinaryTree(1, new BinaryTreeNode <int>(0), null); t2 = t1.Left; Validate(t1, t2, expected: true); Validate(t1, new BinaryTreeNode <int>(4), expected: false); Validate(t1, TreeHelpers.CreateBinaryTree(1, 0, 0), expected: false); t2 = TreeHelpers.CreateBinaryTree(1, new BinaryTreeNode <int>(0), null); Validate(t1, t2, expected: true); // Larger tree tests t1 = TreeHelpers.CreateBinaryTree(9, new BinaryTreeNode <int>(8), t1); t2 = TreeHelpers.CreateBinaryTree(9, new BinaryTreeNode <int>(8), t2); Validate(t1, t2, expected: true); Validate(t1, t2.Left, expected: true); Validate(t1, t2.Right, expected: true); Validate(t1, t2.Right.Right, expected: true); Validate(t1, TreeHelpers.CreateBinaryTree(8, new BinaryTreeNode <int>(9), null), expected: false); // Medium tree tests t1 = TreeHelpers.CreateBinaryTree( data: 4, left: TreeHelpers.CreateBinaryTree(2, 1, 10), right: TreeHelpers.CreateBinaryTree(6, 5, 7)); t2 = TreeHelpers.CreateBinaryTree(2, 1, 10); Validate(t1, t2, expected: true); Validate(t1, TreeHelpers.CreateBinaryTree(5, 6, 7), expected: false); }
public void Question_4_11_BasicCases() { // Single Node tree tests. var tree = TreeHelpers.CreateBinaryTree(0, null, null); Validate(tree); // Small tree; tree = TreeHelpers.CreateBinaryTree(1, 0, 2); Validate(tree); // Awkward tree. tree = TreeHelpers.CreateBinaryTreeFromArray(0, 1, 2, 3, 4, 5); Validate(tree); // Bigger tree tree = TreeHelpers.CreateBinaryTreeFromArray <int>(3, 1, 0, 2, 5, 4, 6); Validate(tree); }
public void Question_4_3_BasicCases() { // One node tree var tree = TreeHelpers.CreateBinaryTree(1, null, null); var expected = new List <List <int> >() { ListHelpers.CreateList(1) }; Validate(expected, tree); // Two level tree tree = TreeHelpers.CreateBinaryTree(1, 2, 0); expected = new List <List <int> >() { ListHelpers.CreateList(1), ListHelpers.CreateList(2, 0) }; Validate(expected, tree); // Two level tree var temp1 = TreeHelpers.CreateBinaryTree(0, 1, 2); var temp2 = TreeHelpers.CreateBinaryTree(4, 5, 6); var temp3 = TreeHelpers.CreateBinaryTree(11, temp1, temp2); var temp4 = TreeHelpers.CreateBinaryTree(8, temp3, null); expected = new List <List <int> >() { ListHelpers.CreateList(8), ListHelpers.CreateList(11), ListHelpers.CreateList(0, 4), ListHelpers.CreateList(1, 2, 5, 6), }; Validate(expected, temp4); }
public void Question_4_9_BasicCases() { // Null tree var tree = (BinaryTreeNode <int>)null; var expected = new List <List <int> >(); Validate(tree, expected); // Single Node tree tests. tree = TreeHelpers.CreateBinaryTree(1, null, null); expected = new List <List <int> >() { new List <int>() { 1 } }; Validate(tree, expected); // Small tree tests. tree = TreeHelpers.CreateBinaryTree(1, new BinaryTreeNode <int>(0), null); expected = new List <List <int> >() { new List <int>() { 1, 0 } }; Validate(tree, expected); // Small tree tests. tree = TreeHelpers.CreateBinaryTree(1, 0, 2); expected = new List <List <int> >() { new List <int>() { 1, 0, 2 }, new List <int>() { 1, 2, 0 } }; Validate(tree, expected); // Unbalanced tree // Small tree tests. tree = TreeHelpers.CreateBinaryTree(1, new BinaryTreeNode <int>(0), TreeHelpers.CreateBinaryTree(4, 2, 5)); expected = new List <List <int> >() { new List <int>() { 1, 0, 4, 2, 5 }, new List <int>() { 1, 0, 4, 5, 2 }, new List <int>() { 1, 4, 2, 5, 0 }, new List <int>() { 1, 4, 5, 2, 0 } }; Validate(tree, expected); // Medium tree tests tree = TreeHelpers.CreateBinaryTree( data: 4, left: TreeHelpers.CreateBinaryTree(2, 1, 10), right: TreeHelpers.CreateBinaryTree(6, 5, 7)); expected = new List <List <int> >() { new List <int>() { 4, 2, 1, 10, 6, 5, 7 }, new List <int>() { 4, 2, 10, 1, 6, 5, 7 }, new List <int>() { 4, 2, 1, 10, 6, 7, 5 }, new List <int>() { 4, 2, 10, 1, 6, 7, 5 }, new List <int>() { 4, 6, 5, 7, 2, 1, 10 }, new List <int>() { 4, 6, 7, 5, 2, 1, 10 }, new List <int>() { 4, 6, 5, 7, 2, 10, 1 }, new List <int>() { 4, 6, 7, 5, 2, 10, 1 } }; Validate(tree, expected); }
public void Question_4_2_BasicCases() { // Single node tree test. var binaryTree = TreeHelpers.CreateBinaryTree(data: 0); this.Validate <int>(1, binaryTree); // Two node tree // Both trees valid: 1 0 // / or \ // 0 1 // This test is specific to the implementation. binaryTree = TreeHelpers.CreateBinaryTree( data: 1, left: new BinaryTreeNode <int>(0), right: null); this.Validate <int>(2, binaryTree); // Thee node tree binaryTree = TreeHelpers.CreateBinaryTree(data: 1, left: 0, right: 2); this.Validate <int>(3, binaryTree); // Four node tree binaryTree = TreeHelpers.CreateBinaryTree( data: 2, left: TreeHelpers.CreateBinaryTree( data: 1, left: new BinaryTreeNode <int>(0), right: null), right: new BinaryTreeNode <int>(3)); this.Validate <int>(4, binaryTree); // Five node tree binaryTree = TreeHelpers.CreateBinaryTree( data: 2, left: TreeHelpers.CreateBinaryTree( data: 1, left: new BinaryTreeNode <int>(0), right: null), right: TreeHelpers.CreateBinaryTree( data: 4, left: new BinaryTreeNode <int>(3), right: null)); this.Validate <int>(5, binaryTree); // Six node tree binaryTree = TreeHelpers.CreateBinaryTree( data: 3, left: TreeHelpers.CreateBinaryTree( data: 1, left: 0, right: 2), right: TreeHelpers.CreateBinaryTree( data: 5, left: new BinaryTreeNode <int>(4), right: null)); this.Validate <int>(6, binaryTree); // Seven node tree binaryTree = TreeHelpers.CreateBinaryTree( data: 3, left: TreeHelpers.CreateBinaryTree( data: 1, left: 0, right: 2), right: TreeHelpers.CreateBinaryTree( data: 5, left: 4, right: 6)); this.Validate <int>(7, binaryTree); // Eight node tree binaryTree = TreeHelpers.CreateBinaryTree( data: 4, left: TreeHelpers.CreateBinaryTree( data: 2, left: TreeHelpers.CreateBinaryTree( data: 1, left: new BinaryTreeNode <int>(0), right: null), right: new BinaryTreeNode <int>(3)), right: TreeHelpers.CreateBinaryTree(6, 5, 7)); this.Validate <int>(8, binaryTree); }