Пример #1
0
 def test_get(self):
     """ Test that get returns correct values """
     seq = [3, 8, 5, 6, 1, 0, 2, 4, 9, 7]
     utils.list_to_bst(seq, self.bst)
     (seq[5], seq[8]) = (seq[8], seq[5])
     self.bst.insert(seq[5], 5)
     self.bst.insert(seq[8], 8)
     for v, k in enumerate(seq):
         self.assertEqual(v, self.bst.get(k))
Пример #2
0
 def test_get(self):
     """ Test that get returns correct values """
     size = 20
     seq = utils.random_seq(size)
     utils.list_to_bst(seq, self.bst)
     (seq[5], seq[16]) = (seq[16], seq[5])
     self.bst.insert(seq[5], 5)
     self.bst.insert(seq[16], 16)
     for v, k in enumerate(seq):
         self.assertEqual(v, self.bst.get(k))
Пример #3
0
 def test_del_error(self):
     """ Test that remove raise KeyError """
     with self.assertRaises(KeyError):
         self.bst.remove(3)
     size = 10
     seq = utils.random_seq(size)
     utils.list_to_bst(seq, self.bst)
     with self.assertRaises(KeyError):
         self.bst.remove(-1)
     with self.assertRaises(KeyError):
         self.bst.remove(size + 1)
     self.assertTrue(utils.is_bst(self.bst.root))
Пример #4
0
    def test_del_error(self):
        """ Test that remove raise KeyError """
        with self.assertRaises(KeyError):
            self.bst.remove(3)
        seq = [1, 5, 2, 4, 3, 0, 9, 7, 8, 6]
        utils.list_to_bst(seq, self.bst)

        with self.assertRaises(KeyError):
            self.bst.remove(-1)
        with self.assertRaises(KeyError):
            self.bst.remove(len(seq)+1)
        self.assertTrue(utils.is_bst(self.bst.root))
Пример #5
0
    def test_del(self):
        """ Test that remove maintain correct structure in tree """
        seq = [1, 5, 2, 4, 3, 0, 9, 7, 8, 6]
        utils.list_to_bst(seq, self.bst)
        remove_seq = [5, 0, 2, 3, 4, 1, 6, 7, 8, 9]

        for k in remove_seq:
            v = seq.index(k)
            self.assertEqual(v, self.bst.remove(k))
            with self.assertRaises(KeyError):
                self.bst.get(k)
            self.assertTrue(utils.is_bst(self.bst.root))
Пример #6
0
    def test_del(self):
        """ Test that remove maintain correct structure in tree """
        size = 10
        seq = utils.random_seq(size)
        utils.list_to_bst(seq, self.bst)
        r_seq = utils.random_seq_from_list(seq)

        for k in r_seq:
            v = seq.index(k)
            self.assertEqual(v, self.bst.remove(k))
            self.assertTrue(utils.is_bst(self.bst.root))
            print('k: {}, v: {}'.format(k, v))
Пример #7
0
 def test_inorder_traversal(self):
     """ Test the inorder traversal print"""
     size = 20
     seq = utils.random_seq(size)
     utils.list_to_bst(seq, self.bst)
     seq_dict = utils.list_to_dict(seq)
     sorted_keys = sorted(seq_dict.keys())
     res = [str(seq_dict[i]) for i in sorted_keys]
     with patch('sys.stdout', new=StringIO()) as fake_out:
         self.bst.inorder_traversal_print()
         printed_values = fake_out.getvalue().strip("\n")
         list_data = printed_values.split("\n")
         self.assertEqual(list_data, res)
Пример #8
0
    def test_inorder_traversal(self):
        """ Test the inorder traversal print"""
        seq = [3, 10, 4, 14, 1, 8, 2, 9, 12, 18, 16, 7, 0, 11, 17, 5, 13, 6, 19, 15]
        utils.list_to_bst(seq, self.bst)

        seq_dict = utils.list_to_dict(seq)# Done to get index for each value.
        sorted_keys = sorted(seq_dict.keys())# Get keys in order, values from seq list
        # use sorted keys to get values for they keys in order
        res = [str(seq_dict[i]) for i in sorted_keys]
        with patch('sys.stdout', new=StringIO()) as fake_out:
            self.bst.inorder_traversal_print()
            printed_values = fake_out.getvalue().strip("\n")
            list_data = printed_values.split("\n")
            self.assertEqual(list_data, res)
Пример #9
0
 def test_insert(self):
     """ Test that insert creates binary search tree """
     size = 100
     seq = utils.random_seq(size)
     utils.list_to_bst(seq, self.bst)
     self.assertTrue(utils.is_bst(self.bst.root))
Пример #10
0
 def test_insert(self):
     """ Test that insert creates binary search tree """
     seq = [3, 8, 5, 6, 1, 0, 2, 4, 9, 7]
     utils.list_to_bst(seq, self.bst)
     self.assertTrue(utils.is_bst(self.bst.root))