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 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
|
import math
def print_tree(array,unit_width=2): length = len(array) depth = math.ceil(math.log2(length + 1))
index = 0 width = 2 ** depth - 1 for i in range(depth): for j in range(2 ** i): print('{:^{}}'.format(array[index],width * unit_width),end=' ' * unit_width)
index += 1 if index >= length: break width = width // 2 print()
print_tree([x + 1 for x in range(29)])
======================================================================================= import math
def print_tree(array): ''' 前空格 元素间隔 1 7 0(这里可以不当特例解决,用15) 2 3 7 3 1 3 4 0 1 第一行中间间隔0个,第二行7个,第三行3个,第四行1个。用总的层次加1,减去层级的数字,再平方,最后减1,就是前空格的数字。如(4 + 1 - 1) ** 2 - 1 = 7,这就是第一层的前空格数 ''' index = 1 depth = math.ceil(math.log2(len(array)))
sep = ' ' for i in range(depth): offset = 2 ** i print(sep * (2 ** (depth - i - 1) - 1),end='')
line = array[index:index + offset]
for j,x in enumerate(line): print("{:>{}}".format(x,len(sep)),end='')
interval = 0 if i == 0 else 2 ** (depth - i) - 1
if j < len(line) - 1:
print(sep * interval,end='')
index += offset
print() print_tree([0,30,20,80,40,50,10,60,70,90,22]) print_tree([0,30,20,80,40,50,10,60,70,90,22,33,44,55,66,77]) print_tree([0,30,20,80,40,50,10,60,70,90,22,33,44,55,66,77,88,99,11]) =======================================================================================
origin = [0,30,20,80,40,50,10,60,70,90]
total = len(origin) - 1 print(origin) print_tree(origin)
def heap_adjust(n,i,array:list): ''' 调整当前结点(核心算法) 调整的结点的起点在n//2,可以保证所有调整的结点都有孩子结点 :param n: 待比较数个数,从max_heap传入的total值,不会变化。n就是边界,n就是节点的数量,就是上面 total的值 :param i: 当前结点的下标,i是当前父节点的索引号,i的值是从max_heap函数传入的变化的值 :param array: 待排序数据 :return: None,是否return无所谓,因为要调整的是从外面传进来的,所以可以不return或return array ''' while 2 * i <= n: lchild_index = 2 * i max_child_index = lchile_index if n > lchild_index and array[lchild_index + 1] > array[lchild_index]:
max_child_index = lchild_index + 1 if array[max_child_index] > array[i]: array[i],array[max_child_index] = array[max_child_index],array[i] i = max_child_index
else: break heap_adjust(total,total//2, origin) print(origin) print_tree(origin)
def max_heap(total,array:list): for i in range(total//2,0,-1):
heap_adjust(total,i,array)
return array
print_tree(max_heap(total,origin))
def sort(total,array:list):
while total > 1:
array[1],array[total] = array[total],array[1] total -= 1 heap_adjust(total,1,array) return array
print_tree(sort(total,origin))
def sort(total,array:list): while total > 1: array[1],array[total] = array[total],array[1] total -= 1 if total == 2 and array[total] >= array[total-1]: break
heap_adjust(total,1,array) return array
print_tree(sort(total,origin))
import math
def print_tree(array): ''' 前空格 元素间 1 7 0 2 3 7 3 1 3 4 0 1 ''' index = 1 depth = math.ceil(math.log2(len(array))) sep = ' ' for i in range(depth): offset = 2 ** i print(sep * (2 ** (depth - i -1) - 1),end='') line = array[index:index + offset] for j,x in enumerate(line): print("{:{}}".format(x,len(sep)),end='') interval = 0 if i == 0 else 2 ** (depth - i) - 1 if j < len(line) - 1: print(sep * interval,end='') index += offset print()
origin = [0,50,10,90,30,70,40,80,60,20]
total = len(origin) - 1 print(origin) print_tree(origin) print("="*50)
def heap_adjust(n,i,array:list): ''' 调整当前结点(核心算法) 调整的结点的起点在n//2,保证所有调整的结点都有孩子结点 :param n:待比较数个数 :param i: 当前结点的下标 :param array: 待排序数据 :return: None ''' while 2 * i <= n: lchile_index = 2 * i max_child_index = lchile_index if n > lchile_index and array[lchile_index + 1] > array[lchile_index]: max_child_index = lchile_index + 1 if array[max_child_index] > array[i]: array[i],array[max_child_index] = array[max_child_index],array[i] i = max_child_index else: break
def max_heap(total,array:list): for i in range(total//2,0,-1): heap_adjust(total,i,array) return array
print_tree(max_heap(total,origin)) print("="*50)
def sort(total,array:list): while total > 1: print(origin) print_tree(origin) array[1],array[total] = array[total],array[1] print(origin) print_tree(origin) print('='*80) total -= 1 if total == 2 and array[total] >= array[total-1]: break heap_adjust(total,1,array) return array
print_tree(sort(total,origin)) print(origin)
|