[問題] 關於list用於tree的表示

看板Python作者 (耐綸)時間6年前 (2017/10/01 16:02), 編輯推噓0(002)
留言2則, 2人參與, 6年前最新討論串1/1
最近在學python順便重看algo的書籍 目前看到第六章這個部份 http://interactivepython.org/runestone/static/pythonds/Trees/ListofListsRepresentation.html 我的疑問是 def insertLeft(root,newBranch): t = root.pop(1) if len(t) > 1: root.insert(1,[newBranch,t,[]]) else: root.insert(1,[newBranch, [], []]) return root 他先確定了右邊節點是否為空 只是if len(t) > 1 時的作法是否應是 root.insert(1,[t,newBranch,[]]) 才是? 因為不為空,表示左子樹的根應該為t本身,而不是newBranch? 以上 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.167.126.20 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1506844935.A.D1D.html

10/01 16:42, 6年前 , 1F
他是 append 在 LTree 的 root
10/01 16:42, 1F

10/01 16:51, 6年前 , 2F
我後來看下一章的例子發現,是我搞錯語意
10/01 16:51, 2F
文章代碼(AID): #1PqA47qT (Python)