while layers:
height += 1
new_list = []
for node in layers:
if node.left:
new_list.append(node.left)
if node.right:
new_list.append(node.right)
layers = new_list
return height
# 二叉树可视化
def visualize(self, axis='off'):
'''
基本算法: 将树状结构映射到二维矩阵中,
如果节点左右下方有节点则把该节点加入到矩阵中的坐标中,
如节点(x,y)左下方有节点则把节点放入(x+offset,y+1)
offset为x坐标偏移量,
offset = 2**(len(matrix)-y-2)
'''
figure, axes = plt.subplots(figsize=(8, 6), dpi=80)
height = self.get_height()
width_ = 2 ** (height - 1)
width = 2 * width_ + 1
matrix = [[[] for x in range(width)] for y in range(height)]
matrix[0][width_] = bst_tree # put head in the middle position
for y in range(len(matrix)):
for x in range(len(matrix[y])):
node = matrix[y][x]
if node:
x1, y1 = (1 / width) * (x + 0.32), 1 - (1 / height) * y - 0.21
axes.text(x1, y1, str(node.value), color='white', fontsize=FONT_SIZE, fontweight='bold')
offset = 2 ** (len(matrix) - y - 2)
if node.left:
matrix[y + 1][x - offset] = node.left
x2, y2 = (1 / width) * (x - offset + 0.5), 1 - (1 / height) * (y + 1) - 0.2
line = mlines.Line2D([x1, x2], [y1, y2], zorder=-1)
axes.add_line(line)
if node.right:
matrix[y + 1][x + offset] = node.right
x2, y2 = (1 / width) * (x + offset + 0.5), 1 - (1 / height) * (y + 1) - 0.2
line = mlines.Line2D([x1, x2], [y1, y2], zorder=-1)
axes.add_line(line)
cc = plt.Circle(((1 / width) * (x + 0.5), 1 - (1 / height) * y - 0.2), 1 / width / 2 * NODE_SIZE_SCALE, color='blue')
axes.set_aspect(1)
axes.add_artist(cc)
plt.axis(axis)
plt.show()
使用递归的方式构建二叉搜索树
def make_binary_search_tree(data):
if not data:
return
data.sort()
mid = len(data) // 2
tree = Node(data[mid], # 中间节点数据
make_binary_search_tree(data[:mid]), # 左子树
make_binary_search_tree(data[mid+1:])) # 右子树
return tree
FONT_SIZE = 20
NODE_SIZE_SCALE = 1
X = [3, 6, 5, 2, 4, 1, 7]
if name == ‘main’:
bst = make_binary_search_tree(X)
bst.visualize()
运行结果:
![在这里插入图片描述](h