class TreeNode:
def __init__(self,x):
self.value = x
self.right = None
self.left = None
def printTree(head):
print("Binary Tree:")
printInOrder(head,0,"H",17)
print()
def printInOrder(head,height,Str,length):
if head == None:
return
printInOrder(head.right,height + 1,"v",length)
""" 获取当前字符串长度"""
val = Str + head.value + Str
""" 根据指定字符串长度大小,计算左右长度进行填充"""
lenL = (length - len(val))/2
lenR = length - len(val) - lenL
"""填充完成的val"""
val = getSpace(lenL) + val + getSpace(lenR)
print(getSpace(height * length) + val)
printInOrder(head.left,height + 1, "^",length)
def getSpace(num):
space = " "
buf = ""
for i in range(num):
buf+=space
return buf