###前提・実現したいこと
python3でプログラミングしています。
逆ポーランド記法の式から、式の木(2分木)を作るプログラムを作成しています。
再帰の部分の処理がうまくいっていないようなのですが、よくわかりません。よろしくおねがいします。
(入力例)ab*cd+e/-
(希望する出力)-*ab/+cde
###発生している問題・エラーメッセージ
(誤った出力)-/ee/ee
###該当のソースコード
python 3.4.3
python
1# coding: utf-8 2# Here your code ! 3 4class tnode : 5 def __init__(self): 6 self.left = None 7 self.ope = "" 8 self.right = None 9 10def gentree(p, w): 11 p = tnode() 12 w_lst = list(w) 13 p.ope = w_lst.pop() 14 if p.ope == "-" or p.ope == "+" or p.ope == "*" or p.ope == "/" : 15 w = "".join(w_lst) 16 p.right = gentree(p.right, w) 17 p.left = gentree(p.left, w) 18 return p 19 20def prefix(p): 21 if p : 22 yield p.ope 23 yield from prefix(p.left) 24 yield from prefix(p.right) 25 26# main 27f = input() 28print(f) 29 30root = None 31root = gentree(root, f) 32 33for x in prefix(root): 34 print(x, end="")
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/10/14 12:08