Пример #1
0
    def testSuchen(self):
        tree = avlbaum.AVLBaum()
        l = [5, 3, 7, 2, 9, 4, 6, 1, 8, 10]
        for element in l:
            tree.einfuegen(element)

        self.assertEqual(tree.suchen(5), 5)
        self.assertIsNone(tree.suchen(11))
Пример #2
0
 def testLeeren(self):
     tree = avlbaum.AVLBaum()
     l = [5, 3, 7, 2, 9, 4, 6, 1, 8, 10]
     for element in l:
         tree.einfuegen(element)
     tree.leeren()
     
     self.assertIsNone(tree.root)
Пример #3
0
    def testHoleTiefe(self):
        tree = avlbaum.AVLBaum()
        l = [5, 3, 7, 2, 9, 4, 6, 1, 8, 10]
        for element in l:
            tree.einfuegen(element)

        self.assertEqual(tree.holeTiefe(avlbaum.AVLKnoten()), 0)
        self.assertEqual(tree.holeTiefe(tree.root), 1)
        self.assertEqual(tree.holeTiefe(tree.root.holeRechts().holeRechts().holeRechts()), 4)
Пример #4
0
    def testTraversieren(self):
        tree = avlbaum.AVLBaum()
        l = [5, 3, 7, 2, 9, 4, 6, 1, 8, 10]
        for element in l:
            tree.einfuegen(element)
        pre = [(5, 3), (3, 2), (2, 1), (1, 0), (4, 0), (7, 2), (6, 0), (9, 1), (8, 0), (10, 0)]
        post = [(1, 0), (2, 1), (4, 0), (3, 2), (6, 0), (8, 0), (10, 0), (9, 1), (7, 2), (5, 3)]

        self.assertEqual(pre, tree.traversieren("pre"))
        self.assertEqual(post, tree.traversieren("post"))
Пример #5
0
    def testLoeschenWurzel(self):
        tree = avlbaum.AVLBaum()
        l = [2, 3, 1]
        for element in l:
            tree.einfuegen(element)

        tree.loeschen(2)
        self.assertEqual(tree.root.holeWert(), 1)
        self.assertEqual(tree.root.holeRechts().holeWert(), 3)
        self.assertAllNodesBalanced(tree)
        self.assertNodesInOrder(tree, l[1:])
Пример #6
0
    def testLoeschenAlles(self):
        tree = avlbaum.AVLBaum()
        l = [5, 3, 7, 2, 9, 4, 6, 1, 8, 10]
        for element in l:
            tree.einfuegen(element)

        for n in range(len(l)):
            w = tree.root.holeWert()
            tree.loeschen(w)
            l.remove(w)
            self.assertAllNodesBalanced(tree)
            self.assertNodesInOrder(tree, l)
Пример #7
0
    def testEinfuegen(self):
        tree = avlbaum.AVLBaum()
        l = [5, 3, 7, 2, 9, 4, 6, 1, 8, 10]
        done = []
        for element in l:
            tree.einfuegen(element)
            done.append(element)
            self.assertAllNodesBalanced(tree)
            self.assertNodesInOrder(tree, done)

        self.assertEqual(tree.root.holeWert(), 5)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeRechts().holeWert(), 10)
Пример #8
0
def addToTree():
    """
    Add a node of the value from the Entry e_value to the tree

    parameters:     -
    return values:  -
    """
    global tree
    v = e_value.getText()
    if len(v) <= 0:
        return
    if tree == None:
        tree = avltree.AVLBaum()
    tree.einfuegen(float(v))
Пример #9
0
    def testDoppeltLinksRotation(self):
        tree = avlbaum.AVLBaum()
        l = [2, 1, 6, 7, 4]
        for element in l:
            tree.einfuegen(element)
        tree.einfuegen(5)
        
        self.assertAllNodesBalanced(tree)
        self.assertNodesInOrder(tree, l + [5])

        self.assertEqual(tree.root.holeWert(), 4)
        self.assertEqual(tree.root.holeLinks().holeWert(), 2)
        self.assertEqual(tree.root.holeLinks().holeLinks().holeWert(), 1)
        self.assertEqual(tree.root.holeRechts().holeWert(), 6)
        self.assertEqual(tree.root.holeRechts().holeLinks().holeWert(), 5)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeWert(), 7)
Пример #10
0
def createRandomTree():
    """
    Create a new random tree with a length and node values from the Entry e_length

    parameters:     -
    return values:  -
    """
    global tree
    l       = []
    length  = int(e_length.getText())
    for n in range(length):
        while True:
            number = random.randint(1, length)
            if number not in l:
              l.append(number)
              break
    tree = avltree.AVLBaum()
    for element in l:
        tree.einfuegen(element)
Пример #11
0
    def testDoppeltRechtsRotation(self):
        tree = avlbaum.AVLBaum()
        l = [3, 1, 7, 2, 8, 5, 4, 6]
        for element in l:
            tree.einfuegen(element)
        tree.einfuegen(6.5)
        
        self.assertAllNodesBalanced(tree)
        self.assertNodesInOrder(tree, l + [6.5])

        self.assertEqual(tree.root.holeWert(), 3)
        self.assertEqual(tree.root.holeLinks().holeWert(), 1)
        self.assertEqual(tree.root.holeLinks().holeRechts().holeWert(), 2)
        self.assertEqual(tree.root.holeRechts().holeWert(), 6)
        self.assertEqual(tree.root.holeRechts().holeLinks().holeWert(), 5)
        self.assertEqual(tree.root.holeRechts().holeLinks().holeLinks().holeWert(), 4)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeWert(), 7)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeLinks().holeWert(), 6.5)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeRechts().holeWert(), 8)
Пример #12
0
    def testEinfachRechtsRotation(self):
        tree = avlbaum.AVLBaum()
        l = [5, 3, 7, 2, 9, 4, 6, 1, 8, 10]
        for element in l:
            tree.einfuegen(element)
        tree.einfuegen(0)
        
        self.assertAllNodesBalanced(tree)
        self.assertNodesInOrder(tree, l + [0])

        self.assertEqual(tree.root.holeWert(), 5)
        self.assertEqual(tree.root.holeLinks().holeWert(), 3)
        self.assertEqual(tree.root.holeLinks().holeLinks().holeWert(), 1)
        self.assertEqual(tree.root.holeLinks().holeLinks().holeLinks().holeWert(), 0)
        self.assertEqual(tree.root.holeLinks().holeLinks().holeRechts().holeWert(), 2)
        self.assertEqual(tree.root.holeLinks().holeRechts().holeWert(), 4)
        self.assertEqual(tree.root.holeRechts().holeWert(), 7)
        self.assertEqual(tree.root.holeRechts().holeLinks().holeWert(), 6)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeWert(), 9)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeLinks().holeWert(), 8)
        self.assertEqual(tree.root.holeRechts().holeRechts().holeRechts().holeWert(), 10)
Пример #13
0
 def testKomplex(self):
     tree = avlbaum.AVLBaum()
     l = []
     amount = 200
     for i in range(0, amount):
         while 1:
             number = random.randint(1, amount)
             if number not in l:
               l.append(number)
               break
     done = []
     for element in l:
         tree.einfuegen(element)
         done.append(element)
         self.assertAllNodesBalanced(tree)
         self.assertNodesInOrder(tree, done)
     for element in l:
         tree.loeschen(element)
         done.remove(element)
         self.assertAllNodesBalanced(tree)
         self.assertNodesInOrder(tree, done)