-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
163 lines (148 loc) · 4.29 KB
/
test.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from construct_tree import TreeInitialize
def preorder(self, root):
if root is None:
return ''
if root.lef:
self.preorder(root.left)
if root.right:
self.preorder(root.right)
def leaf(root,list):
if root == None:
return 0
elif root.left == None and root.right == None:
list.append(root.item_id)
return list
else:
leaf(root.left, list)
leaf(root.right, list)
return list
def map_generate(df):
#生成map 为了提高访问速度
r_value = {}
df = df.values
for i in df:
value = r_value.get(i[0])
if value == None:
r_value[i[0]] = [[i[1],i[2],i[3]]]
else:
r_value[i[0]].append([i[1], i[2], i[3]])
r_value[i[0]] = r_value[i[0]]
return r_value
def _node_list1(root):
# 将二叉树数据提出放入list
def node_val(node):
if (node.left or node.right):
return str(node.val)+'-'+ str(0)
else:
return str(node.item_id)+'-'+ str(1)
node_queue = [root]
arr_arr_node = []
arr_arr_node.extend([node_val(node_queue[0])])
while node_queue:
tmp = []
tmp_val = []
for i in node_queue:
if i is None:
tmp.append(None)
tmp.append(None)
tmp_val.append("")
tmp_val.append("")
else:
if i.left:
tmp.append(i.left)
tmp_val.append(node_val(i.left))
else:
tmp.append(None)
tmp_val.append("")
if i.right:
tmp.append(i.right)
tmp_val.append(node_val(i.right))
else:
tmp.append(None)
tmp_val.append("")
node_queue = tmp
is_break = True
for j in tmp:
if j != None:
is_break = False
if is_break:
break
else:
arr_arr_node.extend(tmp_val)
return arr_arr_node
def printTree(root):
if not root:
return
print('Binary Tree:')
printInOrder(root, 0, 'H', 17)
def printInOrder(root, height, preStr, length):
if not root:
return
printInOrder(root.right, height + 1, 'v', length)
string = preStr + str(root.val) + preStr
leftLen = (length - len(string)) // 2
rightLen = length - len(string) - leftLen
res = " " * leftLen + string + " " * rightLen
print(" " * height * length + res)
printInOrder(root.left, height + 1, '^', length)
def _node_list(root):
# 将二叉树数据提出放入list
def node_val(node):
if (node.left or node.right):
return (node.val, 0)
else:
return (node.item_id, 1)
node_queue = [root]
arr_arr_node = []
arr_arr_node.append([node_val(node_queue[0])])
while node_queue:
tmp = []
tmp_val = []
for i in node_queue:
if i is None:
tmp.append(None)
tmp.append(None)
tmp_val.append("")
tmp_val.append("")
else:
if i.left:
tmp.append(i.left)
tmp_val.append(node_val(i.left))
else:
tmp.append(None)
tmp_val.append("")
if i.right:
tmp.append(i.right)
tmp_val.append(node_val(i.right))
else:
tmp.append(None)
tmp_val.append("")
node_queue = tmp
is_break = True
for j in tmp:
if j != None:
is_break = False
if is_break:
break
else:
arr_arr_node.append(tmp_val)
return arr_arr_node
if __name__ == '__main__':
a = []
import pandas as pd
import numpy as np
data = pd.DataFrame({'item_ID':range(20),'category_ID':range(20)})
# data1= data.sample(frac=1).reset_index(drop=True)
# print(data1)
tree = TreeInitialize(data)
tree.random_binary_tree()
print(leaf(tree.root,a))
r = _node_list(tree.root)
r1 = _node_list1(tree.root)
print(r)
print(r1)
#
#
# import numpy as np
# a = np.array([[1,2,3],[4,4,5]])
# print(a.shape)