KD-Tree
仅以此文纪念过往的岁月
简介
KD-Tree 全称 K-Dimensional Tree ,KD-Tree本质是一种平衡二叉树,其一般应用于KNN分类。其关键是查询,对于KNN应用而言,一般是一次构造后,不再增加和删除节点。对某一节点计算其近邻的点。
KD-Tree构造
Python代码
from collections import namedtuple
from operator import itemgetter
from pprint import pformat
class Node:
def __init__(self,_sa,_loc,_lc,_rc,_p):
self.splitAttribute = _sa
self.location = _loc
self.left_child = _lc
self.right_child = _rc
self.parent = _p
return
def isLeft(self):
if self.parent.left_child == self:
return True
else:
return False
def isRight(self):
if self.parent.right_child == self:
return True
else:
return False
def isRoot(self):
if self.parent == None:
return True
else:
return False
def neghbor(self):
if self.isRight() =&