コード例 #1
0
    def test_creation(self):
        """ Test the creation of a list.
        """
        bt = binary_tree.BinaryTree()

        # The BT has an empty root node
        self.assertIs(bt.root_node, None)

        # But we can create one with a root node.
        bt = binary_tree.BinaryTree(binary_tree.Node('g'))

        self.assertEquals(bt.root_node.data, 'g')
コード例 #2
0
 def test_binary_tree_insert(self):
     bt = binary_tree.BinaryTree()
     bt.insert(10)
     bt.insert(11)
     bt.insert(9)
     assert bt.tree.left.value == 9
     assert bt.tree.right.value == 11
コード例 #3
0
 def test_bt_sort(self):
     my_arr = [2, 5, 7, 19, 13, 20, 15, 22, 4]
     my_tree = bt.BinaryTree()
     for i in range(len(my_arr)):
         my_tree.insert(my_arr[i])
     assert my_tree.traverse_dfs_tree(
         my_tree.head) == [2, 4, 5, 7, 13, 15, 19, 20, 22]
コード例 #4
0
 def test_binary_tree_init_values(self):
     bt = binary_tree.BinaryTree(keys=[1, 2, 3, 4, 0])
     assert bt.tree.value == 1
     assert bt.tree.right.value == 2
     assert bt.tree.right.right.value == 3
     assert bt.tree.right.right.right.value == 4
     assert bt.tree.left.value == 0
コード例 #5
0
 def test_bt_insert(self):
     my_arr = [2, 5, 7]
     my_tree = bt.BinaryTree()
     for i in range(len(my_arr)):
         my_tree.insert(my_arr[i])
     assert my_tree.head.node_value == 2
     assert my_tree.depth == 3
コード例 #6
0
ファイル: test_map.py プロジェクト: usrnm242/algorithms
def compare_speed(lst, plot=False):
    avl_tree = avl.BinaryTree()
    binary_tree = binary.BinaryTree()

    avl_time = []
    binary_time = []

    for i in lst:
        start = time()
        avl_tree.append(i)
        end = time()
        avl_time.append(end - start)
        # _________________________ #
        start = time()
        binary_tree.append(i)
        end = time()
        binary_time.append(end - start)

    avl_time = np.array(avl_time)
    binary_time = np.array(binary_time)

    if plot is True:
        avl_times.append(avl_time.mean())
        binary_times.append(binary_time.mean())
    else:
        print("avl time mean =", avl_time.mean())
        print("binary time mean =", binary_time.mean())
        print("is avl time mean < binary time mean:", avl_time.mean() < binary_time.mean())
コード例 #7
0
 def ExampleTree(self):
     tree = binary_tree.BinaryTree(1)
     tree.root.left = binary_tree.Node(2)
     tree.root.right = binary_tree.Node(3)
     tree.root.left.left = binary_tree.Node(4)
     tree.root.left.right = binary_tree.Node(5)
     return tree
コード例 #8
0
 def test_insert(self):
     tree = binary_tree.BinaryTree()
     tree.insert('b', 'b_val')
     self.assertEqual(tree.root.value, 'b_val')
     tree.insert('c', 'c_val')
     self.assertEqual(tree.root.right.key, 'c')
     tree.insert('a', 'a_val')
     self.assertEqual(tree.root.left.value, 'a_val')
コード例 #9
0
 def test_binary_tree_search(self):
     bt = binary_tree.BinaryTree(keys=[1, 2, 3, 4, 0], values=[
                                 'one', 'two', 'three', 'four', 'zero'])
     assert bt.search(1) == 'one'
     assert bt.search(2) == 'two'
     assert bt.search(3) == 'three'
     assert bt.search(4) == 'four'
     assert bt.search(0) == 'zero'
コード例 #10
0
    def test_duplicate_insert(self):
        tree = binary_tree.BinaryTree()
        tree.insert(10, 'a')
        tree.insert(15, 'b')
        tree.insert(5, 'c')
        tree.insert(12, 'd')
        tree.insert(12, 'e')

        self.assertEqual(tree.root.right.left.value, 'd')
コード例 #11
0
 def test_binary_tree_search_no_find(self):
     bt = binary_tree.BinaryTree(keys=[1, 2, 3, 4, 0], values=[
                                 'one', 'two', 'three', 'four', 'zero'])
     assert bt.search(11) == -1
     assert bt.search(100) == -1
     bt.insert(11, value='eleven')
     bt.insert(100, value='hundred')
     assert bt.search(11) == 'eleven'
     assert bt.search(100) == 'hundred'
コード例 #12
0
 def test_search(self):
     test3 = binary_tree.BinaryTree()
     test3.insert(1, 'value2')
     test3.insert(2, 'value3')
     test3.insert(0.3, 'value0.3')
     self.assertEqual(test3.search(1).value, 'value2')
     self.assertEqual(test3.search(2).value, 'value3')
     self.assertEqual(test3.search(0.3).value, 'value0.3')
     self.assertEqual(test3.search(4), None)
コード例 #13
0
    def test_basic_add(self):
        tree = binary_tree.BinaryTree()
        tree.insert(10, 'a')
        tree.insert(15, 'b')
        tree.insert(-5, 'c')
        tree.insert(12, 'd')

        self.assertEqual(tree.root.key, 10)
        self.assertEqual(tree.root.left.key, -5)
        self.assertEqual(tree.root.right.left.key, 12)
コード例 #14
0
    def test_inorder(self):
        """ We print with an inorder representation.
        """
        bt = binary_tree.BinaryTree()

        bt.populate(['g', 'b', 't', 'z', 'c'])

        self.assertEquals(bt.inorder(bt.root_node), ['b', 'c', 'g', 't', 'z'])

        self.assertEquals(bt.__str__(), 'b c g t z')
コード例 #15
0
 def test_search(self):
     tree = binary_tree.BinaryTree()
     tree.insert('b', 'b_val')
     tree.insert('c', 'c_val')
     tree.insert('a', 'a_val')
     search_a = tree.search('a')
     self.assertEqual(search_a.value, 'a_val')
     search_a = tree.search('b')
     self.assertEqual(search_a.value, 'b_val')
     search_a = tree.search('c')
     self.assertEqual(search_a.value, 'c_val')
コード例 #16
0
 def test_insert(self):
     test2 = binary_tree.BinaryTree()
     self.assertEqual(test2.root, None)
     test2.insert(1, 'value2')
     self.assertEqual(test2.root.key, 1)
     self.assertEqual(test2.root.value, 'value2')
     test2.insert(2, 'value3')
     self.assertEqual(test2.root.right.key, 2)
     self.assertEqual(test2.root.right.value, 'value3')
     test2.insert(0.3, 'value0.3')
     self.assertEqual(test2.root.left.key, 0.3)
     self.assertEqual(test2.root.left.value, 'value0.3')
コード例 #17
0
    def test_non_num_key_on_search(self):
        tree = binary_tree.BinaryTree()
        tree.insert(10, 'a')
        tree.insert(15, 'b')
        tree.insert(5, 'c')
        tree.insert(12, 'd')

        with self.assertRaises(TypeError) as ex:
            tree.search("not int")

        self.assertEqual(
            str(ex.exception), "Keys must be integers")
コード例 #18
0
    def test_population(self):
        """ We have a helper to populate the tree.
        """
        bt = binary_tree.BinaryTree()

        bt.populate(['g', 'b', 't', 'z', 'c'])

        self.assertEquals(bt.root_node.data, 'g')

        self.assertEquals(bt.root_node.left.data, 'b')

        self.assertEquals(bt.root_node.left.right.data, 'c')
コード例 #19
0
	def test_when_there_are_multiple_same_nodes_in_the_tree(self):
		root = b.Node(3)
		e1 = b.Node(3)
		e2 = b.Node(3)
		e3 = b.Node(4)
		e4 = b.Node(5)

		bt = b.BinaryTree(root)
		bt.root.lchild = e1
		bt.root.rchild = e2
		bt.root.lchild.lchild = e3
		bt.root.rchild.lchild = e4
コード例 #20
0
    def test_random_insert_and_search(self):
        tree = binary_tree.BinaryTree()
        tree.insert(1, 'a')

        entries = {}
        for i in range(0, 100):
            key = random.randint(-1000, 1000)
            value = random.randint(-1000, 1000)
            tree.insert(key, value)

        for key, value in entries.items():
            self.assertEqual(tree.search(key), value)
コード例 #21
0
 def testFind(self):
     # Search all elements
     bt = binary_tree.BinaryTree()
     bt.add(50)
     bt.add(17)
     bt.add(76)
     bt.add(9)
     bt.add(23)
     self.assertEqual(bt.find(50), bt.root)
     self.assertEqual(bt.find(17), bt.root.left)
     self.assertEqual(bt.find(76), bt.root.right)
     self.assertEqual(bt.find(9), bt.root.left.left)
     self.assertEqual(bt.find(23), bt.root.left.right)
コード例 #22
0
 def test_binary_tree_init_key_value(self):
     bt = binary_tree.BinaryTree(keys=[1, 2, 3, 4, 0], values=[
                                 'one', 'two', 'three', 'four', 'zero'])
     assert bt.tree.key == 1
     assert bt.tree.right.key == 2
     assert bt.tree.right.right.key == 3
     assert bt.tree.right.right.right.key == 4
     assert bt.tree.left.key == 0
     assert bt.tree.value == 'one'
     assert bt.tree.right.value == 'two'
     assert bt.tree.right.right.value == 'three'
     assert bt.tree.right.right.right.value == 'four'
     assert bt.tree.left.value == 'zero'
コード例 #23
0
 def setUp(self):
     """This setup uses tree.insert, therefore tests in this file 
     shall only be performed after all tests in *test_binary_tree.py* are passed.
     
     The test tree is the same as provided in Q1 in assignment2. """
     self.tree = btree.BinaryTree(16)
     self.tree.insert(16, 9)
     self.tree.insert(16, 18, left=False)
     self.tree.insert(18, 19, left=False)
     self.tree.insert(9, 3)
     self.tree.insert(9, 14, left=False)
     self.tree.insert(3, 1)
     self.tree.insert(3, 5, left=False)
コード例 #24
0
ファイル: parcours.py プロジェクト: qkzk/qkzk.github.io
def tester_tlm():
    arbre_abcdefg = bt.BinaryTree(
        "a",
        bt.BinaryTree(
            "b",
            feuille("d"),
            feuille("e"),
        ),
        bt.BinaryTree(
            "c",
            feuille("f"),
            feuille("g")
        )
    )
    # arbre_abcdefg.show()

    # parcours largeur
    visites = parcours_largeur(arbre_abcdefg)
    assert visites == list('abcdefg')
    assert parcours_largeur(VIDE) == []

    # parcours en profondeur
    assert parcours_profondeur(arbre_abcdefg,
                               ordre="prefixe") == list('abdecfg')

    assert parcours_profondeur(arbre_abcdefg,
                               ordre="infixe") == list('dbeafcg')

    assert parcours_profondeur(arbre_abcdefg,
                               ordre="suffixe") == list('debfgca')

    # profondeur pile
    assert profondeur_pile(arbre_abcdefg) == list('abdecfg')

    # prefixe, infixe, suffixe
    assert prefixe(arbre_abcdefg) == list('abdecfg')
    assert infixe(arbre_abcdefg) == list('dbeafcg')
    assert suffixe(arbre_abcdefg) == list('debfgca')
コード例 #25
0
def main():
    global struct
    global insert_num
    global delete_num
    global min_num
    global max_num
    global succ_num
    global find_num
    global curr_elem_num
    global greatest_elem_number

    start = time.time()
    args = list(sys.argv)
    struct_type = str(args[2])
    if struct_type == 'bst':
        struct = binary_tree.BinaryTree()
    elif struct_type == 'rbt':
        struct = rb_tree.RBTree()
    elif struct_type == 'hmap':
        struct = hmap.HashMap()
    else:
        struct = None
        return 0
    functions_number = int(input())
    while functions_number:
        function = input()
        args = None
        try:
            name, args = function.split(" ")
        except:
            name = function.split(" ")[0]
        print(name)
        if args is not None:
            eval(name)(args)
        else:
            eval(name)()
        functions_number -= 1

    end = time.time()
    print("Czas działania: %s" % (str(end - start)), file=sys.stderr)
    print("Liczba operacji:", file=sys.stderr)
    print("Insert: %d, delete: %d, min: %d, max: %d, find: %d, successor: %d" %
          (insert_num, delete_num, min_num, max_num, find_num, succ_num),
          file=sys.stderr)
    print("Maksymalne zapełnienie struktury: %d" % (greatest_elem_number),
          file=sys.stderr)
    print("Końcowe zapełnienie struktury: %d" % (curr_elem_num),
          file=sys.stderr)
コード例 #26
0
	def test_when_there_are_none_of_the_same_nodes_in_the_tree(self):

		root = b.Node(6)
		e1 = b.Node(7)
		e2 = b.Node(8)
		e3 = b.Node(9)
		e4 = b.Node(10)

		bt = b.BinaryTree(root)
		bt.root.lchild = e1
		bt.root.rchild = e2
		bt.root.lchild.lchild = e3
		bt.root.rchild.lchild = e4

		#self.assertEqual(bt.search(6), True)
		self.assertEqual(bt.search(7), True)		
コード例 #27
0
 def testDelete(self):
     bt = binary_tree.BinaryTree()
     bt.add(50)
     bt.add(17)
     bt.add(76)
     bt.add(9)
     bt.add(23)
     bt.add(14)
     bt.add(12)
     bt.add(54)
     bt.add(72)
     bt.add(67)
     bt.delete(9)
     self.assertEqual(bt.root.left.left.value, 14)
     bt.delete(67)
     self.assertEqual(bt.root.right.left.right.left, None)
     bt.delete(76)
     self.assertEqual(bt.root.right.value, 54)
コード例 #28
0
    def test_create_tree(self):
        root = self.createTree([10, 5, 2, 4, 6, 8])

        self.assertEqual(root.key, 10)
        self.assertEqual(root.left_child.key, 5)
        self.assertEqual(root.left_child.left_child.key, 2)
        self.assertEqual(root.left_child.right_child.key, 6)
        self.assertEqual(root.left_child.right_child.right_child.key, 8)

        self.assertEqual(root.size, 6)
        self.assertEqual(root.left_child.size, 5)
        self.assertEqual(root.left_child.left_child.size, 2)
        self.assertEqual(root.left_child.left_child.right_child.size, 1)
        self.assertEqual(root.left_child.right_child.size, 2)
        self.assertEqual(root.left_child.right_child.right_child.size, 1)

        bsb = binary_tree.BinaryTree()
        result = bsb.preorder(root)

        self.assertEqual(result, [10, 5, 2, 4, 6, 8])
コード例 #29
0
ファイル: per.py プロジェクト: EAboelhamd/DDQN-PER
	def __init__(self, args):
		print('Initializing Priortized Experience Replay')
		self.args = args
		# Store (state, action, reward, next_state)
		self.size = self.args.replay_size

		# Batches
		self.batch_states = np.empty([self.args.batch_size, 84, 84, 4])
		self.batch_actions = np.empty([self.args.batch_size])
		self.batch_rewards = np.empty([self.args.batch_size])
		self.batch_terminals = np.empty([self.args.batch_size])
		self.batch_next_states = np.empty([self.args.batch_size, 84, 84, 4])

		# Count number of samples in binary tree
		self.counter = 0
		self.full_flag = False
		
		# Create binary tree
		self.bt = binary_tree.BinaryTree(self.size)
		# To normalize importance weight
		self.max_priority = 0
コード例 #30
0
 def testAdd(self):
     bt = binary_tree.BinaryTree()
     bt.add(50)
     bt.add(17)
     bt.add(76)
     bt.add(9)
     bt.add(23)
     bt.add(14)
     bt.add(12)
     bt.add(54)
     bt.add(72)
     bt.add(67)
     self.assertEqual(bt.root.value, 50)
     self.assertEqual(bt.root.left.value, 17)
     self.assertEqual(bt.root.right.value, 76)
     self.assertEqual(bt.root.left.left.value, 9)
     self.assertEqual(bt.root.left.right.value, 23)
     self.assertEqual(bt.root.left.left.right.value, 14)
     self.assertEqual(bt.root.left.left.right.left.value, 12)
     self.assertEqual(bt.root.right.left.value, 54)
     self.assertEqual(bt.root.right.left.right.value, 72)
     self.assertEqual(bt.root.right.left.right.left.value, 67)