帮我修改这段代码 from collections import deque class BTNode: def __init__(self,d=None): self.data=d self.lchild=None self.rchild=None class BTree: def __init__(self,d=None): self.b=d def SetRoot(self,r): self.b=r def DispBTree(self): return self._DispBTreel(self.b) def _DispBTree1(self,t): if t==None: return "" else: bstr=str(t.data) if t.lchild!=None or t.rchild!=None: bstr+="(" bstr+=self._DispBTree(t.lchild) if t.rchild!=None: bstr+="," bstr+=self._DispBTree(t.rchild) bstr==")" return bstr def RePostOrder(bt): _RePostOrder(bt.b) def _RePostOrder(t): if t==None: return print(t.data+" ") _RePostOrder(t.lchild) _RePostOrder(t.rchild) b=BTNode('A') p1=BTNode('B') p2=BTNode('C') p3=BTNode('D') p4=BTNode('E') p5=BTNode('F') p6=BTNode('G') b.lchild=p1 b.rchild=p2 p1.lchild=p3 p1.rchild=p4 p2.lchild=p5 p2.rchild=p6 bt=BTree() bt.SetRoot(b) print("bt:",end=' ');print(bt.DispBTree()) print("求解结果:") RePostOrder(bt)
时间: 2023-07-16 22:12:42 浏览: 97
我的代码数据结构求修改
代码存在一些小问题,我给你修改一下:
```python
from collections import deque
class BTNode:
def __init__(self,d=None):
self.data=d
self.lchild=None
self.rchild=None
class BTree:
def __init__(self,d=None):
self.b=d
def SetRoot(self,r):
self.b=r
def DispBTree(self):
return self._DispBTree(self.b)
def _DispBTree(self,t):
if t==None:
return ""
else:
bstr=str(t.data)
if t.lchild!=None or t.rchild!=None:
bstr+="("
bstr+=self._DispBTree(t.lchild)
if t.rchild!=None:
bstr+=","
bstr+=self._DispBTree(t.rchild)
bstr+=")"
return bstr
def RePostOrder(bt):
_RePostOrder(bt.b)
def _RePostOrder(t):
if t==None:
return
_RePostOrder(t.lchild)
_RePostOrder(t.rchild)
print(t.data)
b=BTNode('A')
p1=BTNode('B')
p2=BTNode('C')
p3=BTNode('D')
p4=BTNode('E')
p5=BTNode('F')
p6=BTNode('G')
b.lchild=p1
b.rchild=p2
p1.lchild=p3
p1.rchild=p4
p2.lchild=p5
p2.rchild=p6
bt=BTree()
bt.SetRoot(b)
print("bt:",end=' ')
print(bt.DispBTree())
print("求解结果:")
RePostOrder(bt)
```
这段代码是二叉树后序遍历算法的实现。其中 `_RePostOrder` 是递归函数,用于对二叉树进行后序遍历;`RePostOrder` 函数则是对 `_RePostOrder` 的一个包装,用于外部调用。在 `main` 函数中,我们创建了一个二叉树,并调用 `RePostOrder` 函数对其进行后序遍历。
阅读全文