Пример #1
0
def _addword(T, word, index):
    index += 1
    if index < len(word):
        notfound = True
        i = 0
        """
        if not T.children:
            bb = Tree((word[index],index = len(word)),[])
            _addword(bb, word, index)
            T.children.append(bb)
        """
        for kid in T.children:
            if (kid.key[0] == word[index]):
                _addword(kid, word, index)
                notfound = False
                break
            if (kid.key[0] > word[index]):
                bb = tree.Tree((word[index], index == len(word)), [])
                _addword(bb, word, index)
                T.children.insert(i, bb)
                notfound = False
                break
            i += 1
        if (notfound and index < len(word)):
            bb = tree.Tree((word[index], index == len(word)), [])
            _addword(bb, word, index)
            T.children.append(bb)
    if (index == len(word)):
        T.key = (T.key[0], True)
    return
Пример #2
0
def __create_subtree(word, i):
    if i == len(word) - 1:
        T = tree.Tree((word[i], True), [])
    else:
        T = tree.Tree((word[i], False), [])
    if i < len(word) - 1:
        T.children.append(__create_subtree(word, i + 1))
    return T
Пример #3
0
def treeasbin2tree(B):
    T = tree.Tree(B.key, [])
    child = B.child
    while child != None:
        T.children.append(treeasbin2tree(child))
        child = child.sibling
    return T
Пример #4
0
def bin2tree(b):
    t = tree.Tree(b.key)
    child = b.child
    while child:
        t.children.append(bin2tree(child))
        child = child.sibling
    return t
Пример #5
0
def bin2tree3(b, p=None):
    t = tree.Tree(b.key)
    if b.child:
        t.children.append(bin2tree3(b.child, t))
    if b.sibling:
        p.children.append(bin2tree3(b.sibling, p))
    t.children.reverse()
    return t
Пример #6
0
def bin2tree2(b, p=None):
    t = tree.Tree(b.key)
    if p:
        p.children.append(t)
    if b.child:
        bin2tree2(b.child, t)
    if b.sibling:
        bin2tree2(b.sibling, p)
    return t
Пример #7
0
def __treeasbin2tree2(B, parent):
    '''
    convert B -> added as new child of parent
    '''
    newChild = tree.Tree(B.key)
    parent.children.append(newChild)
    if B.sibling:
        __treeasbin2tree2(B.sibling, parent)
    if B.child:
        __treeasbin2tree2(B.child, newChild)
Пример #8
0
def treefromfile(filename):
    """ build the prefix tree from a file of words
    
    """
    reading = open(filename, "r")
    T = tree.Tree(('', False), [])
    content = reading.read().splitlines()
    for item in content:
        addword(T, item)
    reading.close()
    return T
Пример #9
0
def treefromfile(filename):
    """ build the prefix tree from a file of words

    :param filename: The file name
    :rtype: Tree
    """
    T = tree.Tree('', [])
    with open(filename, "r") as f:
        for line in f:
            addword(T, line.strip())
    return T
Пример #10
0
def __list2tree(s, i=0):
    i = i + 1  # to skip the '('
    key = ""
    while s[i] != '(' and s[i] != ')':  # s[i] not in "()"
        key += s[i]
        i += 1
    T = tree.Tree(int(key), [])
    while s[i] != ')':
        (C, i) = __list2tree(s, i)
        T.children.append(C)
    i += 1
    return (T, i)
Пример #11
0
def list2tree(s, pos=0, type=int):
    if pos < len(s) and s[pos] == '(':
        pos += 1
        w = ''
        while s[pos] not in '()':
            w += s[pos]
            pos += 1
        t = tree.Tree(type(w))
        while s[pos] != ')':
            c, pos = list2tree(s, pos, type)
            t.children.append(c)
        pos += 1
        return t, pos
    return None
Пример #12
0
def __list2tree(s, i=0):
    i = i + 1  # to skip the '('
    #        construire la clé = key
    key = ""
    while s[i] not in "()":
        key += s[i]
        i += 1
    T = tree.Tree(int(key), [])
    #        construire T.children : appels récursifs
    while s[i] != ')':  # or s[i] == '('
        (C, i) = __list2tree(s, i)
        T.children.append(C)

    i += 1  # to skip ')'
    return (T, i)
Пример #13
0
def treeasbin2tree2(B):
    T = tree.Tree(B.key)
    if B.child:
        __treeasbin2tree2(B.child, T)
    return T