def test_tree():
    print("test tree initialization")
    rb = RedBlackTree(0)
    assert rb.getValue() == 0
    left = RedBlackTree(1)
    left.getValue() == 1
    rb.addLeftTree(left)
    assert left.getParent() == rb
    assert rb.getValue() == 0
    assert rb.leftTree == left
    right = RedBlackTree(-1)
    right.getValue() == -1
    rb.addRightTree(right)
    assert rb.rightTree == right
    assert rb.getValue() == 0
    assert right.getParent() == rb
    right.getValue() == -1
    left.getValue() == 1

    rb = RedBlackTree(10)
    rb.binaryInsert(7)
    assert rb.getLeftTree().getValue() == 7
    rb.binaryInsert(12)
    assert rb.getRightTree().getValue() == 12
    rb.binaryInsert(14)
    assert rb.getRightTree().getRightTree().getValue() == 14
    print("pass tree initialization")
Beispiel #2
0
def user_in(user_input, words_file):
    
#    print(user_input == "AVL")     #USED TO DEBUG
    if user_input == 'AVL':
        AVL_list = read_file(words_file)
        english_words = AVLTree.AVLTree()
        
        for i in AVL_list:
#            a = i.split()
            english_words.insert(AVLTree.Node(i))
            
#        print_ascend (english_words.root)   #used to debug
        return english_words
      
    elif user_input == 'RBT':
        RBT_list = read_file(words_file)
        english_words = RedBlackTree.RedBlackTree()
        
        for i in RBT_list:
#            a = i.split
            english_words.insert(i)
            
#        print_ascend (english_words.root)   #used to debug
        return english_words
      
    else:
        print ('Invalid entry')
        print('Must be either AVL or RBT')
Beispiel #3
0
def loadRedBlackTree(words):
    tree = rbt.RedBlackTree()

    for key, word in words.items():
        tree.insert(word, compWords)

    return tree
Beispiel #4
0
    def root_insert(self, segment, bottom_segment=None):
        if self.current_state_tree is None:
            self.current_state_tree = RedBlackTree()

        segment.key = segment.update_key(self.x)
        if bottom_segment is not None:
            bottom_segment.key = bottom_segment.update_key(self.x)
            self.intersections_to_delete.append((segment, bottom_segment))

        self.current_state_tree.insert(segment, bottom_segment, self.x)
Beispiel #5
0
from Point import *
from RedBlackTree import *

rb_tree = RedBlackTree()

p1 = Point(1, 2)
p2 = Point(1, 4)
rb_tree.add(p1)
rb_tree.add(p2)

for node in rb_tree:
    print(node)
Beispiel #6
0
 def root_insert(self, segment, bottom_segment=None):
     if self.rb_tree is None:
         self.rb_tree = RedBlackTree()
     self.rb_tree.insert(segment, bottom_segment, x=self.x)
#################################################################
# Author: Yuhan Huang
# Date: 2019.12.30
# Github homepage: https://github.com/Krokette29
#################################################################

import random
from RedBlackTree import *

# test for randomly insertion and deletion
treeRBT = RedBlackTree()
num_nodes = 20

# insertion
key_list = [random.randint(1000, 2000) for i in range(num_nodes)]
value_list = [random.randint(0, 100) for i in range(1, num_nodes + 1)]
random.shuffle(key_list)
print("key list: {}".format(key_list))
print("value list: {}".format(value_list))
for i in range(num_nodes):
    treeRBT.insert(key_list[i], value_list[i])

treeRBT.check_all()
treeRBT.show_paths()

# show all information of all nodes
# for i in treeRBT:
#     i.show_info()

# deletion
delete_list = [i for i in range(num_nodes)]
        parent = tree.getParent(problem[0]) if problem[0] > 0 else (
            problem[0] == 0)
        print('Found problem at', problem,
              '' if parent else 'but missing expected parent')
        
        tree.printTree()

def printProgress(trial, trials, satisfied):
    print(('{} of {} trial{} complete, {:3.1f}% with '
           '{} satisfying all rules, {:3.1f}%').format(
               trial, trials, '' if trials == 1 else 's', 100 * trial / trials,
               satisfied, 100 * satisfied / trial))
    
if __name__ == '__main__':
    # random.seed(3.14159)  # Use fixed seed for testing consistency
    tree = RedBlackTree(title='Red-Black Tree Tester')
    values = [int(arg) for arg in sys.argv[1:] if arg.isdigit()
    ] if len(sys.argv) > 1 else [i for i in range(1, 96, 3)]

    rulesSatisfied = 0
    nNodes = 2 ** tree.MAX_LEVEL - 1
    trials = 1000
    for trial in range(trials):
        tree.emptyAndFill(nNodes)
        satisfied = len(tree.canvas.itemconfigure(tree.measures[3],
                                                  'text')[-1]) > 0
        height = tree.getHeight(0)
        tree.startAnimations()
        if satisfied:
            rulesSatisfied += 1
            print('Trial', trial, 'satisfied the rules')
Beispiel #9
0
from RedBlackTree import *
from Candidato import *
from random import randint
import re

eleitores = RedBlackTree()
titulosUsados = BinaryTree()
candidatos = {}


def addTitulo(numero):
    if numero.isdigit():
        if len(numero) == 4:
            numero = int(numero)
            if eleitores.search(numero) == None:
                eleitores.insert(numero)
                print("Título adicionado com sucesso!")
                return True
            else:
                print("Título já cadastrado!")
                return False
        else:
            print("O título deve ser composto de 4 digitos numéricos!")
            return False
    else:
        print("O título deve conter apenas números!")
        return False


def removerTitulo(numero):
    if numero.isdigit():
def test_left_rotate():
    print("Test left rotate")
    rb = RedBlackTree(5)
    left = RedBlackTree(3)
    right = RedBlackTree(7)
    rb.addLeftTree(left)
    rb.addRightTree(right)
    left_left = RedBlackTree(2)
    left_right = RedBlackTree(4)
    left.addLeftTree(left_left)
    left.addRightTree(left_right)

    rb.left_rotate()
    assert rb.getParent() == left
    assert rb.getRightTree() == right
    assert rb.getLeftTree() == left_right
    assert rb.getParent().getLeftTree() == left_left
    assert rb.getParent().getParent() == None
    assert rb.getRightTree().getLeftTree() == None
    assert rb.getRightTree().getRightTree() == None
    assert rb.getLeftTree().getRightTree() == None
    assert rb.getLeftTree().getLeftTree() == None
    assert rb.getParent().getLeftTree().getRightTree() == None
    assert rb.getParent().getLeftTree().getLeftTree() == None

    rb1 = RedBlackTree(5)
    right = RedBlackTree(7)
    rb1.addRightTree(right)
    rb1.left_rotate()
    assert rb1.getParent() == None
    assert rb1.getRightTree() == right
    assert rb1.getLeftTree() == None

    parent = RedBlackTree(10)
    parent.binaryInsert(24)
    parent.binaryInsert(3)
    parent.binaryInsert(29)
    parent.binaryInsert(16)
    parent.binaryInsert(12)
    parent.binaryInsert(17)
    other_center = parent.getLeftTree()
    central = parent.getRightTree()
    left = central.getLeftTree()
    right = central.getRightTree()
    left_right = left.getRightTree()
    left_left = left.getLeftTree()
    assert parent.getValue() == 10
    assert other_center.getValue() == 3
    assert central.getValue() == 24
    assert left.getValue() == 16
    assert right.getValue() == 29
    assert left_right.getValue() == 17
    assert left_left.getValue() == 12

    central.left_rotate()
    assert central.getRightTree() == right
    assert central.getLeftTree() == left_right
    assert central.getParent() == left
    assert left.getLeftTree() == left_left
    assert left.getParent() == parent
    assert parent.getRightTree() == left
    assert left.getRightTree() == central
    assert right.getParent() == central
    assert left_right.getParent() == central
    assert left_left.getParent() == left
    assert parent.getParent() == None
    assert parent.getLeftTree() == other_center
    assert other_center.getParent() == parent

    print("pass left rotate")
Beispiel #11
0
import RedBlackTree as RBT

temp = RBT.RedBlackTree()
temp.put(4, "Am")
temp.put(1, "Hello")
temp.put(2, "World")
temp.put(3, "I")
temp.put(5, "Your")
temp.put(6, "Creator")

print(
    temp.get(1) + " " + temp.get(2) + ", " + temp.get(3) + " " + temp.get(4) +
    " " + temp.get(5) + " " + temp.get(6) + ".")
Beispiel #12
0
 def setUp(self):
     self.rbt = RedBlackTree()
     self.rbt.insertValue(10)