# 计算树高
def get_height(self):
layers = [self]
height = 0
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)
KD-Tree的原理及其在KNN中的应用(附Python代码)
最新推荐文章于 2024-09-14 19:55:48 发布
本文介绍了如何使用递归计算二叉搜索树的高度,展示了二叉树的可视化过程,并详细讲解了KD-Tree的数据结构、构建方法以及在最近邻搜索中的应用,强调了其在多维数据处理中的高效性能。
摘要由CSDN通过智能技术生成