Exemplo n.º 1
0
    def test_left_and_right_rotate(self):

        avl = AVLTree()
        avl.root = Node(3)
        avl.root.left = Node(2, parent=avl.root)
        avl.root.left.left = Node(1, parent=avl.root.left)
        avl.size = 3

        avl.right_rotate(avl.root)

        assert avl.root.value == 2
        assert avl.root.left.value == 1
        assert avl.root.right.value == 3
Exemplo n.º 2
0
 def test_left_and_right_rotate(self):
     # 1 normal right right rotation
     avl = AVLTree()
     avl.root = Node(3)
     avl.root.left = Node(2, parent=avl.root)
     avl.root.left.left = Node(1, parent=avl.root.left)
     avl.size = 3
     avl.right_rotate(avl.root)
     assert avl.root.value == 2
     assert avl.root.height == 1
     assert avl.root.left.value == 1
     assert avl.root.left.height == 0
     assert avl.root.right.value == 3
     assert avl.root.right.left == None
     assert avl.root.right.right == None
     assert avl.root.right.height == 0
Exemplo n.º 3
0
def create_avl_tree(file):
    words_in_my_tree = AVLTree()
    for line in file:
        word = line.strip("\n")
        words_in_my_tree.insert(Node(word))
    file.close()
    return words_in_my_tree
Exemplo n.º 4
0
def AVLT_method():
    # Create an empty AVLTree object.
    tree = AVLTree()

    # Insert some random-looking integers into the tree.
    keys = [10, 20, 5, 22, 15, 47, 19, 3, 12, 18]
    for key in keys:
        node = Node(key)
        tree.insert(node)

    # Print the tree after all inserts are complete.
    print("Tree after initial insertions:")
    print(tree)

    # Find and remove the node with the key value 12.
    # This should cause a right rotation on node 10.
    print("Remove node 12:")
    tree.remove_key(12)
    print(tree)

    # Find and remove the node with the key value 20.
    # This should cause its right child to shift up into
    # the 20 node's position without any other reordering
    # required.
    print("Remove node 20:")
    tree.remove_key(20)
    print(tree)

    # Attempt to remove a node with key 30, a value not in the tree.
    print("Remove node 30 (should not be in tree):")
    if not tree.remove_key(30):
        print("*** Key not found. Tree is not changed. ***")
    print(tree)
Exemplo n.º 5
0
def createTree(
):  #creates either an AVL Tree or Red-Black Tree and stores words as the keys
    tree_type = -1
    word_embeddings = open('glove.6B.50d.txt')

    while tree_type != '1' and tree_type != '2':
        print("Use AVL Tree or Red-Black Tree?")
        print("Type 1 for AVL")
        print("Type 2 for Red-Black")
        tree_type = input()

    if tree_type == '1':
        tree = AVLTree()
        #for i in range(50): #stores first 50 words
        for word in word_embeddings:  #stores all words in list
            #word = word_embeddings.readline()
            if word[0].isalpha(
            ) == True:  #ignores words that do not start with an alphabetic character
                node = Node(getWord(word), getWordEmbedding(word))
                tree.insert(node)
        word_embeddings.close()
        return tree

    if tree_type == '2':
        tree = RedBlackTree()
        #for i in range(50): #stores first 50 words
        for word in word_embeddings:  #stores all words in list
            #word = word_embeddings.readline()
            if word[0].isalpha() == True:
                tree.insert(getWord(word), getWordEmbedding(word))
        word_embeddings.close()
        return tree
Exemplo n.º 6
0
def avl_loader(file): # this method simply loads the user provided file into an AVL Tree
    avl_Tree = AVLTree()

    for line in file:
        word = line.strip('\n').lower()
        avl_Tree.insert(Node(word))
    file.close()

    return avl_Tree
Exemplo n.º 7
0
def create_avl(words):
        # create an avl tree
        avl_tree = AVLTree()
        # read the file
        file = open(words, "r")
        line = file.readline()
        # while loop to read all lines in file
        while line:
            # each word is added to the tree
            new_word = Node(line.rstrip().lower())
            avl_tree.AVL_Tree_Insert(new_word)
            line = file.readline()
        # return the tree
        return avl_tree
Exemplo n.º 8
0
def create_rb(words):
        # create an rb tree
        rb_tree = RBTree()
        # read the file
        file = open(words, "r")
        line = file.readline()
        # while loop to read all lines in file
        while line:
            # each word is added to the tree
            new_word = Node(line.rstrip().lower())
            rb_tree.insert(new_word)
            line = file.readline()
        # return the tree
        return rb_tree
Exemplo n.º 9
0
def read_file(): #Creates a Tree from the given file
    f = open('glove.6B.50d.txt', encoding="utf-8")
    line = f.readline()
    while line:
        _line = line.split(" ")
        word = _line[0]
        if word[0].isalpha():
            embedding_array = []
            for j in range(1,len(_line)):
                embedding_array.append(float(_line[j]))
            node = Node(word, embedding_array)
            try:
                tree.insert(node)
            except:
                tree.insert(word,embedding_array)
        line = f.readline()
    f.close()
Exemplo n.º 10
0
def createAVLTree(file_name):
    print("Creating AVL Tree...")
    avlTree = AVLTree()
    try:

        with open(file_name, encoding="utf8") as file:
            alphabet = list(string.ascii_letters)
            for line in file:
                array = line.split(" ")
                word = array[0]
                if alphabet.__contains__(word[0]):
                    numbers = []
                    for num in array[1:]:
                        numbers.append(float(num))
                    wordNode = Node(word, numbers)
                    avlTree.insert(wordNode)
    except FileNotFoundError:
        print("File not found...")
        exit()
    return avlTree
Exemplo n.º 11
0
 def setUp(self):
     self.tree = AVLTree(Node(2))
     self.tree.insert(1)
Exemplo n.º 12
0
from AVLTree import AVLTree, Node


# Create an empty AVLTree object.

tree = AVLTree()


# Insert some random-looking integers into the tree.

keys = [10, 20, 5, 22, 15, 47, 19, 3, 12, 18]

for key in keys:

    node = Node(key)

    tree.insert(node)


# Print the tree after all inserts are complete.

print("Tree after initial insertions:")

print(tree)


# Find and remove the node with the key value 12.

# This should cause a right rotation on node 10.

print("Remove node 12:")