def setUp(self): # print("Initializing binary tree") self.full_binary_tree = self._generate_full_binary_tree() self.binary_tree = self._generate_binary_tree() self.left_tree = self._generate_left_tree() self.right_tree = self._generate_right_tree() self.empty_tree = BinaryTree()
def _generate_binary_tree(self): binary_tree = BinaryTree() binary_tree.copy_from_iterable( [0, 1, 2, None, 4, None, 6, None, None, 7]) return binary_tree
class TreeTest(unittest.TestCase): def setUp(self): self.al = Algorithm() self.tree = BinaryTree(1) tree = self.tree tree.left = BinaryTree(2) tree.right = BinaryTree(3) tree = tree.left tree.left = BinaryTree(4) tree.right = BinaryTree(5) tree = self.tree tree = tree.right tree.left = BinaryTree(6) tree.right = BinaryTree(7) tree.right.right = BinaryTree(8) def test_pre_order(self): self.assertEqual(self.al.pre_order(self.tree), self.tree.pre_order()) def test_in_order(self): self.assertEqual(self.al.in_order(self.tree), self.tree.in_order()) def test_last_order(self): self.assertEqual(self.al.last_order(self.tree), self.tree.last_order()) def test_mirror_tree(self): self.al.mirror_tree(self.tree) self.assertEqual(self.tree.pre_order(), [1, 3, 7, 8, 6, 2, 5, 4])
def _generate_full_binary_tree(self): """ Generates a 3-level full binary tree """ full_binary_tree = BinaryTree() full_binary_tree.copy_from_iterable([0, 1, 2, 3, 4, 5, 6]) return full_binary_tree
def _generate_left_tree(self): """ Generates a binary tree with each node having only left child called left tree for simplicity """ left_tree = BinaryTree() left_tree.copy_from_iterable([0, 1, None, 2, None, None, None, 3]) return left_tree
def _generate_right_tree(self): """ Generates a binary tree with each node having only right child called right tree for simplicity """ right_tree = BinaryTree() right_tree.copy_from_iterable([ 0, None, 1, None, None, None, 2, None, None, None, None, None, None, None, 3 ]) return right_tree
def rebuild_binary_tree(pre_order, in_order): if len(pre_order) < 1: return BinaryTree() root_index = in_order.index( pre_order[0]) # 前序遍历第一个元素是根,并通过中序遍历分出左子树、右子树 left_in_order = in_order[:root_index] # 左子树中序遍历 right_in_order = in_order[root_index + 1:] # 右子树中序遍历 left_pre_order = pre_order[1:len(left_in_order) + 1] # 通过左子树长度分出左子树前序遍历 right_pre_order = pre_order[len(left_in_order) + 1:] # 通过右子树长度分出右子树前序遍历 return BinaryTree( pre_order[0], Algorithm.rebuild_binary_tree(left_pre_order, left_in_order), Algorithm.rebuild_binary_tree(right_pre_order, right_in_order))
def setUp(self): self.al = Algorithm() self.tree = BinaryTree(1) tree = self.tree tree.left = BinaryTree(2) tree.right = BinaryTree(3) tree = tree.left tree.left = BinaryTree(4) tree.right = BinaryTree(5) tree = self.tree tree = tree.right tree.left = BinaryTree(6) tree.right = BinaryTree(7) tree.right.right = BinaryTree(8)
def test_split_subtree(self): """ Tests splitting subtree """ subtree_root = self.binary_tree.split_subtree( self.binary_tree.root().left_child()) subtree = BinaryTree(subtree_root) self.assertEqual(3, subtree.size()) self.assertEqual(2, subtree.depth()) self.assertEqual([(1, 2, 0), None, (4, 1, 1), None, None, (7, 0, 2)], subtree._level_order_traversal()) self.assertEqual(3, self.binary_tree.size()) self.assertEqual(2, self.binary_tree.depth()) self.assertEqual(2, self.binary_tree.root().height()) self.assertEqual([(0, 2, 0), None, (2, 1, 1), None, None, None, (6, 0, 2)], self.binary_tree._level_order_traversal())
def build_prase_tree(expression): ex_list = list(expression) stack = Stack() bin_tree = BinaryTree('root') for i in ex_list: if i == '(': stack.push(bin_tree) bin_tree.insert_left_child('-') bin_tree = bin_tree.left_child elif i in '+-*/': bin_tree = stack.pop() bin_tree.set_root_val(i) bin_tree.insert_right_child('') stack.push(bin_tree) bin_tree = bin_tree.right_child elif i == ')': bin_tree = stack.pop() elif i not in '+-*/': bin_tree.set_root_val(i) else: raise ValueError return bin_tree
def randomized_DTL(examples, attributes, default, pruning_threshold): if max(default.values()) == 1 or (len(examples) < 50): logger.debug("Found a leaf node, default: {}".format(default)) return default else: best_attribute, best_threshold, max_gain = choose_rand_attribute(examples, attributes, default) tree = BinaryTree() tree.add_root(attribute=best_attribute, threshold=best_threshold, gain=max_gain) examples_left = [row for row in examples if row[best_attribute] < best_threshold] examples_right = [row for row in examples if row[best_attribute] >= best_threshold] if len(examples_left) > 0 and len(examples_right) > 0: tree.attach_left(randomized_DTL(examples_left, attributes, distribution(examples_left), pruning_threshold)) tree.attach_right(randomized_DTL(examples_right, attributes, distribution(examples_right), pruning_threshold)) elif len(examples_left) > 0: tree.attach_left(randomized_DTL(examples_left, attributes, distribution(examples_left), pruning_threshold)) elif len(examples_right) > 0: tree.attach_right(randomized_DTL(examples_right, attributes, distribution(examples_right), pruning_threshold)) return tree
class TestBinaryTree(unittest.TestCase): @classmethod def setUpClass(self): self.initial_list = [i for i in range(5)] @classmethod def tearDownClass(self): print("All tests for binary tree completed") def _generate_binary_tree(self): binary_tree = BinaryTree() binary_tree.copy_from_iterable( [0, 1, 2, None, 4, None, 6, None, None, 7]) return binary_tree def _generate_full_binary_tree(self): """ Generates a 3-level full binary tree """ full_binary_tree = BinaryTree() full_binary_tree.copy_from_iterable([0, 1, 2, 3, 4, 5, 6]) return full_binary_tree def _generate_left_tree(self): """ Generates a binary tree with each node having only left child called left tree for simplicity """ left_tree = BinaryTree() left_tree.copy_from_iterable([0, 1, None, 2, None, None, None, 3]) return left_tree def _generate_right_tree(self): """ Generates a binary tree with each node having only right child called right tree for simplicity """ right_tree = BinaryTree() right_tree.copy_from_iterable([ 0, None, 1, None, None, None, 2, None, None, None, None, None, None, None, 3 ]) return right_tree def setUp(self): # print("Initializing binary tree") self.full_binary_tree = self._generate_full_binary_tree() self.binary_tree = self._generate_binary_tree() self.left_tree = self._generate_left_tree() self.right_tree = self._generate_right_tree() self.empty_tree = BinaryTree() def tearDown(self): pass def test_copy_from_iterable(self): """ Tests tree construction """ # full binary tree self.assertEqual(7, self.full_binary_tree.size()) self.assertEqual(2, self.full_binary_tree.depth()) self.assertEqual(True, self.full_binary_tree.is_full_binary_tree()) self.assertEqual([(0, 2, 0), (1, 1, 1), (2, 1, 1), (3, 0, 2), (4, 0, 2), (5, 0, 2), (6, 0, 2)], self.full_binary_tree._level_order_traversal()) self.assertEqual(False, self.full_binary_tree.empty()) # binary tree self.assertEqual(6, self.binary_tree.size()) self.assertEqual(3, self.binary_tree.depth()) self.assertEqual(False, self.binary_tree.is_full_binary_tree()) self.assertEqual([(0, 3, 0), (1, 2, 1), (2, 2, 1), None, (4, 1, 2), None, (6, 1, 2), None, None, (7, 0, 3)], self.binary_tree._level_order_traversal()) # empty tree self.assertEqual(True, self.empty_tree.empty()) # left tree self.assertEqual(4, self.left_tree.size()) self.assertEqual(3, self.left_tree.depth()) self.assertEqual([(0, 3, 0), (1, 2, 1), None, (2, 1, 2), None, None, None, (3, 0, 3)], self.left_tree._level_order_traversal()) # right tree self.assertEqual(4, self.right_tree.size()) self.assertEqual(3, self.right_tree.depth()) self.assertEqual([(0, 3, 0), None, (1, 2, 1), None, None, None, (2, 1, 2), None, None, None, None, None, None, None, (3, 0, 3)], self.right_tree._level_order_traversal()) def test_insert_root(self): """ Tests inserting new root node """ new_root = TreeNode(-1, 0) self.binary_tree.insert_root(new_root) self.assertEqual(7, self.binary_tree.size()) self.assertEqual(4, self.binary_tree.depth()) self.assertEqual(-1, self.binary_tree.root().data()) self.assertEqual([(-1, 4, 0), (0, 3, 1), None, (1, 2, 2), (2, 2, 2), None, None, None, (4, 1, 3), None, (6, 1, 3), None, None, None, None, None, None, (7, 0, 4)], self.binary_tree._level_order_traversal()) def test_insert_left_subtree(self): """ Tests inserting a left subtree to the binary tree """ # inerts full binary tree below the left child node of root as left child subtree_root = self.binary_tree.root().left_child() self.binary_tree.insert_left_subtree(subtree_root, self.full_binary_tree) self.assertEqual(13, self.binary_tree.size()) self.assertEqual(4, self.binary_tree.depth()) self.assertEqual([(0, 4, 0), (1, 3, 1), (2, 3, 1), (0, 2, 2), (4, 2, 2), None, (6, 2, 2), (1, 1, 3), (2, 1, 3), (7, 1, 3), None, None, None, None, None, (3, 0, 4), (4, 0, 4), (5, 0, 4), (6, 0, 4)], self.binary_tree._level_order_traversal()) # inerts full binary tree below root as right child self.binary_tree = self._generate_binary_tree() subtree_root = self.binary_tree.root() full_binary_tree = self._generate_full_binary_tree() self.binary_tree.insert_left_subtree(subtree_root, full_binary_tree) self.assertEqual(10, self.binary_tree.size()) self.assertEqual(3, self.binary_tree.depth()) self.assertEqual([(0, 3, 0), (0, 2, 1), (2, 2, 1), (1, 1, 2), (2, 1, 2), None, (6, 1, 2), (3, 0, 3), (4, 0, 3), (5, 0, 3), (6, 0, 3)], self.binary_tree._level_order_traversal()) def test_insert_right_subtree(self): """ Tests inserting a right subtree to the binary tree """ # inerts full binary tree below the left child node of root as left child subtree_root = self.binary_tree.root().left_child() self.binary_tree.insert_right_subtree(subtree_root, self.full_binary_tree) self.assertEqual(11, self.binary_tree.size()) self.assertEqual(4, self.binary_tree.depth()) self.assertEqual( [(0, 4, 0), (1, 3, 1), (2, 3, 1), None, (0, 2, 2), None, (6, 2, 2), None, None, (1, 1, 3), (2, 1, 3), None, None, None, None, None, None, None, None, (3, 0, 4), (4, 0, 4), (5, 0, 4), (6, 0, 4)], self.binary_tree._level_order_traversal()) self.binary_tree = self._generate_binary_tree() subtree_root = self.binary_tree.root() full_binary_tree = self._generate_full_binary_tree() self.binary_tree.insert_right_subtree(subtree_root, full_binary_tree) self.assertEqual([(0, 3, 0), (1, 2, 1), (0, 2, 1), None, (4, 1, 2), (1, 1, 2), (2, 1, 2), None, None, (7, 0, 3), None, (3, 0, 3), (4, 0, 3), (5, 0, 3), (6, 0, 3)], self.binary_tree._level_order_traversal()) self.assertEqual(11, self.binary_tree.size()) self.assertEqual(3, self.binary_tree.depth()) self.assertEqual(3, self.binary_tree.root().height()) def test_remove_subtree(self): """ Tests removing subtree """ self.full_binary_tree.remove_subtree( self.full_binary_tree.root().left_child()) self.assertEqual(4, self.full_binary_tree.size()) self.assertEqual(2, self.full_binary_tree.depth()) self.assertEqual([(0, 2, 0), None, (2, 1, 1), None, None, (5, 0, 2), (6, 0, 2)], self.full_binary_tree._level_order_traversal()) self.binary_tree.remove_subtree(self.binary_tree.root().right_child()) self.assertEqual(4, self.binary_tree.size()) self.assertEqual(3, self.binary_tree.depth()) self.assertEqual(0, self.binary_tree.root().data()) self.assertEqual(1, self.binary_tree.root().left_child().data()) self.assertEqual(2, self.binary_tree.root().left_child().height()) self.assertEqual( 4, self.binary_tree.root().left_child().right_child().data()) self.assertEqual( 1, self.binary_tree.root().left_child().right_child().height()) self.assertEqual( 7, self.binary_tree.root().left_child().right_child().left_child(). data()) self.assertEqual( 0, self.binary_tree.root().left_child().right_child().left_child(). height()) self.binary_tree.remove_subtree(self.binary_tree.root()) self.assertEqual(0, self.binary_tree.size()) self.assertEqual(0, self.binary_tree.depth()) self.assertEqual(None, self.binary_tree.root()) def test_split_subtree(self): """ Tests splitting subtree """ subtree_root = self.binary_tree.split_subtree( self.binary_tree.root().left_child()) subtree = BinaryTree(subtree_root) self.assertEqual(3, subtree.size()) self.assertEqual(2, subtree.depth()) self.assertEqual([(1, 2, 0), None, (4, 1, 1), None, None, (7, 0, 2)], subtree._level_order_traversal()) self.assertEqual(3, self.binary_tree.size()) self.assertEqual(2, self.binary_tree.depth()) self.assertEqual(2, self.binary_tree.root().height()) self.assertEqual([(0, 2, 0), None, (2, 1, 1), None, None, None, (6, 0, 2)], self.binary_tree._level_order_traversal()) def test_preorder_traversal(self): """ Tests pre-order traversal """ self.assertEqual([0, 1, 4, 7, 2, 6], self.binary_tree.preorder_traversal()) self.assertEqual([0, 1, 3, 4, 2, 5, 6], self.full_binary_tree.preorder_traversal()) self.assertEqual([], self.empty_tree.preorder_traversal()) def test_inorder_traversal(self): """ Tests in-order traversal """ self.assertEqual([1, 7, 4, 0, 2, 6], self.binary_tree.inorder_traversal()) self.assertEqual([3, 1, 4, 0, 5, 2, 6], self.full_binary_tree.inorder_traversal()) self.assertEqual([], self.empty_tree.inorder_traversal()) def test_postorder_traversal(self): """ Tests post-order traversal """ self.assertEqual([7, 4, 1, 6, 2, 0], self.binary_tree.postorder_traversal()) self.assertEqual([3, 4, 1, 5, 6, 2, 0], self.full_binary_tree.postorder_traversal()) self.assertEqual([], self.empty_tree.postorder_traversal()) def test_level_order_traversal(self): """ Tests level order traversal """ self.assertEqual([0, 1, 2, None, 4, None, 6, None, None, 7], self.binary_tree.level_order_traversal()) self.assertEqual([0, 1, 2, 3, 4, 5, 6], self.full_binary_tree.level_order_traversal()) self.assertEqual([], self.empty_tree.level_order_traversal()) self.assertEqual( [0, 1, 2, 4, 6, 7], self.binary_tree.level_order_traversal(include_none=False)) self.assertEqual( [0, 1, 2, 3, 4, 5, 6], self.full_binary_tree.level_order_traversal(include_none=False)) self.assertEqual( [], self.empty_tree.level_order_traversal(include_none=False))