def test_compare_leaves(self): rootA = Node(1) rootA.left_child = Node(2) rootA.left_child.left_child = Node(4) rootA.right_child = Node(3) rootA.right_child.left_child = Node(6) rootA.right_child.right_child = Node(7) rootB = Node(0) rootB.left_child = Node(5) rootB.left_child.right_child = Node(4) rootB.right_child = Node(8) rootB.right_child.left_child = Node(6) rootB.right_child.right_child = Node(7) self.assertTrue(rootA.compare_leaves(rootB)) rootC = Node(0) rootC.left_child = Node(1) rootC.left_child.left_child = Node(8) rootC.left_child.right_child = Node(9) rootC.right_child = Node(2) rootD = Node(1) rootD.left_child = Node(4) rootD.left_child.right_child = Node(8) rootD.right_child = Node(3) rootD.right_child.left_child = Node(2) rootD.right_child.right_child = Node(9) self.assertFalse(rootC.compare_leaves(rootD))
def test_vertical_string(self): root = Node(10) root.left_child = Node(12) root.left_child.left_child = Node(25) root.left_child.right_child = Node(30) root.right_child = Node(15) root.right_child.left_child = Node(36) self.assertEqual('251210303615', root.vertical_string())
def test_find_leftmost_rightmost(self): root = Node(10) root.left_child = Node(12) root.left_child.left_child = Node(25) root.left_child.right_child = Node(30) root.right_child = Node(15) root.right_child.left_child = Node(36) left, right = root.find_leftmost_rightmost(0) self.assertEqual(left, -2) self.assertEqual(right, 1)
def test_to_dlinked_list(self): root = Node(10) root.left_child = Node(12) root.left_child.left_child = Node(25) root.left_child.right_child = Node(30) root.right_child = Node(15) root.right_child.left_child = Node(36) self.assertEqual('251230103615', root.str_in_order()) dll = DoublyLinkedList() dll.head, temp = root.to_dlinked_list() self.assertEqual('25 12 30 10 36 15 ', str(dll))
def test_balance(self): root = Node(4) root.left_child = Node(3) root.left_child.left_child = Node(2) root.left_child.left_child.left_child = Node(1) root.right_child = Node(5) root.right_child.right_child = Node(6) root.right_child.right_child.right_child = Node(7) broot = root.balance() self.assertEqual(broot.str_in_order(), '1234567') self.assertEqual(broot.str_pre_order(), '4213657') self.assertEqual(broot.str_post_order(), '1325764')
def test_boundaries(self): root = Node(10) root.left_child = Node(12) root.left_child.left_child = Node(25) root.left_child.right_child = Node(30) root.left_child.left_child.right_child = Node(19) root.left_child.right_child.right_child = Node(23) root.left_child.left_child.right_child.right_child = Node(3) root.left_child.left_child.right_child.right_child.right_child = Node(6) root.right_child = Node(15) root.right_child.left_child = Node(36) self.assertEqual('1012251936233615', root.boundaries())