コード例 #1
0
def AVL_read_file():
    word_file = open("english_words_two.txt", "r")
    line = word_file.readline()
    avl = AVLTree()

    for line in word_file:
        word = line.replace("\n", "")
        avl.AVL_insert(word)
    return avl
コード例 #2
0
 def setUp(self):
     self.avl = AVLTree()
     self.fullAVL = AVLTree()
     
     self.insertList = [47,30,2,6,12,64,62,98,93,95,97,99,3,4,5,7]
     for item in self.insertList:
         self.assertTrue(self.fullAVL.insert(item))
     self.result = '(None)<-93(BF:1)->(6,97)\n'\
              '(93)<-6(BF:0)->(4,30)\n'\
              '(93)<-97(BF:-1)->(95,98)\n'\
              '(6)<-4(BF:1)->(3,5)\n'\
              '(6)<-30(BF:0)->(12,62)\n'\
              '(97)<-95(BF:0)->(None,None)\n'\
              '(97)<-98(BF:-1)->(None,99)\n'\
              '(4)<-3(BF:1)->(2,None)\n'\
              '(4)<-5(BF:0)->(None,None)\n'\
              '(30)<-12(BF:1)->(7,None)\n'\
              '(30)<-62(BF:0)->(47,64)\n'\
              '(98)<-99(BF:0)->(None,None)\n'\
              '(3)<-2(BF:0)->(None,None)\n'\
              '(12)<-7(BF:0)->(None,None)\n'\
              '(62)<-47(BF:0)->(None,None)\n'\
              '(62)<-64(BF:0)->(None,None)\n'
コード例 #3
0
def populate_AVL(filename):
    file = open(filename, "r", encoding="utf8")
    tree = AVLTree()
    for line in file:
        current = line.strip().split(" ")
        # For each line in the file, if the word starts with an alphabetic
        # character, insert the word and embedding in a node within the tree
        if "a" <= current[0][0].lower() <= "z":
            embedding = []
            # Populate the embedding list
            for num in current[1:]:
                embedding.append(float(num))
            node = AVLNode(current[0].strip(), embedding)
            tree.insert(node)
    return tree
コード例 #4
0
def create_avl(file_name):
    english_words = AVLTree()

    # Open file and read first line
    file = open(file_name, "r")
    line = file.readline()

    # Loop will go trough every line in the file
    while line:
        # Add each word, lowercased, to avoid searching errors
        new_word = Node(line.rstrip().lower())
        english_words.insert(new_word)
        line = file.readline()

    # Returns AVL Tree
    return english_words
コード例 #5
0
    """
    if root == None:  # Empty Tree
        print("Tree is empty")
        return
    last_level = 2**root.height  # Maximum number of Nodes in last level + 1
    cq = CircularDeque(last_level)
    previous_level = 1  # Keep track of level of last printed node
    current = (root, previous_level)
    cq.insertFront(current)
    while not cq.isEmpty():
        current = cq.getFront()
        if current[0] != None:
            if previous_level < current[1]:
                print()
            print(current[0].val, end=" ")
            cq.insertRear((current[0].left, current[1] + 1))
            cq.insertRear((current[0].right, current[1] + 1))
            previous_level = current[1]
        cq.deleteFront()
    print("\n")


myTree = AVLTree()
root = None
nums = [33, 13, 52, 9, 21, 61, 8, 11]
for num in nums:
    root = myTree.insert(root, num)
printLevelOrder(root)
myTree.delete(root, 13)
printLevelOrder(root)
コード例 #6
0
from AVL_Tree import AVLTree
import random
import os

path = './tree_pic'
if not os.path.exists(path):
    os.mkdir(path)

# 创建一个生成器, 做图片的名称
'''
g = (path + '/tree' + str(i) + '.png' for i in range(1, 30))
'''
t = AVLTree()
# lst = [random.randrange(20, 300) for i in range(20)]
lst = random.sample(range(30, 300), 20)
t.insert(lst)
print(lst)
#t.draw(next(g))
print(t.get_tree_state())
for i in range(8):
    k = random.choice(lst)
    print("删除键%d" % k)
    t.delete_key(k)
    print(t.get_tree_state())  # 打印树的高度, 元素个数,树是否平衡

    #t.draw(next(g))

t.insert(-5, 200, 300)
t.insert(10, 0, 410, 15, 500)
#t.draw(next(g))
print(t.get_tree_state())