Пример #1
0
class TestAVLMethods(unittest.TestCase):

    def setUp(self):
        self.bst = BinaryTree()
        
    def tearDown(self):
        self.bst = None
        
    def test_basic(self):
        self.bst.add(10)
        self.assertTrue(10 in self.bst)
        
    def test_adding_and_removing(self):
        vals = []
        for _ in range(1000):
            n = random.randint(1,100000)
            vals.append(n)
            if not n in self.bst:
                self.bst.add(n)
                self.assertTrue(self.bst.assertAVLProperty())
           
        for r in vals:
            self.bst.remove(r)
            self.assertFalse(r in self.bst)
            self.assertTrue(self.bst.assertAVLProperty())
Пример #2
0
def performanceAVL():
    """Demonstrate AVL properties always hold by building and deconstructing AVL trees from scratch."""
    n = 4

    while n <= 1024:

        bt = BinaryTree()
        added = []
        for i in range(n):
            new = i
            if not new in bt:
                bt.add(new)
                added.append(new)

                if not bt.root.assertAVLProperty():
                    print(bt)
                    assert False

        random.shuffle(added)

        for i in added:
            bt.remove(i)
            if (bt.root):
                if not bt.root.assertAVLProperty():
                    print(bt)
                    assert False
            assert i not in bt

        print("pass " + str(n))

        n *= 2
Пример #3
0
import random
from adk.avl import BinaryTree
"""
Generate numerous trees of size n and determine range of heights in the AVL trees. 
Not definitive, but indicative of the minimum & maximum height of an AVL tree given n nodes
"""
for n in range(1, 511):
    minH = 999
    maxH = 0
    for _ in range(1000):
        bst = BinaryTree()
        for i in range(n):
            val = random.randint(1, 100000)
            bst.add(val)

        if bst.root.height < minH:
            minH = bst.root.height
        if bst.root.height > maxH:
            maxH = bst.root.height

    print(n, minH, maxH)
Пример #4
0
 def setUp(self):
     self.bst = BinaryTree()
Пример #5
0
import random
from adk.avl import BinaryTree

"""
Generate numerous trees of size n and determine range of heights in the AVL trees. 
Not definitive, but indicative of the minimum & maximum height of an AVL tree given n nodes
"""
for n in range(1,511):
    minH = 999
    maxH = 0
    for _ in range(1000):
        bst = BinaryTree()
        for i in range(n):
            val = random.randint(1,100000)
            bst.add(val)
        
        if bst.root.height < minH:
            minH = bst.root.height
        if bst.root.height > maxH:
            maxH = bst.root.height
    
    print (n, minH, maxH)
    
Пример #6
0
 def setUp(self):
     self.bst = BinaryTree()