def test_leftonly(self): """Test a tree with only a left side.""" s = "42(-100(12(10)[11[17]])[23])" root = mfs(s, BTN) self.assertEqual( root.preordertraversal(), [42, -100, 12, 10, 11, 17, 23])
def test_bothchildren(self): """Test a tree which has both children.""" s = "42(-100(12)[28])[100[111]]" root = mfs(s, BTN) self.assertEqual( root.preordertraversal(), [42, -100, 12, 28, 100, 111])
def test_rightonly(self): """Test a tree with only a right side.""" s = "42[-100(12(10)[11(17)])[32]]" root = mfs(s, BTN) self.assertEqual( root.preordertraversal(), [42, -100, 12, 10, 11, 17, 32])
def test_singlecase(self): """Test a tree with just a root.""" s = "42" root = mfs(s, BTN) self.assertEqual( root.preordertraversal(), [42])
def test_singlecase(self): """Test a tree with just a root.""" s = "42" root = mfs(s, BTN) n1 = root.findnode(42) n2 = root.findnode(42) self.assertEqual(lca(n1, n2).val, 42)
def test_bothchildren(self): """Test a tree which has both children.""" s = "42(-100(12)[28])[100(2000(1)[2])[111]]" root = mfs(s, BTN) self.assertEqual( root.findkunbalanceddescendent(1).val, 100) self.assertIsNone(root.findkunbalanceddescendent(2))
def test_differentbranches(self): """Tests when node1 and node2 are are not their LCA.""" s = ( "1(10(11(12(20(21(22[23(24)[25]]))[200]))" + "[13(14[15[16(17(18)[19])]])]))" + "[2[3(33(34)[35])[4(5(6)[7[8(9)]])]]]" ) root = mfs(s, BTN) n1 = root.findnode(34) n2 = root.findnode(9) self.assertEqual(lca(root, n1, n2).val, 3)
def test_verydifferentbranches(self): """Tests when the LCA is the root.""" s = ( "1(10(11(12(20(21(22[23(24)[25]]))[200]))" + "[13(14[15[16(17(18)[19])]])]))" + "[2[3(33(34)[35])[4(5(6)[7[8(9)]])]]]" ) root = mfs(s, BTN) n1 = root.findnode(34) n2 = root.findnode(20) self.assertEqual(lca(n1, n2).val, 1)
def test_node2isLCA(self): """Tests when node2 is the LCA of node1 and node2.""" s = ( "1(10(11(12(20(21(22[23(24)[25]]))[200]))" + "[13(14[15[16(17(18)[19])]])]))" + "[2[3(33(34)[35])[4(5(6)[7[8(9)]])]]]" ) root = mfs(s, BTN) n1 = root.findnode(24) n2 = root.findnode(20) self.assertEqual(lca(n1, n2).val, 20)
def test_singlecase(self): """Test a tree with just a root.""" s = "42" root = mfs(s, BTN) root.fixsize() self.assertEqual( root.find_kth_node(1).val, 42) try: root.find_kth_node(2) except IndexError: pass
def test_rightonly(self): """Test a tree with only a right side.""" s = "42[-100(12(10)[11(999)(17)])[32]]" root = mfs(s, BTN) self.assertEqual( root.findkunbalanceddescendent(1).val, 12) self.assertEqual( root.findkunbalanceddescendent(2).val, -100) self.assertEqual( root.findkunbalanceddescendent(3).val, -100)
def test_strictleftonly(self): """Test a tree with only left children.""" s = "1(2(3(4(5(6)))))" root = mfs(s, BTN) root.fixsize() self.assertEqual( root.find_kth_node(1).val, 6) self.assertEqual( root.find_kth_node(3).val, 4) self.assertEqual( root.find_kth_node(5).val, 2)
def test_exceptionthrow(self): """Tests when the node1 and node2 are in different trees.""" s = ( "1(10(11(12(20(21(22[23(24)[25]]))[200]))" + "[13(14[15[16(17(18)[19])]])]))" + "[2[3(33(34)[35])[4(5(6)[7[8(9)]])]]]" ) root = mfs(s, BTN) n1 = root.findnode(34) n2 = BTN(42) try: lca(n1, n2) except StandardError: pass
def test_strictrightonly(self): """Test a tree with only right children.""" s = "1[2[3[4[5[6]]]]]" root = mfs(s, BTN) root.fixsize() self.assertEqual( root.find_kth_node(1).val, 1) self.assertEqual( root.find_kth_node(3).val, 3) self.assertEqual( root.find_kth_node(5).val, 5)
def test_rightonly(self): """Test a tree with only a right side.""" s = "42[-100(12(10)[11(17)])[32]]" root = mfs(s, BTN) inord = root.inordertraversal() preord = root.preordertraversal() postord = root.postordertraversal() root2 = mft(BTN, inord, postorder=postord) inord2 = root2.inordertraversal() preord2 = root2.preordertraversal() postord2 = root2.postordertraversal() self.assertEqual(inord, inord2) self.assertEqual(preord, preord2) self.assertEqual(postord, postord2)
def test_singlecase(self): """Test a tree with just a root.""" s = "42" root = mfs(s, BTN) inord = root.inordertraversal() preord = root.preordertraversal() postord = root.postordertraversal() root2 = mft(BTN, inord, postorder=postord) inord2 = root2.inordertraversal() preord2 = root2.preordertraversal() postord2 = root2.postordertraversal() self.assertEqual(inord, inord2) self.assertEqual(preord, preord2) self.assertEqual(postord, postord2)
def test_leftonly(self): """Test a tree with only a left side.""" s = "42(-100(12(10)[11[17]])[23])" root = mfs(s, BTN) inord = root.inordertraversal() preord = root.preordertraversal() postord = root.postordertraversal() root2 = mft(BTN, inord, preorder=preord) inord2 = root2.inordertraversal() preord2 = root2.preordertraversal() postord2 = root2.postordertraversal() self.assertEqual(inord, inord2) self.assertEqual(preord, preord2) self.assertEqual(postord, postord2)
def test_singlecase(self): """Test a tree with just a root.""" array = [42, None, None] s = "42" root = mbt(array) root_ = mfs(s, BTN) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.postordertraversal(), root_.postordertraversal())
def test_bothchildren(self): """Test a tree which has both children.""" s = "42(-100(12)[28])[100[111]]" root = mfs(s, BTN) inord = root.inordertraversal() preord = root.preordertraversal() postord = root.postordertraversal() root2 = mft(BTN, inord, preorder=preord) inord2 = root2.inordertraversal() preord2 = root2.preordertraversal() postord2 = root2.postordertraversal() self.assertEqual(inord, inord2) self.assertEqual(preord, preord2) self.assertEqual(postord, postord2)
def test_leftonly(self): """Test a tree with only a left side.""" s = "42(-100(12(10)[11[17[112233]]])[23])" root = mfs(s, BTN) self.assertEqual( root.findkunbalanceddescendent(1).val, 11) self.assertEqual( root.findkunbalanceddescendent(2).val, -100) self.assertEqual( root.findkunbalanceddescendent(3).val, -100) self.assertEqual( root.findkunbalanceddescendent(4).val, 42)
def test_strictrightonly(self): """Test a tree with only right children.""" s = "1[2[3[4[5[6]]]]]" root = mfs(s, BTN) self.assertEqual( root.findkunbalanceddescendent(1).val, 4) self.assertEqual( root.findkunbalanceddescendent(2).val, 3) self.assertEqual( root.findkunbalanceddescendent(3).val, 2) self.assertEqual( root.findkunbalanceddescendent(4).val, 1)
def test_strictleftonly(self): """Test a tree with only left children.""" s = "1(2(3(4(5(6)))))" root = mfs(s, BTN) self.assertEqual( root.findkunbalanceddescendent(1).val, 4) self.assertEqual( root.findkunbalanceddescendent(2).val, 3) self.assertEqual( root.findkunbalanceddescendent(3).val, 2) self.assertEqual( root.findkunbalanceddescendent(4).val, 1)
def test_leftonly(self): """Test a tree with only a left side.""" s = "42(-100(12(10)[11[17[112233]]])[23])" root = mfs(s, BTN) root.fixsize() self.assertEqual( root.find_kth_node(1).val, 10) self.assertEqual( root.find_kth_node(3).val, 11) self.assertEqual( root.find_kth_node(5).val, 112233) self.assertEqual( root.find_kth_node(7).val, 23)
def test_bothchildren(self): """Test a tree which has both children.""" array = [ 42, -100, 12, None, None, 28, None, None, 100, None, 111, None, None] s = "42(-100(12)[28])[100[111]]" root = mbt(array) root_ = mfs(s, BTN) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.postordertraversal(), root_.postordertraversal())
def test_rightonly(self): """Test a tree with only a right side.""" array = [ 42, None, -100, 12, 10, None, None, 11, 17, None, None, None, 32, None, None] s = "42[-100(12(10)[11(17)])[32]]" root = mbt(array) root_ = mfs(s, BTN) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.postordertraversal(), root_.postordertraversal())
def test_rightonly(self): """Test a tree with only a right side.""" s = "42[-100(12(10)[11(999)[17]])[32]]" root = mfs(s, BTN) root.fixsize() self.assertEqual( root.find_kth_node(1).val, 42) self.assertEqual( root.find_kth_node(3).val, 12) self.assertEqual( root.find_kth_node(5).val, 11) self.assertEqual( root.find_kth_node(7).val, -100)
def test_leftonly(self): """Test a tree with only a left side.""" array = [ 42, -100, 12, 10, None, None, 11, None, 17, None, None, 23, None, None, None] s = "42(-100(12(10)[11[17]])[23])" root = mbt(array) root_ = mfs(s, BTN) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.preordertraversal(), root_.preordertraversal()) self.assertEqual( root.postordertraversal(), root_.postordertraversal())
def test_bothchildren(self): """Test a tree which has both children.""" s = "42(-100(12)[28])[100(2000(1)[2])[111]]" root = mfs(s, BTN) root.fixsize() self.assertEqual( root.find_kth_node(1).val, 12) self.assertEqual( root.find_kth_node(3).val, 28) self.assertEqual( root.find_kth_node(5).val, 1) self.assertEqual( root.find_kth_node(7).val, 2) self.assertEqual( root.find_kth_node(9).val, 111)
def test_singlecase(self): """Test a tree with just a root.""" s = "42" root = mfs(s, BTN) self.assertTrue(root.isbalanced())
def test_singlecase(self): """Test a tree with just a root.""" s = "42" root = mfs(s, BTN) self.assertIsNone(root.findkunbalanceddescendent(1))