我必须创建并返回二进制搜索树的有序遍历的字符串表示。下面我有顺序遍历,但我无法弄清楚如何将其更改为字符串形式。我尝试使用+将它们连接在一起,但是它不能在终端上运行。如果需要,我也可以发布我的整个BST代码。拜托,谢谢你。
def in_order(self):
if self.__root is not None:
self._in(self.__root)
def _in(self, root):
if root is None:
return
if root is not None:
self._in(root.lchild)
print(str(root.value))
self._in(root.rchild)
答案 0 :(得分:0)
连接并返回:
def in_order(self):
if self.__root is not None:
return self._in(self.__root)
def _in(self, root):
if root is None:
return ''
if root is not None:
left = self._in(root.lchild)
right = self._in(root.rchild)
return left + ' ' + str(root.value) + ' ' + right
您必须打印调用obj.in_order()
的结果:print(obj.in_order())
。如果您希望每行一个,请在' '
中将'\n'
替换为Public Function vNum2(val As Object)
Dim result As Boolean = False
Try
'Dim allowedChars As String = "42.2.3"
Dim allowedChars As String = val.ToString()
'Bellow line will count how many dots are in string, if there one or none, result will be True
If allowedChars.Where(Function(dots) dots = ".").Count < 2 Then result = True
Catch ex As Exception
MsgBox("Error 1010xVNum2: " & ex.Message)
End Try
Return result
End Function
。