예제 #1
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]))
예제 #2
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))
예제 #3
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))
예제 #4
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]))
예제 #5
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))
예제 #6
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]))
예제 #7
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]))
예제 #8
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))
예제 #9
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))
예제 #10
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]))
예제 #11
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))
예제 #12
0
    def __init__(self):
        self.__top = None

        self.__op  = Tree()

        self.__push = AVLTree()
        self.__pop  = AVLTree()
        self.__cur_time = 0
예제 #13
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]))
예제 #14
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))
예제 #15
0
    def test_put_withNoneTypeArgumentValue_shouldRemoveAssociatedKeyFromBSTDecreaseLengthAndMaintainBalanceFactor(
            self):
        bst = AVLTree()
        bst.put("A", 1)
        bst.put("B", 2)
        bst.put("C", 3)
        bst.put("D", 4)

        self.assertIn("A", bst)
        self.assertEqual(4, len(bst))
        self.assertEqual(2, bst.height())

        bst.put("A", None)
        self.assertNotIn("A", bst)
        self.assertEqual(3, len(bst))
        self.assertEqual(1, bst.height())
예제 #16
0
    def test_put_withCompareFunctionAnddNoneTypeArgumentValue_shouldRemoveAssociatedKeyFromBSTDecreaseLengthAndMaintainBalanceFactor(
            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.assertEqual(4, len(bst))
        self.assertEqual(2, bst.height())

        bst.put([0, 1], None)
        self.assertNotIn([0, 1], bst)
        self.assertEqual(3, len(bst))
        self.assertEqual(1, bst.height())
예제 #17
0
    def test_get_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.get("A"))
        self.assertEqual(2, bst.get("B"))
        self.assertEqual(3, bst.get("C"))
        self.assertEqual(4, bst.get("D"))
예제 #18
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)
예제 #19
0
    def test_get_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.get([0, 0]))
        self.assertEqual(2, bst.get([0, 1]))
        self.assertEqual(3, bst.get([0, 2]))
        self.assertEqual(4, bst.get([0, 3]))
예제 #20
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)
예제 #21
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)
예제 #22
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)
예제 #23
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))
예제 #24
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]))
예제 #25
0
    def test_keys_withNoneTypeArgument_shouldRaiseValueError(self):
        bst = AVLTree()

        with self.assertRaises(ValueError):
            bst.keys(None, 1)
        with self.assertRaises(ValueError):
            bst.keys(1, None)
예제 #26
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))
예제 #27
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]))
예제 #28
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)
예제 #29
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)
예제 #30
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)