試試這個:
def toTree(expression):
tree = dict()
msg =""
stack = list()
for char in expression:
if(char == '('):
stack.append(msg)
msg = ""
elif char == ')':
parent = stack.pop()
if parent not in tree:
tree[parent] = list()
tree[parent].append(msg)
msg = parent
else:
msg += char
return tree
expression = "(Root(AB(ABC)(CBA))(CD(CDE)(FGH)))"
print toTree(expression)
它返回一個字典,其中根可以用鑰匙'來訪問。然後你可以做一個簡單的BFS來打印輸出。
OUTPUT:
{
'' : ['Root'],
'AB' : ['ABC', 'CBA'],
'Root': ['AB', 'CD'],
'CD' : ['CDE', 'FGH']
}
您必須消除表達所有的空格,您開始之前,或通過添加以下作爲的第一行用於忽略表達inrrelevant charaters -loop:
if char == <IRRELEVANT CHARACTER>:
continue
上述代碼將在O(n)時間運行,其中n是表達式的長度。
EDIT
這裏是打印功能:
def printTree(tree, node):
if node not in tree:
return
print '%s -> %s' % (node, ' '.join(child for child in tree[node]))
for child in tree[node]:
printTree(tree, child)
期望的輸出可通過以下來實現:
expression = "(Root(AB(ABC)(CBA))(CD(CDE)(FGH)))"
tree = toTree(expression)
printTree(tree, tree[''][0])
輸出
Root -> AB CD
AB -> ABC CBA
CD -> CDE FGH
編輯
假設節點名稱不是唯一的,我們只需要給新名稱的節點。
def parseExpression(expression):
nodeMap = dict()
counter = 1
node = ""
retExp =""
for char in expression:
if char == '(' or char == ')' :
if (len(node) > 0):
nodeMap[str(counter)] = node;
retExp += str(counter)
counter +=1
retExp += char
node =""
elif char == ' ': continue
else :
node += char
return retExp,nodeMap
打印功能現在將變爲:
expression = " (Root(SQ (VBZ) (NP (DT) (NN)) (VP (VB) (NP (NN)))))"
expression, nodeMap = parseExpression(expression)
tree = toTree(expression)
printTree(tree, tree[''][0], nodeMap)
輸出:
Root -> SQ
SQ -> VBZ NP VP
NP -> DT NN
VP -> VB NP
NP -> NN
def printTree(tree, node, nodeMap):
if node not in tree:
return
print '%s -> %s' % (nodeMap[node], ' '.join(nodeMap[child] for child in tree[node]))
for child in tree[node]:
printTree(tree, child, nodeMap)
輸出可以通過使用獲得這是可以做到用
您使用的表示是基於Lisp的S表達式s,這可以幫助你的谷歌搜索,如果沒有別的.. –
提示:您將要使用的主要數據結構是一個堆棧,最上面的項目是您要添加的孩子的節點。 –