1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
| import numpy as np from numpy import array from pyope.ope import OPE, ValueRange
class decision_node: def __init__(self, value=None, col=None, rb=None, lb=None): self.value = value self.col = col self.rb = rb self.lb = lb
def LoadTxtMethod(filename): result = list() for line in open(filename): data = line.split(' ', 1) data[0] = float(data[0]) data[1] = float(data[1]) data_float = np.array(data) result.append(data_float) return array(result)
def median(x): n = len(x) x = list(x) x_order = sorted(x) return x_order[n // 2], x.index(x_order[n // 2])
def build_tree(x, j=0): rb = [] lb = [] m, n = x.shape if m == 0: return None edge, row = median(x[:, j].copy()) for i in range(m): if x[i][j] > edge: rb.append(i) if x[i][j] < edge: lb.append(i) rb_x = x[rb, :] lb_x = x[lb, :] rightBranch = build_tree(rb_x, (j + 1) % n) leftBranch = build_tree(lb_x, (j + 1) % n) return decision_node(x[row, :], j, rightBranch, leftBranch)
def travel_tree(node, aim): global pointlist if node is None: return col = node.col if aim[col] > node.value[col]: travel_tree(node.rb, aim) if aim[col] < node.value[col]: travel_tree(node.lb, aim) dis = dist(node.value, aim) if len(knears) < k: knears.setdefault(tuple(node.value.tolist()), dis) pointlist = sorted(knears.items(), key=lambda item: item[1], reverse=True) elif dis <= pointlist[0][1]: knears.setdefault(tuple(node.value.tolist()), dis) pointlist = sorted(knears.items(), key=lambda item: item[1], reverse=True) if node.rb is not None or node.lb is not None: if abs(aim[node.col] - node.value[node.col]) < pointlist[0][1]: if aim[node.col] < node.value[node.col]: travel_tree(node.rb, aim) if aim[node.col] > node.value[node.col]: travel_tree(node.lb, aim) return pointlist
def dist(x1, x2): return ((np.array(x1) - np.array(x2)) ** 2).sum() ** 0.5
cipher = OPE(b'long key' * 2, in_range=ValueRange(0, 10000000), out_range=ValueRange(0, 100000000))
def encryption(point): point[0] = float(point[0]) point[1] = float(point[1]) en_point = [] en_point.append(cipher.encrypt(int(point[0] * 1000000))) en_point.append(cipher.encrypt(int(point[1] * 1000000))) en_point_arr = np.array(en_point)
return en_point_arr
def decryption(point): point[0] = int(point[0]) point[1] = int(point[1]) de_point = [] de_point.append(cipher.decrypt(point[0]) / 1000000) de_point.append(cipher.decrypt(point[1]) / 1000000) de_point_arr = np.array(de_point)
return de_point_arr
if __name__ == "__main__": tmp = input('请输入目标点:') tmp = tmp.split(',') point_tmp = encryption(tmp) aim = [] for num in point_tmp: aim.append(num) knears = {} k = int(input('请输入需要查询的点的个数')) file = 'NE_encrypt.txt' data_c = LoadTxtMethod(file) tree = build_tree(data_c)
pointlist = travel_tree(tree, aim) i = 1 for point in pointlist[-k:]: x = [point[0][0], point[0][1]] de_point = decryption(x) print("****************--------第" + str(i) + "条检索数据--------****************") print("检索结果: ", de_point) print("距离为:", dist(tmp, de_point)) i = i + 1
|