Ejemplo n.º 1
0
 def test_rank_withCompareFunctionAndKeyNotInBSTAndGreaterThanEveryKey_shouldReturnBSTSize(
         self):
     bst = AVLTree(self.cmp)
     for i in range(10):
         bst.put([i, 0], 0)
     for i in range(10, 20):
         self.assertEqual(bst.size(), bst.rank([i, 0]))
Ejemplo n.º 2
0
 def test_ceiling_withCompareFunctionAndArgumentNotInBSTHavingCeilingValue_shouldReturnSmallerStrictlyGreaterElementFromBST(
         self):
     bst = AVLTree(self.cmp)
     for i in range(10):
         bst.put([0, i], i)
     for i in range(10):
         self.assertEqual([0, i], bst.ceiling([0, i - 0.5]))
Ejemplo n.º 3
0
 def test_rank_withKeyNotInBSTAndAmongKeys_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey(
         self):
     bst = AVLTree()
     for i in range(0, 20, 2):
         bst.put(i, 0)
     for i in range(1, 20, 2):
         self.assertEqual((i + 1) / 2, bst.rank(i))
Ejemplo n.º 4
0
 def test_floor_withCompareFunctionAndArgumentNotInBSTHavingFloorValue_shouldReturnGreaterStrictlyLowerElementFromBST(
         self):
     bst = AVLTree(self.cmp)
     for i in range(10):
         bst.put([0, i], i)
     for i in range(10):
         self.assertEqual([0, i], bst.floor([0, i + 0.5]))
Ejemplo n.º 5
0
 def test_ceiling_withArgumentNotInBSTHavingCeilingValue_shouldReturnSmallerStrictlyGreaterElementFromBST(
         self):
     bst = AVLTree()
     for i in range(10):
         bst.put(i, i)
     for i in range(10):
         self.assertEqual(i, bst.ceiling(i - 0.5))
Ejemplo n.º 6
0
 def test_rank_withKeyInBST_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey(
         self):
     bst = AVLTree()
     for i in range(20):
         bst.put(i, 0)
     for i in range(20):
         self.assertEqual(i, bst.rank(i))
Ejemplo n.º 7
0
 def test_floor_withArgumentNotInBSTHavingFloorValue_shouldReturnGreaterStrictlyLowerElementFromBST(
         self):
     bst = AVLTree()
     for i in range(10):
         bst.put(i, i)
     for i in range(10):
         self.assertEqual(i, bst.floor(i + 0.5))
Ejemplo n.º 8
0
 def test_rank_withKeyNotInBSTAndGreaterThanEveryKey_shouldReturnBSTSize(
         self):
     bst = AVLTree()
     for i in range(10):
         bst.put(i, 0)
     for i in range(10, 20):
         self.assertEqual(bst.size(), bst.rank(i))
Ejemplo n.º 9
0
 def test_rank_withCompareFunctionAndKeyNotInBSTAndSmallerThanEveryKey_shouldReturnZero(
         self):
     bst = AVLTree(self.cmp)
     for i in range(10, 20):
         bst.put([i, 0], 0)
     for i in range(10):
         self.assertEqual(0, bst.rank([i, 0]))
Ejemplo n.º 10
0
 def test_rank_withCompareFunctionAndKeyInBST_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey(
         self):
     bst = AVLTree(self.cmp)
     for i in range(20):
         bst.put([i, 0], 0)
     for i in range(20):
         self.assertEqual(i, bst.rank([i, 0]))
Ejemplo n.º 11
0
 def test_rank_withCompareFunctionAndKeyNotInBSTAndAmongKeys_shouldReturnCorrectNumberOfKeysStrictlySmallerThanGivenKey(
         self):
     bst = AVLTree(self.cmp)
     for i in range(0, 20, 2):
         bst.put([i, 0], 0)
     for i in range(1, 20, 2):
         self.assertEqual((i + 1) / 2, bst.rank([i, 0]))
Ejemplo n.º 12
0
    def select_withValidK_shouldReturnTheKthSmallestElementInBST(self):
        bst = AVLTree()
        for i in range(20):
            bst.put(i, i)

        for i in range(20):
            self.assertEqual(i, bst.select(i))
Ejemplo n.º 13
0
    def select_withCompareFunctionAndValidK_shouldReturnTheKthSmallestElementInBST(
            self):
        bst = AVLTree(self.cmp)
        for i in range(20):
            bst.put([i, 0], i)

        for i in range(20):
            self.assertEqual(i, bst.select(i))
Ejemplo n.º 14
0
 def test_successor_withCompareFunctionAndKeyInBST_shouldReturnInOrderSuccessorOfGivenKey(
         self):
     bst = AVLTree(self.cmp)
     for i in range(10):
         bst.put([i, 0], 0)
     for i in range(10):
         if i == 9:
             self.assertIsNone(bst.successor([i, 0]))
         else:
             self.assertEqual([i + 1, 0], bst.successor([i, 0]))
Ejemplo n.º 15
0
 def test_successor_withKeyInBST_shouldReturnInOrderSuccessorOfGivenKey(
         self):
     bst = AVLTree()
     for i in range(10):
         bst.put(i, 0)
     for i in range(10):
         if i == 9:
             self.assertIsNone(bst.successor(i))
         else:
             self.assertEqual(i + 1, bst.successor(i))
Ejemplo n.º 16
0
    def test_contains_withKeyNotInBST_shouldReturnFalse(self):
        bst = AVLTree()
        bst.put("A", 1)
        bst.put("B", 2)
        bst.put("C", 3)
        bst.put("D", 4)

        self.assertNotIn("E", bst)
        self.assertNotIn("F", bst)
        self.assertNotIn("G", bst)
Ejemplo n.º 17
0
    def test_contains_withKeyInBST_shouldReturnTrue(self):
        bst = AVLTree()
        bst.put("A", 1)
        bst.put("B", 2)
        bst.put("C", 3)
        bst.put("D", 4)

        self.assertIn("A", bst)
        self.assertIn("B", bst)
        self.assertIn("C", bst)
        self.assertIn("D", bst)
Ejemplo n.º 18
0
    def test_getItem_withKeyInBST_shouldReturnAssociatedValue(self):
        bst = AVLTree()
        bst.put("A", 1)
        bst.put("B", 2)
        bst.put("C", 3)
        bst.put("D", 4)

        self.assertEqual(1, bst["A"])
        self.assertEqual(2, bst["B"])
        self.assertEqual(3, bst["C"])
        self.assertEqual(4, bst["D"])
Ejemplo n.º 19
0
    def test_contains_withCompareFunctionAndKeyInBST_shouldReturnTrue(self):
        bst = AVLTree(self.cmp)
        bst.put([0, 0], 1)
        bst.put([0, 1], 2)
        bst.put([0, 2], 3)
        bst.put([0, 3], 4)

        self.assertIn([0, 1], bst)
        self.assertIn([0, 2], bst)
        self.assertIn([0, 3], bst)
        self.assertIn([0, 3], bst)
Ejemplo n.º 20
0
    def test_select_withArgumentOutOfBSTRange_shouldRaiseValueError(self):
        bst = AVLTree()
        for i in range(20):
            bst.put(i, i)

        for i in range(-20, 0):
            with self.assertRaises(ValueError):
                bst.select(i)
        for i in range(20, 40):
            with self.assertRaises(ValueError):
                bst.select(i)
Ejemplo n.º 21
0
    def test_getItem_withCompareFunctionAndKeyInBST_shouldReturnAssociatedValue(
            self):
        bst = AVLTree(self.cmp)
        bst.put([0, 0], 1)
        bst.put([0, 1], 2)
        bst.put([0, 2], 3)
        bst.put([0, 3], 4)

        self.assertEqual(1, bst[[0, 0]])
        self.assertEqual(2, bst[[0, 1]])
        self.assertEqual(3, bst[[0, 2]])
        self.assertEqual(4, bst[[0, 3]])
Ejemplo n.º 22
0
    def test_select_withCompareFunctionAndArgumentOutOfBSTRange_shouldRaiseValueError(
            self):
        bst = AVLTree(self.cmp)
        for i in range(20):
            bst.put([i, 0], i)

        for i in range(-20, 0):
            with self.assertRaises(ValueError):
                bst.select(i)
        for i in range(20, 40):
            with self.assertRaises(ValueError):
                bst.select(i)
Ejemplo n.º 23
0
    def test_delete_withKeyNotInBST_shouldNotModifyBST(self):
        bst = AVLTree()
        bst.put("A", 1)
        bst.put("B", 2)
        bst.put("C", 3)
        bst.put("D", 4)

        bst.delete("G")
        self.assertEqual(4, len(bst))
        self.assertIn("A", bst)
        self.assertIn("B", bst)
        self.assertIn("C", bst)
        self.assertIn("D", bst)
Ejemplo n.º 24
0
    def test_keys_withNotEmptyBSTAndArgumentsNotInBST_shouldReturnListOfKeysInGivenRangeExcludingGivenArguments(
            self):
        bst = AVLTree()
        for i in range(0, 20, 2):
            bst.put(i, i)

        for i in range(-1, 20, 2):
            for j in range(i, 20, 2):
                if i == j:
                    expected = []
                else:
                    expected = [e for e in range(i + 1, j + 1, 2)]
                self.assertEqual(expected, bst.keys(i, j))
Ejemplo n.º 25
0
    def test_keys_withCompareFunctionAndNotEmptyBST_shouldReturnListOfKeysInGivenRangeIncludingGivenArguments(
            self):
        bst = AVLTree(self.cmp)
        for i in range(20):
            bst.put([0, i], i)

        for i in range(20):
            for j in range(i, 20):
                if i == j:
                    expected = [[0, i]]
                else:
                    expected = [[0, e] for e in range(i, j + 1)]
                self.assertEqual(expected, bst.keys([0, i], [0, j]))
Ejemplo n.º 26
0
    def test_delete_withCompareFunctionAndKeyNotInBST_shouldNotModifyBST(self):
        bst = AVLTree(self.cmp)
        bst.put([0, 0], 1)
        bst.put([0, 1], 2)
        bst.put([0, 2], 3)
        bst.put([0, 3], 4)

        bst.delete([0, 69])
        self.assertEqual(4, len(bst))
        self.assertIn([0, 1], bst)
        self.assertIn([0, 2], bst)
        self.assertIn([0, 3], bst)
        self.assertIn([0, 3], bst)
Ejemplo n.º 27
0
    def test_delete_withCompareFunctionAndKeyInBST_shouldDeleteKeyFromBSTDecreaseLengthAndMaintainBalanceFactor(
            self):
        bst = AVLTree(self.cmp)
        bst.put([0, 0], 1)
        bst.put([0, 1], 2)
        bst.put([0, 2], 3)
        bst.put([0, 3], 4)
        self.assertEqual(2, bst.height())

        bst.delete([0, 1])
        self.assertEqual(3, len(bst))
        self.assertEqual(1, bst.height())

        bst.delete([0, 2])
        self.assertEqual(2, len(bst))
        self.assertEqual(1, bst.height())
Ejemplo n.º 28
0
    def test_delete_withKeyInBST_shouldDeleteKeyFromBSTDecreaseLengthAndMaintainBalanceFactor(
            self):
        bst = AVLTree()
        bst.put("A", 1)
        bst.put("B", 2)
        bst.put("C", 3)
        bst.put("D", 4)
        self.assertEqual(2, bst.height())

        bst.delete("A")
        self.assertEqual(3, len(bst))
        self.assertEqual(1, bst.height())

        bst.delete("C")
        self.assertEqual(2, len(bst))
        self.assertEqual(1, bst.height())
Ejemplo n.º 29
0
    def test_deleteMin_withCompareFunctionAndNotEmptyBST_shouldDeleteMinKeyFromBSTAndDecreaseBSTLength(
            self):
        bst = AVLTree(self.cmp)
        bst.put([0, 0], 1)
        bst.put([0, 1], 2)
        bst.put([0, 2], 3)
        bst.put([0, 3], 4)

        bst.delete_min()
        self.assertEqual(3, len(bst))
        self.assertNotIn([0, 0], bst)

        bst.delete_min()
        self.assertEqual(2, len(bst))
        self.assertNotIn([0, 1], bst)

        bst.delete_min()
        self.assertEqual(1, len(bst))
        self.assertNotIn([0, 2], bst)

        bst.delete_min()
        self.assertEqual(0, len(bst))
        self.assertNotIn([0, 3], bst)
Ejemplo n.º 30
0
    def test_deleteMin_withNotEmptyBST_shouldDeleteMinKeyFromBSTAndDecreaseBSTLength(
            self):
        bst = AVLTree()
        bst.put("A", 1)
        bst.put("B", 2)
        bst.put("C", 3)
        bst.put("D", 4)

        bst.delete_min()
        self.assertEqual(3, len(bst))
        self.assertNotIn("A", bst)

        bst.delete_min()
        self.assertEqual(2, len(bst))
        self.assertNotIn("B", bst)

        bst.delete_min()
        self.assertEqual(1, len(bst))
        self.assertNotIn("C", bst)

        bst.delete_min()
        self.assertEqual(0, len(bst))
        self.assertNotIn("D", bst)