Пример #1
0
    def test_search(self):
        avl = AVLTree(1, None,
                      None).add(2).add(3).add(4).add(5).add(-2).add(10)
        searched_node = avl.search(-2)
        expected_node = AVLTree(-2, None, None)

        self.assertEqual(searched_node, expected_node)
Пример #2
0
 def test_cmp(self):
     avl1 = AVLTree(1, None, None)
     avl2 = AVLTree(3, None, None)
     avl3 = AVLTree(3, None, None)
     self.assertGreater(avl2, avl1)
     self.assertTrue(not avl2 > avl3)
     self.assertTrue(not avl2 < avl3)
     self.assertTrue(avl2 == avl3)
Пример #3
0
    def test_init(self):
        avl = AVLTree(0, None, None)
        self.assertEqual(avl.height, 1)

        avl = AVLTree(0, AVLTree(None, None, None), None)
        self.assertEqual(avl.height, 2)

        avl = AVLTree(0, AVLTree(None, None, None), AVLTree(None, None, None))
        self.assertEqual(avl.height, 2)

        avl = AVLTree(None, AVLTree(None, AVLTree(None, None, None), None),
                      AVLTree(None, None, None))
        self.assertEqual(avl.height, 3)
Пример #4
0
    def test_pop(self):
        avl = AVLTree(
            50, AVLTree(17, AVLTree(12, None, None), AVLTree(23, None, None)),
            AVLTree(72, AVLTree(54, None, None), AVLTree(76, None, None)))

        avl = avl.pop(50)

        actual_list = list(avl)
        expected_list = sorted([17, 12, 23, 72, 54, 76], reverse=True)
        self.assertEqual(actual_list, expected_list)
Пример #5
0
    def test_iter(self):
        avl = AVLTree(
            50, AVLTree(17, AVLTree(12, None, None), AVLTree(23, None, None)),
            AVLTree(72, AVLTree(54, None, None), AVLTree(76, None, None)))

        expected_itered = [76, 72, 54, 50, 23, 17, 12]
        actual_itered = list(avl)

        self.assertEqual(actual_itered, expected_itered)
Пример #6
0
    def test_add(self):
        avl = AVLTree(50, AVLTree(17, None, AVLTree(23, None, None)),
                      AVLTree(72, None, None))

        avl = avl.add(13).add(-1).add(100).add(200).add(300).add(40).add(39)

        expected_itered = sorted(
            [50, 17, 23, 72, 13, -1, 100, 200, 300, 40, 39], reverse=True)
        actual_itered = list(avl)

        self.assertEqual(actual_itered, expected_itered)
Пример #7
0
    def test_in(self):
        avl = AVLTree(
            50, AVLTree(17, AVLTree(12, None, None), AVLTree(23, None, None)),
            AVLTree(72, AVLTree(54, None, None), AVLTree(76, None, None)))

        self.assertIn(50, avl)
        self.assertIn(17, avl)
        self.assertIn(12, avl)
        self.assertIn(23, avl)
        self.assertIn(72, avl)
        self.assertIn(54, avl)
        self.assertIn(76, avl)

        self.assertNotIn(200, avl)
        self.assertNotIn(0, avl)
Пример #8
0
Some tend to first look for x in the tree and then look for its predecessor.
However, x in not necessarily a key in the given tree, just some key with a
given value. Moreover, even if x is the tree, finding it first doesn't help.

To get a 5 stars feedback for problem solving, your peer must be able to explain
why it's possible to always store the last key smaller than x without comparing
it to the previously stored key.

If your peer is stuck, offer them to think about what they know of binary search
trees. If it doesn't help, ask how can this be applied for the solution.
"""

from trees import AVLTree

tree = AVLTree([2, 3, 4, 7, 17, 19, 21, 35, 89])
root = tree._root
tree.display()

def findLargestSmallerKey(root, x, result=-1):
	if not root:
		return result
	elif root.key >= x:
		return findLargestSmallerKey(root.left, x, result)
	else:
		return findLargestSmallerKey(root.right, x, root.key)

def findLargestSmallerKey2(root, x):
	result = 0
	while(root):
		if root.key >= x: root = root.left
Пример #9
0
    def test_remove_add_balanced(self):
        tree = AVLTree(4, None, None)
        tree.add(1).add(2).add(3).add(4).add(7).add(9).pop(4).pop(9).add(
            20).add(-2).add(21).add(-3).pop(-3)

        self.assertTrue(tree.is_balanced())
Пример #10
0
    def test_is_balanced(self):
        tree = AVLTree(4, None, None)
        self.assertTrue(tree.is_balanced())

        tree = AVLTree(None, AVLTree(None, AVLTree(None, None, None), None),
                       None)
        self.assertFalse(tree.is_balanced())

        tree = AVLTree(
            50, AVLTree(17, AVLTree(12, None, None), AVLTree(23, None, None)),
            AVLTree(72, AVLTree(54, None, None), AVLTree(76, None, None)))

        self.assertTrue(tree.is_balanced())
Пример #11
0
    def test_repr(self):
        avl = AVLTree(
            50, AVLTree(17, AVLTree(12, None, None), AVLTree(23, None, None)),
            AVLTree(72, AVLTree(54, None, None), AVLTree(76, None, None)))

        self.assertEqual(eval(repr(avl)), avl)
Пример #12
0
    def test_str(self):
        avl = AVLTree(
            50, AVLTree(17, AVLTree(12, None, None), AVLTree(23, None, None)),
            AVLTree(72, AVLTree(54, None, None), AVLTree(76, None, None)))

        self.assertEqual(str(avl), "76,72,54,50,23,17,12")
Пример #13
0
Some tend to first look for x in the tree and then look for its predecessor.
However, x in not necessarily a key in the given tree, just some key with a
given value. Moreover, even if x is the tree, finding it first doesn't help.

To get a 5 stars feedback for problem solving, your peer must be able to explain
why it's possible to always store the last key smaller than x without comparing
it to the previously stored key.

If your peer is stuck, offer them to think about what they know of binary search
trees. If it doesn't help, ask how can this be applied for the solution.
"""

from trees import AVLTree

tree = AVLTree([2, 3, 4, 7, 17, 19, 21, 35, 89])
root = tree._root
tree.display()


def findLargestSmallerKey(root, x, result=-1):
    if not root:
        return result
    elif root.key >= x:
        return findLargestSmallerKey(root.left, x, result)
    else:
        return findLargestSmallerKey(root.right, x, root.key)


def findLargestSmallerKey2(root, x):
    result = 0