def test_construct():
    seq1 = [1, 2, 4, 7, 3, 5, 6, 8]
    seq2 = [4, 7, 2, 1, 5, 3, 8, 6]
    btree = construct(seq1, seq2)
    print('*', btree)
    tmp = BinTree()
    tmp.preorder_trav(btree)
def test_find_path():
    btree = BinTree()
    btree.add(10)
    btree.add(5)
    btree.add(12)
    btree.add(4)
    btree.add(7)
    find_path(btree.root, 22)
    find_path(btree.root, 17)
def construct_core(preorder, start_preorder, end_preorder, inorder,
                   start_inorder, end_inorder):
    """
    先根据前序遍历序列的第一个数字创建根结点,接下来在中序遍历序列中找到根结点的位置,
    这样就能确定左、右子树结点的数量。在前序遍历和中序遍历的序列中划分了左、右子树结
    点的值之后,就可以递归地去分别构建它的左右子树。
    :param preorder:
    :param start_preorder:
    :param end_preorder:
    :param inorder:
    :param start_inorder:
    :param end_inorder:
    :return:
    """
    # 前序遍历的第一个数字是根结点的值
    root_value = preorder[start_preorder]
    root = BinTree(root=root_value)
    root.left = root.right = None
    root.data = root.root
    if start_preorder == end_preorder:
        if start_inorder == end_inorder and inorder[start_inorder] == inorder[
                end_inorder]:
            return root
        else:
            raise Exception("错误的输入")

    # 在中序遍历中找到根结点的值
    root_inorder = start_inorder
    while root_inorder <= end_inorder and inorder[root_inorder] != root_value:
        root_inorder += 1
    # 如果两个序列不匹配
    if root_inorder == end_inorder and inorder[root_inorder] != root_value:
        raise Exception("两个序列不匹配!", root_value, inorder[root_value])
    left_length = root_inorder - start_inorder
    left_preorder_end = start_preorder + left_length
    # 构建左子树
    if left_length > 0:
        root.left = construct_core(preorder, start_preorder + 1,
                                   left_preorder_end, inorder, start_inorder,
                                   root_inorder - 1)
    # 构建右子树
    if left_length < end_preorder - start_preorder:
        # root.right = construct_core(preorder, left_preorder_end + 1, end_preorder, inorder, root_inorder + 1,
        #                             end_inorder)
        root.right = construct_core(preorder, left_preorder_end + 1,
                                    end_preorder, inorder, root_inorder + 1,
                                    end_inorder)
    return root
def test_has_subtree():
    node_list = [
        {
            'data': 'A',
            'left': 'B',
            'right': 'C',
            'is_root': True
        },
        {
            'data': 'B',
            'left': 'D',
            'right': 'E',
            'is_root': False
        },
        {
            'data': 'D',
            'left': None,
            'right': None,
            'is_root': False
        },
        {
            'data': 'E',
            'left': 'H',
            'right': None,
            'is_root': False
        },
        {
            'data': 'H',
            'left': None,
            'right': None,
            'is_root': False
        },
        {
            'data': 'C',
            'left': 'F',
            'right': 'G',
            'is_root': False
        },
        {
            'data': 'F',
            'left': None,
            'right': None,
            'is_root': False
        },
        {
            'data': 'G',
            'left': 'I',
            'right': 'J',
            'is_root': False
        },
        {
            'data': 'I',
            'left': None,
            'right': None,
            'is_root': False
        },
        {
            'data': 'J',
            'left': None,
            'right': None,
            'is_root': False
        },
    ]

    btree1 = BinTree.build_from(node_list)
    node_list2 = [
        {
            'data': 'A',
            'left': 'B',
            'right': 'D',
            'is_root': True
        },
        {
            'data': 'B',
            'left': None,
            'right': None,
            'is_root': False
        },
        {
            'data': 'D',
            'left': None,
            'right': None,
            'is_root': False
        },
    ]

    btree2 = BinTree.build_from(node_list2)
    print(has_subtree(btree1.root, btree2.root))
Esempio n. 5
0
"""
Author: Novanator 2012
Program: Translation3
"""
import random
from btree import Node
from btree import BinTree

file = open ("list.txt", "r")
contents = file.read ()
words = contents.split ()

random.shuffle(words)

tree=BinTree()
for i in words:
    tree.put(i)
    
l=[]
for x in words:
    if tree.exists(x[::-1]) and x not in l:
        l.append(x)
        
l2=[]

for x in l:
    if x not in l2:
        l2.append(x)
        l2.append(x[::-1])
        
for i in range (0,len(l2),2):
def test_print_from_top_to_bottom():
    # 空树
    btree = BinTree()
    print_from_top_to_bottom(btree.root)
    # 单结点树
    btree = BinTree()
    btree.add(8)
    print_from_top_to_bottom(btree.root)
    # 多结点树
    btree = BinTree()
    btree.add(8)
    btree.add(6)
    btree.add(10)
    btree.add(5)
    btree.add(7)
    btree.add(9)
    # btree.add(11)
    print_from_top_to_bottom(btree.root)
Esempio n. 7
0
        if (lista[0] + x + lista[2]) == endword:
            writeChain(b)
            return True

        if (lista[0] + lista[1] + x) == endword:
            writeChain(c)
            return True

    return False


endword = input("Write the end word:")
f = open("list.txt", "r")
contents = f.read()
words = contents.split()
tree = BinTree()
for i in words:
    tree.put(i)

random.shuffle(words)
letter = [
    "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o",
    "p", "q", "r", "s", "t", "u", "v", "x", "y", "z", "å", "ä", "ö"
]

plista = []

for x in words:
    klist = []
    oldwords = BinTree()
    q = Queue()
def test_convert():
    btree = BinTree()
    btree.add(10)
    btree.add(6)
    btree.add(14)
    btree.add(4)
    btree.add(8)
    btree.add(12)
    btree.add(16)
    convert(btree.root)
Esempio n. 9
0
"""
Author: Novanator 2012
Program: Translation3
"""
import random
from btree import Node
from btree import BinTree

file = open("list.txt", "r")
contents = file.read()
words = contents.split()

random.shuffle(words)

tree = BinTree()
for i in words:
    tree.put(i)

l = []
for x in words:
    if tree.exists(x[::-1]) and x not in l:
        l.append(x)

l2 = []

for x in l:
    if x not in l2:
        l2.append(x)
        l2.append(x[::-1])

for i in range(0, len(l2), 2):
Esempio n. 10
0
def test_mirror_use_stack():
    # 空结点树
    tree = BinTree()
    print(mirror_use_stack(tree.root))
    # 一个结点的树
    tree = BinTree()
    tree.add('A')
    mirror_use_stack(tree.root)
    tree.preorder_trav(tree.root)
    # 多结点树
    tree = BinTree()
    tree.add('A')
    tree.add('B')
    tree.add('C')
    tree.add('D')
    tree.add('E')
    tree.add('F')
    tree.add('G')
    print("-------------------镜像前-------------------")
    tree.preorder_trav(tree.root)
    mirror_use_stack(tree.root)
    print("-------------------镜像后-------------------")
    tree.preorder_trav(tree.root)
Esempio n. 11
0
                if (lista[0]+x+lista[2])==endword:
                    writeChain(b)
                    return True
                
                if (lista[0]+lista[1]+x)==endword:
                    writeChain(c)
                    return True
                
        return False


endword = input("Write the end word:" )
f=open("list.txt","r")
contents = f.read ()
words = contents.split ()
tree=BinTree()
for i in words:
    tree.put(i)

random.shuffle(words)
letter=["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","x","y","z","å","ä","ö"]

plista=[]

for x in words:
    klist=[]
    oldwords=BinTree()
    q=Queue()

    startword = Nodeson(x)
    q.put(startword)