有关更多基本信息(适用于初学者)......您可能需要阅读以下问题: How to Implement a Binary Tree?
有关如何实现二叉树的更多信息...您可能想要打开此站点: http://www.openbookproject.net/thinkcs/python/english2e/ch21.html
二元操作树与其父亲...二叉树的方式不同。这是字符串'(7 + 3)(5-2)的表达式树:Expression Tree for (7+3) (5-2)。
下面的代码片段应该执行以下操作...
此代码存在一些问题需要解决......
或许您可以添加自己的代码来回答这个问题...如果您这样做,那么您可以忽略下面的代码。请注意,它应该打印节点中缀,前缀和后缀......
class Node:
def __init__ (self, data, currNode):
self.data = data
self.left = None
self.right = None
self.parent = currNode
currNode = self.right
return currNode
class BinaryOperationTree():
def __init__(self):
strng = raw_input('Please enter the operation to turn it into a binary tree.\n')
print strng
def readCharacter(self):
for i in range(-1, str_len, -1):
k = k + 1
if (k >= 2):
j = j * 16
l = 0 #Counter
currNode = Node
while (l <= k - 1):
if isDigit(hexa[l]):
encntrDigit(hexa[l], currNode)
elif isRPar(hexa[l]):
enctrRpar(currNode)
elif isLPar(hexa[l]):
enctrLpar(currNode)
elif isOperator(hexa[l]):
enctrOperator(hexa[1], currNode)
def isDigit(x):
chars = ['0','1','2','3','4','5','6','7','8','9']
if chars in x:
return True
else:
return False
def isRPar(x):
if ')' in x:
return True
else:
return False
def isLPar(x):
if '(' in x:
return True
else:
return False
def isOperator(x):
chars = ['+','-','*','/']
if chars in x:
return True
else:
return False
def encntrDigit(x, currNode):
currNode.data = x
currNode = currNode.parent
return currNode
def enctrRpar(self, currNode):
currNode = currNode.parent
def enctrLPar(self, currNode):
currNode = Node()
def enctrOperator(self, x, currNode):
currNode.data = x
currNode = currNode.parent
#Prints the tree in Pre-Order Format
def preOrder (node):
if (node is not None):
print node.data, "",
preOrder(node.left)
preOrder(node.right)
#Prints the tree in Order Format
def inOrder (node):
if (node is not None):
inOrder (node.left)
print node.data, "",
inOrder (node.right)
#Prints the tree in Post-Order Format
def postOrder (node):
if (node is not None):
postOrder (node.left)
postOrder (node.right)
print node.data, "",
def main():
string = BinaryOperationTree()
string.readCharacter()
main()