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
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
def treeasbin2tree(B): T = tree.Tree(B.key, []) child = B.child while child != None: T.children.append(treeasbin2tree(child)) child = child.sibling return T
def bin2tree(b): t = tree.Tree(b.key) child = b.child while child: t.children.append(bin2tree(child)) child = child.sibling return t
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
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
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)
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
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
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)
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
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)
def treeasbin2tree2(B): T = tree.Tree(B.key) if B.child: __treeasbin2tree2(B.child, T) return T