默克尔树的python简单实现

更新时间:2023-06-30 14:34:27 阅读: 评论:0

默克尔树的python简单实现
# coding=utf-8
import math
import random
import hashlib
class Node:
def __init__(lf,item):
lf.item = item
lf.child1 = None
万圣节吃什么lf.child2 = None
lf.hash = None
lf.data = None
def change_data(lf,data):
lf.data=data
lf.hash = hashlib.de('utf-8')).hexdigest()[8:-8] #hexdigest()返回摘要,作为⼗六进制数据字符串值 [8:-8]将32位MD5转换为16进制        print("make_hash",lf.hash,"size",len(lf.hash),end=" ")
return lf.hash
:
def __init__(lf):
< = None
美丽鲜花图片class Tree
def add(lf, item):#逐层添加⼦节点
node = Node(item)
end_str = make_data(item)
print("add node ",item)
if end_str !=None and end_str != -1:
node.change_data(end_str)
print("data= %s " % (node.data))
唐宋八大家是谁print("hash= %s " % (node.hash))
is None:
< = node
el:
q = [lf.root]
while True:
pop_node = q.pop(0)
if pop_node.child1 is None:
仪狄
pop_node.child1 = node
return
elif pop_node.child2 is None:
pop_node.child2 = node
return
el:
q.append(pop_node.child1)
q.append(pop_node.child2)
def reload_hash(lf):  # 层次遍历更新hash
is None:
return None
q = [lf.root]
item = [(lf.root.hash)]
while q != []:
pop_node = q.pop(0)
if pop_node.child1 and pop_node.child2 :
if pop_node.child1.hash and pop_node.child2.hash:
print("更新基础节点",pop_node.item,end=" ")
end_str=pop_node.child1.hash+pop_node.child2.hash
end_hash=pop_node.change_data(end_str)
print("end_hash= %s " % (end_hash))
if pop_node.child1 is not None:
q.append(pop_node.child1)
生日贺词祝福语if pop_node.child2 is not None:
q.append(pop_node.child2)
return
def traver(lf):  # 层次遍历
is None:
return None
q = [lf.root]
item = [(lf.root.hash)]
while q != []:
pop_node = q.pop(0)
if pop_node.child1 is not None:
q.append(pop_node.child1)
item.append((pop_node.child1.item,pop_node.child1.data,pop_node.child1.hash))
if pop_node.child2 is not None:
q.append(pop_node.child2)
item.append((pop_node.child2.item,pop_node.child2.data,pop_node.child2.hash))
return (item)
def make_data(i):
if i>6 and i<15:
raw_str = "xizzhongcai153********"节食英语
nums = math.floor(1e5 * random.random())        nums = str(nums)
nums = nums.zfill(5)
end_str = raw_str + nums
开平通宝
print("end_str",end_str)
return end_str托福考点
elif i>=0 and i<7:
return None
el:
return -1
# t = Tree()
# for i in range(15):
#    t.add(i)
if __name__ == '__main__':
t = Tree()
for i in range(7):  # 基础节点
t.add(i)
for i in range(7, 15):  # 带数据的节点
t.add(i)
for i in range(4,0,-1): #刷新基础节点hash值
print("第",i,'层节点hash刷新:')
load_hash())
print('前序遍历:\n节点名:数据:hash值',t.traver()) #作业上传混分

本文发布于:2023-06-30 14:34:27,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/89/1061682.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:节点   数据   进制   作为   基础   鲜花   混分   美丽
相关文章
留言与评论(共有 0 条评论)
   
验证码:
推荐文章
排行榜
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图