예제 #1
0
	def test_successfully_add_to_right_child(self):
		node1 = Node(None, 10)
		node2 = Node(None, 20)

		node1.add_right(node2)

		self.assertIs(node1.right_child, node2)
		self.assertIs(node2.parent, node1)
예제 #2
0
	def test_check_node_is_leaf(self):
		node1 = Node(None, 10)
		node2 = Node(None, 20)

		node1.add_right(node2)

		self.assertFalse(node1.is_leaf())
		self.assertTrue(node2.is_leaf())
예제 #3
0
	def test_search_node_by_not_equal_key(self):
		node1 = Node(None, 10)
		node2 = Node(None, 7)
		node3 = Node(None, 12)
		node4 = Node(None, 4)

		tree = Tree()
		tree.root = node1

		node1.add_left(node2)
		node1.add_right(node3)

		node2.add_left(node4)

		self.assertEqual([node4, False, 'left'], tree.search(3))
		self.assertEqual([node3, False, 'right'], tree.search(15))
예제 #4
0
	def test_search_node_by_equal_key(self):
		node1 = Node(None, 10)
		node2 = Node(None, 7)
		node3 = Node(None, 12)
		node4 = Node(None, 4)

		tree = Tree()
		tree.root = node1

		node1.add_left(node2)
		node1.add_right(node3)

		node2.add_left(node4)

		self.assertEqual([node4, True], tree.search(4))
		self.assertEqual([node3, True], tree.search(12))
예제 #5
0
	def test_failed_to_add_left_child(self):
		node1 = Node(None, 10)
		node2 = Node(None, None)
		node3 = Node(None, 2)

		with self.assertRaises(ValueError):
			node1.add_left(node2)

		node2.key = 20

		with self.assertRaises(ValueError):
			node1.add_left(node2)

		node2.key = 5
		node1.right_child = node3	

		with self.assertRaises(ValueError):
			node1.add_left(node2)
예제 #6
0
	def test_delete_node_right_descendant(self):	
		node1 = Node(None, 8)
		node2 = Node(None, 4)
		node3 = Node(None, 12)
		node4 = Node(None, 10)
		node5 = Node(None, 14)
		node6 = Node(None, 15)

		node1.add_left(node2)
		node1.add_right(node3)
		node3.add_left(node4)
		node3.add_right(node5)
		node5.add_right(node6)

		tree = Tree()
		tree.root = node1

		tree.delete(node3)

		self.assertIs(node5, node1.right_child)
		self.assertIs(node5.parent, node1)

		self.assertIs(node5.left_child, node4)
		self.assertIs(node4.parent, node5)

		self.assertIsNone(node3.parent)
		self.assertIsNone(node3.left_child)
		self.assertIsNone(node3.right_child)
예제 #7
0
	def test_get_min_max_keys(self):
		node1 = Node(None, 10)
		node2 = Node(None, 7)
		node3 = Node(None, 12)
		node4 = Node(None, 4)
		node5 = Node(None, 11)

		tree = Tree()
		tree.root = node1

		node1.add_left(node2)
		node1.add_right(node3)
		node2.add_left(node4)
		node2.add_right(node5)
	
		self.assertEqual({'min': 4, 'max': 12}, tree.get_min_max())
		self.assertEqual({'min': 4, 'max': 11}, tree.get_min_max(node2))