Example #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]))
Example #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]))
Example #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))
Example #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]))
Example #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))
Example #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))
Example #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))
Example #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))
Example #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]))
Example #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]))
Example #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]))
Example #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))
Example #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))
Example #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]))
Example #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))
Example #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)
Example #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)
Example #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"])
Example #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)
Example #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)
Example #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]])
Example #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)
Example #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)
Example #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))
Example #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]))
Example #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)
Example #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())
Example #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())
Example #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)
Example #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)