Esempio n. 1
0
 def setUp(self):
     self.ul = BinarySearchTree(100)
     self.ul.ints = [
         150, 50, 140, 40, 145, 160, 155, 157, 153, 55, 49, 51, 39, 41
     ]
     for i in self.ul.ints:
         self.ul.insert(i)
Esempio n. 2
0
class TreeTests(unittest.TestCase):
    def setUp(self):
        self.ul = BinarySearchTree(100)
        self.ul.ints = [
            150, 50, 140, 40, 145, 160, 155, 157, 153, 55, 49, 51, 39, 41
        ]
        for i in self.ul.ints:
            self.ul.insert(i)

    def test_traverse(self):
        ints = '39, 40, 41, 49, 50, 51, 55, 100, 140, 145, 150, 153, 155, 157, 160'
        self.assertEqual(str(ints), str(self.ul))

    def test_insert(self):
        self.assertTrue(self.ul.left.key == 50)
        self.assertTrue(self.ul.right.key == 150)
        self.assertEqual(self.ul.right.left.key, 140)

    def test_lookup(self):
        self.assertTrue(self.ul.left.right == self.ul.lookup(55))

    def test_delete(self):
        self.ul.delete(50)
        self.ul.ints.remove(50)
        self.assertEqual(str(self.ul.ints), str(self.ul))


#        100
#       /   \
#     98     101
#       \       \
#        99      102
Esempio n. 3
0
class TreeTests(unittest.TestCase):
    def setUp(self):
        self.ul = BinarySearchTree(100)
        self.ul.ints = [150, 50, 140, 40, 145, 160, 155, 157, 153, 55, 49, 51, 39, 41]
        for i in self.ul.ints:
            self.ul.insert(i)

    def test_traverse(self):
        ints = '39, 40, 41, 49, 50, 51, 55, 100, 140, 145, 150, 153, 155, 157, 160'
        self.assertEqual(str(ints), str(self.ul))

    def test_insert(self):
        self.assertTrue(self.ul.left.key == 50)
        self.assertTrue(self.ul.right.key == 150)
        self.assertEqual(self.ul.right.left.key, 140)

    def test_lookup(self):
        self.assertTrue(self.ul.left.right == self.ul.lookup(55))

    def test_delete(self):
        self.ul.delete(50)
        self.ul.ints.remove(50)
        self.assertEqual(str(self.ul.ints), str(self.ul))

#        100
#       /   \
#     98     101
#       \       \
#        99      102
 def test_left_insert(self):
 	#test insert right
 	tree = BinarySearchTree(3)
 	tree.insert(5)
 	tree.insert(3)
 	tree.insert(6)
 	self.assertEqual(tree.right.key, 5)
 def test_right_insert(self):
 	#test insert left
 	tree = BinarySearchTree(5)
 	tree.insert(3)
 	tree.insert(2)
 	tree.insert(4)
 	self.assertEqual(tree.left.key, 3)
Esempio n. 6
0
    def test_delete(self):
        self.tree.insert(25)
        self.tree.insert(30)
        self.tree.insert(20)
        self.tree.insert(27)
        self.tree.insert(23)
        self.tree.insert(24)
        self.tree.insert(19)

        self.tree.insert(75)
        self.tree.insert(70)
        self.tree.insert(80)
        self.tree.insert(72)
        self.tree.insert(77)

        self.tree2 = BinarySearchTree(50)
        self.tree2.insert(27)
        self.tree2.insert(30)
        self.tree2.insert(20)
        self.tree2.insert(23)
        self.tree2.insert(24)
        self.tree2.insert(19)

        self.tree2.insert(75)
        self.tree2.insert(80)
        self.tree2.insert(72)

        self.tree.delete(25)
        self.tree.delete(77)
        self.tree.delete(70)
        self.assertTrue(self.tree == self.tree2)
Esempio n. 7
0
def tree_factory(*values):
    """Create a BST with a given sequence of keys."""
    bst = BinarySearchTree(values[0])

    for key in values[1:]:  # Skip first value since it's already added
        bst.insert(key)

    return bst
 def test_delete_one_child(self):
     #tesing delete nodes parent, node has one child (right side)
     self.t = BinarySearchTree(10)
     self.t.insert(15)
     self.t.insert(20)
     self.t.delete(10)
     self.assertEqual(self.t.lookup(15)[0].key, 15)
     self.assertEqual(self.t.lookup(20)[0].key, 20)
     self.assertIsNone(self.t.lookup(10)[0])
Esempio n. 9
0
    def test_delete(self):
        self.tree.insert(25)
        self.tree.insert(30)
        self.tree.insert(20)
        self.tree.insert(27)
        self.tree.insert(23)
        self.tree.insert(24)
        self.tree.insert(19)

        self.tree.insert(75)
        self.tree.insert(70)
        self.tree.insert(80)
        self.tree.insert(72)
        self.tree.insert(77)


        self.tree2 = BinarySearchTree(50)
        self.tree2.insert(27)
        self.tree2.insert(30)
        self.tree2.insert(20)
        self.tree2.insert(23)
        self.tree2.insert(24)
        self.tree2.insert(19)

        self.tree2.insert(75)
        self.tree2.insert(80)
        self.tree2.insert(72)

        self.tree.delete(25)
        self.tree.delete(77)
        self.tree.delete(70)
        self.assertTrue(self.tree == self.tree2)
Esempio n. 10
0
    def setUp(self):
        #        10
        #       /   \
        #     5       15
        #    /       /  \
        #   2      12    20
        #  / \
        # 1   3

        self.t = BinarySearchTree(10)
        self.t.insert(5)
        self.t.insert(2)
        self.t.insert(1)
        self.t.insert(3)
        self.t.insert(15)
        self.t.insert(12)
        self.t.insert(20)
Esempio n. 11
0
 def test_lookup_left_exists(self):
     tree = BinarySearchTree(4)
     tree.insert(2)
     tree.insert(3)
     tree.insert(5)
     tree.insert(7)
     self.assertEqual(tree.lookup(2)[0].key, 2)
Esempio n. 12
0
    def test_delete_one_child(self):
    	#tesing delete nodes parent, node has one child (right side)
	    self.t = BinarySearchTree(10)
	    self.t.insert(15)
	    self.t.insert(20)
	    self.t.delete(10)
	    self.assertEqual(self.t.lookup(15)[0].key, 15)
	    self.assertEqual(self.t.lookup(20)[0].key, 20)
	    self.assertIsNone(self.t.lookup(10)[0])
Esempio n. 13
0
 def test_lookup_right_exists(self):
     #test lookup right if key exists
     tree = BinarySearchTree(5)
     tree.insert(2)
     tree.insert(6)
     tree.insert(0)
     tree.insert(3)
     self.assertEqual(tree.lookup(6)[0].key, 6)
Esempio n. 14
0
 def test_left_insert(self):
     #test insert right
     tree = BinarySearchTree(3)
     tree.insert(5)
     tree.insert(3)
     tree.insert(6)
     self.assertEqual(tree.right.key, 5)
Esempio n. 15
0
 def test_lookup_left_exists(self):
 	tree = BinarySearchTree(4)
 	tree.insert(2)
 	tree.insert(3)
 	tree.insert(5)
 	tree.insert(7)
 	self.assertEqual(tree.lookup(2)[0].key, 2)
Esempio n. 16
0
 def test_right_insert(self):
     #test insert left
     tree = BinarySearchTree(5)
     tree.insert(3)
     tree.insert(2)
     tree.insert(4)
     self.assertEqual(tree.left.key, 3)
Esempio n. 17
0
 def test_lookup_right_exists(self):
 	#test lookup right if key exists 
 	tree = BinarySearchTree(5)
 	tree.insert(2)
 	tree.insert(6)
 	tree.insert(0)
 	tree.insert(3)
 	self.assertEqual(tree.lookup(6)[0].key, 6)
Esempio n. 18
0
class TreeTests(unittest.TestCase):

    def test_left_insert(self):
    	#test insert right
    	tree = BinarySearchTree(3)
    	tree.insert(5)
    	tree.insert(3)
    	tree.insert(6)
    	self.assertEqual(tree.right.key, 5)

    def test_right_insert(self):
    	#test insert left
    	tree = BinarySearchTree(5)
    	tree.insert(3)
    	tree.insert(2)
    	tree.insert(4)
    	self.assertEqual(tree.left.key, 3)

    def test_lookup_right_exists(self):
    	#test lookup right if key exists 
    	tree = BinarySearchTree(5)
    	tree.insert(2)
    	tree.insert(6)
    	tree.insert(0)
    	tree.insert(3)
    	self.assertEqual(tree.lookup(6)[0].key, 6)

    def test_lookup_left_exists(self):
    	tree = BinarySearchTree(4)
    	tree.insert(2)
    	tree.insert(3)
    	tree.insert(5)
    	tree.insert(7)
    	self.assertEqual(tree.lookup(2)[0].key, 2)

    def test_lookup_right_not_exists(self):
    	tree = BinarySearchTree(3)
    	self.assertEqual(tree.lookup(7)[0], None)

    def test_lookup_left_not_exists(self):
    	tree = BinarySearchTree(3)
    	self.assertEqual(tree.lookup(1)[0], None)

    def test_delete_tree(self):
    	tree = BinarySearchTree(3)
    	self.assertRaises(Exception, tree.delete, 3)

    def test_delete_one_child(self):
    	#tesing delete nodes parent, node has one child (right side)
	    self.t = BinarySearchTree(10)
	    self.t.insert(15)
	    self.t.insert(20)
	    self.t.delete(10)
	    self.assertEqual(self.t.lookup(15)[0].key, 15)
	    self.assertEqual(self.t.lookup(20)[0].key, 20)
	    self.assertIsNone(self.t.lookup(10)[0])
Esempio n. 19
0
class TreeTests(unittest.TestCase):
    def test_left_insert(self):
        #test insert right
        tree = BinarySearchTree(3)
        tree.insert(5)
        tree.insert(3)
        tree.insert(6)
        self.assertEqual(tree.right.key, 5)

    def test_right_insert(self):
        #test insert left
        tree = BinarySearchTree(5)
        tree.insert(3)
        tree.insert(2)
        tree.insert(4)
        self.assertEqual(tree.left.key, 3)

    def test_lookup_right_exists(self):
        #test lookup right if key exists
        tree = BinarySearchTree(5)
        tree.insert(2)
        tree.insert(6)
        tree.insert(0)
        tree.insert(3)
        self.assertEqual(tree.lookup(6)[0].key, 6)

    def test_lookup_left_exists(self):
        tree = BinarySearchTree(4)
        tree.insert(2)
        tree.insert(3)
        tree.insert(5)
        tree.insert(7)
        self.assertEqual(tree.lookup(2)[0].key, 2)

    def test_lookup_right_not_exists(self):
        tree = BinarySearchTree(3)
        self.assertEqual(tree.lookup(7)[0], None)

    def test_lookup_left_not_exists(self):
        tree = BinarySearchTree(3)
        self.assertEqual(tree.lookup(1)[0], None)

    def test_delete_tree(self):
        tree = BinarySearchTree(3)
        self.assertRaises(Exception, tree.delete, 3)

    def test_delete_one_child(self):
        #tesing delete nodes parent, node has one child (right side)
        self.t = BinarySearchTree(10)
        self.t.insert(15)
        self.t.insert(20)
        self.t.delete(10)
        self.assertEqual(self.t.lookup(15)[0].key, 15)
        self.assertEqual(self.t.lookup(20)[0].key, 20)
        self.assertIsNone(self.t.lookup(10)[0])
Esempio n. 20
0
	def setUp(self):
		#        10
		#       /   \
		#     5       15
		#    /       /  \
		#   2      12    20
		#  / \
		# 1   3

		self.t = BinarySearchTree(10)
		self.t.insert(5)
		self.t.insert(2)
		self.t.insert(1)
		self.t.insert(3)
		self.t.insert(15)
		self.t.insert(12)
		self.t.insert(20)
Esempio n. 21
0
 def setUp(self):
     self.tree = BinarySearchTree(50)
Esempio n. 22
0
class TreeDeleteTests(unittest.TestCase):
    def setUp(self):
        #        10
        #       /   \
        #     5       15
        #    /       /  \
        #   2      12    20
        #  / \
        # 1   3

        self.t = BinarySearchTree(10)
        self.t.insert(5)
        self.t.insert(2)
        self.t.insert(1)
        self.t.insert(3)
        self.t.insert(15)
        self.t.insert(12)
        self.t.insert(20)

    def test_delete_leaf_left(self):
        self.t.delete(12)
        self.assertIsNone(self.t.lookup(12)[0])
        self.assertEqual(self.t.lookup(15)[0].key, 15)
        self.assertEqual(self.t.lookup(20)[0].key, 20)

    def test_delete_leaf_right(self):
        self.t.delete(20)
        self.assertIsNone(self.t.lookup(20)[0])
        self.assertEqual(self.t.lookup(12)[0].key, 12)
        self.assertEqual(self.t.lookup(15)[0].key, 15)

    def test_delete_one_child(self):
        self.t.delete(5)
        self.assertIsNone(self.t.lookup(5)[0])
        self.assertEqual(self.t.lookup(1)[0].key, 1)
        self.assertEqual(self.t.lookup(2)[0].key, 2)
        self.assertEqual(self.t.lookup(3)[0].key, 3)
        self.assertEqual(self.t.lookup(10)[0].key, 10)

    def test_delete_parent(self):
        self.t.delete(2)
        self.assertIsNone(self.t.lookup(2)[0])
        self.assertEqual(self.t.lookup(5)[0].key, 5)
        self.assertEqual(self.t.lookup(3)[0].key, 3)
        self.assertEqual(self.t.lookup(1)[0].key, 1)

    def test_delete_no_parent(self):
        self.t.delete(10)
        self.assertIsNone(self.t.lookup(10)[0])
        self.assertEqual(self.t.lookup(5)[0].key, 5)

    def test_one_child_no_parent(self):
        #Test for one child and no parent, left side
        self.t.delete(15)
        self.t.delete(12)
        self.t.delete(20)
        self.t.delete(10)
        self.assertEqual(self.t.lookup(5)[0].key, 5)
        self.assertListEqual([x.key for x in self.t.lookup(2)], [2, 5])
        self.assertListEqual([x.key for x in self.t.lookup(1)], [1, 2])
        self.assertListEqual([x.key for x in self.t.lookup(3)], [3, 2])

    def test_traverse(self):
        self.assertEqual([1, 2, 3, 5, 10, 12, 15, 20],
                         [x.key for x in self.t.traverse()])

    def test_str(self):
        self.assertEqual(self.t.__str__(), '1, 2, 3, 5, 10, 12, 15, 20')
Esempio n. 23
0
 def setUp(self):
     self.ul = BinarySearchTree(100)
     self.ul.ints = [150, 50, 140, 40, 145, 160, 155, 157, 153, 55, 49, 51, 39, 41]
     for i in self.ul.ints:
         self.ul.insert(i)
Esempio n. 24
0
 def test_delete_tree(self):
     tree = BinarySearchTree(3)
     self.assertRaises(Exception, tree.delete, 3)
Esempio n. 25
0
 def test_lookup_left_not_exists(self):
     tree = BinarySearchTree(3)
     self.assertEqual(tree.lookup(1)[0], None)
Esempio n. 26
0
 def test_insert(self):
     bt = BinarySearchTree(5)
     self.assertTrue(bt.insert(2))
     self.assertTrue(bt.insert(3))
Esempio n. 27
0
class TreeTests(unittest.TestCase):

    def setUp(self):
        self.tree = BinarySearchTree(50)

    def text_insert(self):
        self.tree.insert(40)
        self.tree.insert(30)
        self.tree.insert(60)
        self.tree.insert(70)
        assertEqual([self.tree.left.left, self.tree.left, self.tree, self.right, self.right.right], self.tree.tranverse())

    def test_lookup(self):
        self.assertEqual(self.tree, self.tree.lookup(50)[0])
        self.tree.insert(40)
        self.tree.insert(60)
        self.assertEqual(self.tree.left, self.tree.lookup(40)[0])
        self.assertEqual(self.tree.right, self.tree.lookup(60)[0])
        self.assertEqual(None, self.tree.lookup(10)[0])

    def test_traverse(self):
        self.tree.insert(40)
        self.tree.insert(60)
        self.tree.insert(45)
        self.tree.insert(55)
        self.tree.insert(65)

        result = []
        for node in self.tree.traverse():
            result.append(node.key)

        self.assertEqual([40,45,50,55,60,65], result)

    def test_delete(self):
        self.tree.insert(25)
        self.tree.insert(30)
        self.tree.insert(20)
        self.tree.insert(27)
        self.tree.insert(23)
        self.tree.insert(24)
        self.tree.insert(19)

        self.tree.insert(75)
        self.tree.insert(70)
        self.tree.insert(80)
        self.tree.insert(72)
        self.tree.insert(77)


        self.tree2 = BinarySearchTree(50)
        self.tree2.insert(27)
        self.tree2.insert(30)
        self.tree2.insert(20)
        self.tree2.insert(23)
        self.tree2.insert(24)
        self.tree2.insert(19)

        self.tree2.insert(75)
        self.tree2.insert(80)
        self.tree2.insert(72)

        self.tree.delete(25)
        self.tree.delete(77)
        self.tree.delete(70)
        self.assertTrue(self.tree == self.tree2)


    def test_str(self):
        self.tree.insert(25)
        self.tree.insert(30)
        self.tree.insert(20)
        self.tree.insert(27)
        self.tree.insert(23)
        self.tree.insert(24)
        self.tree.insert(19)

        self.tree.insert(75)
        self.tree.insert(70)
        self.tree.insert(80)
        self.tree.insert(72)
        self.tree.insert(77)

        self.assertEqual(str(self.tree), str([19,20,23,24,25,27,30,50,70,72,75,77,80]))
Esempio n. 28
0
class TreeDeleteTests(unittest.TestCase):
	def setUp(self):
		#        10
		#       /   \
		#     5       15
		#    /       /  \
		#   2      12    20
		#  / \
		# 1   3

		self.t = BinarySearchTree(10)
		self.t.insert(5)
		self.t.insert(2)
		self.t.insert(1)
		self.t.insert(3)
		self.t.insert(15)
		self.t.insert(12)
		self.t.insert(20)

	def test_delete_leaf_left(self):
		self.t.delete(12)
		self.assertIsNone(self.t.lookup(12)[0])
		self.assertEqual(self.t.lookup(15)[0].key, 15)
		self.assertEqual(self.t.lookup(20)[0].key, 20)

	def test_delete_leaf_right(self):
		self.t.delete(20)
		self.assertIsNone(self.t.lookup(20)[0])
		self.assertEqual(self.t.lookup(12)[0].key, 12)
		self.assertEqual(self.t.lookup(15)[0].key, 15)

	def test_delete_one_child(self):
		self.t.delete(5)
		self.assertIsNone(self.t.lookup(5)[0])
		self.assertEqual(self.t.lookup(1)[0].key, 1)
		self.assertEqual(self.t.lookup(2)[0].key, 2)
		self.assertEqual(self.t.lookup(3)[0].key, 3)
		self.assertEqual(self.t.lookup(10)[0].key, 10)

	def test_delete_parent(self):
		self.t.delete(2)
		self.assertIsNone(self.t.lookup(2)[0])
		self.assertEqual(self.t.lookup(5)[0].key, 5)
		self.assertEqual(self.t.lookup(3)[0].key, 3)
		self.assertEqual(self.t.lookup(1)[0].key, 1)

	def test_delete_no_parent(self):
		self.t.delete(10)
		self.assertIsNone(self.t.lookup(10)[0])
		self.assertEqual(self.t.lookup(5)[0].key, 5)

	def test_one_child_no_parent(self):
	#Test for one child and no parent, left side
		self.t.delete(15)
		self.t.delete(12)
		self.t.delete(20)
		self.t.delete(10)
		self.assertEqual(self.t.lookup(5)[0].key, 5)
		self.assertListEqual([x.key for x in self.t.lookup(2)], [2, 5])
		self.assertListEqual([x.key for x in self.t.lookup(1)], [1, 2])
		self.assertListEqual([x.key for x in self.t.lookup(3)], [3, 2])


	def test_traverse(self):
		self.assertEqual([1, 2, 3, 5, 10, 12, 15, 20], [x.key for x in self.t.traverse()])

	def test_str(self):
		self.assertEqual(self.t.__str__(), '1, 2, 3, 5, 10, 12, 15, 20')
Esempio n. 29
0
 def setUp(self):
     self.tree = BinarySearchTree(50)
Esempio n. 30
0
class TreeTests(unittest.TestCase):
    def setUp(self):
        self.tree = BinarySearchTree(50)

    def text_insert(self):
        self.tree.insert(40)
        self.tree.insert(30)
        self.tree.insert(60)
        self.tree.insert(70)
        assertEqual([
            self.tree.left.left, self.tree.left, self.tree, self.right,
            self.right.right
        ], self.tree.tranverse())

    def test_lookup(self):
        self.assertEqual(self.tree, self.tree.lookup(50)[0])
        self.tree.insert(40)
        self.tree.insert(60)
        self.assertEqual(self.tree.left, self.tree.lookup(40)[0])
        self.assertEqual(self.tree.right, self.tree.lookup(60)[0])
        self.assertEqual(None, self.tree.lookup(10)[0])

    def test_traverse(self):
        self.tree.insert(40)
        self.tree.insert(60)
        self.tree.insert(45)
        self.tree.insert(55)
        self.tree.insert(65)

        result = []
        for node in self.tree.traverse():
            result.append(node.key)

        self.assertEqual([40, 45, 50, 55, 60, 65], result)

    def test_delete(self):
        self.tree.insert(25)
        self.tree.insert(30)
        self.tree.insert(20)
        self.tree.insert(27)
        self.tree.insert(23)
        self.tree.insert(24)
        self.tree.insert(19)

        self.tree.insert(75)
        self.tree.insert(70)
        self.tree.insert(80)
        self.tree.insert(72)
        self.tree.insert(77)

        self.tree2 = BinarySearchTree(50)
        self.tree2.insert(27)
        self.tree2.insert(30)
        self.tree2.insert(20)
        self.tree2.insert(23)
        self.tree2.insert(24)
        self.tree2.insert(19)

        self.tree2.insert(75)
        self.tree2.insert(80)
        self.tree2.insert(72)

        self.tree.delete(25)
        self.tree.delete(77)
        self.tree.delete(70)
        self.assertTrue(self.tree == self.tree2)

    def test_str(self):
        self.tree.insert(25)
        self.tree.insert(30)
        self.tree.insert(20)
        self.tree.insert(27)
        self.tree.insert(23)
        self.tree.insert(24)
        self.tree.insert(19)

        self.tree.insert(75)
        self.tree.insert(70)
        self.tree.insert(80)
        self.tree.insert(72)
        self.tree.insert(77)

        self.assertEqual(
            str(self.tree),
            str([19, 20, 23, 24, 25, 27, 30, 50, 70, 72, 75, 77, 80]))
Esempio n. 31
0
 def test_lookup_left_not_exists(self):
 	tree = BinarySearchTree(3)
 	self.assertEqual(tree.lookup(1)[0], None)
Esempio n. 32
0
    def test_delete(self):
        """Basic tests of the delete-method."""
        bt = BinarySearchTree(10)
        self.assertFalse(bt.delete(7))  # You can't delete non-existing nodes

        # It's possible to delete children that lack own children
        bt.insert(2)
        self.assertTrue(bt.delete(2))
        self.assertIsNone(bt.lookup(2))

        # It's possible to delete a node that have one other child
        #     10
        #    /
        #   3
        #    \
        #     4
        bt.insert(3)
        bt.insert(4)
        self.assertTrue(bt.delete(3))
        self.assertEqual(bt.left.key, 4)    # 4 should still be a child to root

        # It's possible to delete a node on the right side of the parent
        bt.insert(11)
        self.assertTrue(bt.delete(11))
        self.assertIsNone(bt.right)

        # Delete a node (on the right side of root) that have own children
        bt.insert(15)
        bt.insert(14)
        self.assertTrue(bt.delete(15))
        self.assertEqual(bt.right.key, 14)
        self.assertIsNone(bt.lookup(15))