diff --git a/binarytree2.py b/binarytree2.py new file mode 100644 index 0000000000000000000000000000000000000000..34536971664eff8a563ad226788efd7764386bc5 --- /dev/null +++ b/binarytree2.py @@ -0,0 +1,195 @@ +import numpy as np +import sys +class MorseNode: + def __init__(self, c,morseCode=''): + + self.char = c + self.morseCode=morseCode + self.dot = None + self.dash = None + #← + def insertLeft(self,c): + + self.dot = MorseNode(c) + return self.dot + #→ + def insertRight(self,c): + + self.dash = MorseNode(c) + return self.dash + +root = MorseNode('*') +#1st +root.dash = MorseNode('T') +root.dot = MorseNode('E') +#2nd +root.dot.dot = MorseNode('I') +root.dot.dash = MorseNode('A') + +root.dash.dot = MorseNode('N') +root.dash.dash = MorseNode('M') +#3rd +root.dot.dot.dot = MorseNode('S') +root.dot.dot.dash = MorseNode('U') +root.dot.dash.dot = MorseNode('R') +root.dot.dash.dash = MorseNode('W') + +root.dash.dot.dot = MorseNode('D') +root.dash.dot.dash = MorseNode('K') +root.dash.dash.dot = MorseNode('G') +root.dash.dash.dash = MorseNode('O') +#4th +root.dot.dot.dot.dot = MorseNode('H') +root.dot.dot.dot.dash = MorseNode('V') +root.dot.dot.dash.dot = MorseNode('F') +root.dot.dot.dash.dash = MorseNode('') +root.dot.dash.dot.dot = MorseNode('L') +root.dot.dash.dot.dash = MorseNode('') +root.dot.dash.dash.dot = MorseNode('P') +root.dot.dash.dash.dash = MorseNode('J') + +root.dash.dot.dot.dot = MorseNode("B") +root.dash.dot.dot.dash = MorseNode("X") +root.dash.dot.dash.dot = MorseNode("C") +root.dash.dot.dash.dash = MorseNode("Y") +root.dash.dash.dot.dot = MorseNode("Z") +root.dash.dash.dot.dash = MorseNode("Q") +root.dash.dash.dash.dot = MorseNode("") +root.dash.dash.dash.dash = MorseNode("") + +#5 +root.dot.dot.dot.dot.dot = MorseNode("5") +root.dot.dot.dot.dot.dash = MorseNode("4") +root.dot.dot.dot.dash.dot = MorseNode("") +root.dot.dot.dot.dash.dash = MorseNode("3") +root.dot.dot.dash.dot.dot = MorseNode("") +root.dot.dot.dash.dot.dash = MorseNode("¿") +root.dot.dot.dash.dash.dot = MorseNode("?") +root.dot.dot.dash.dash.dash = MorseNode("2") + +root.dot.dash.dot.dot.dot = MorseNode("&") +root.dot.dash.dot.dot.dash = MorseNode("") +root.dot.dash.dot.dash.dot = MorseNode("+") +root.dot.dash.dot.dash.dash = MorseNode("") +root.dot.dash.dash.dot.dot = MorseNode("+") +root.dot.dash.dash.dot.dash = MorseNode("") +root.dot.dash.dash.dash.dot = MorseNode("") +root.dot.dash.dash.dash.dash = MorseNode("1") + +root.dash.dot.dot.dot.dot = MorseNode("6") +root.dash.dot.dot.dot.dash = MorseNode("=") +root.dash.dot.dot.dash.dot = MorseNode("/") +root.dash.dot.dot.dash.dash = MorseNode("") +root.dash.dot.dash.dot.dot = MorseNode("") +root.dash.dot.dash.dot.dash = MorseNode("") +root.dash.dot.dash.dash.dot = MorseNode("(") +root.dash.dot.dash.dash.dash= MorseNode("") + +root.dash.dash.dot.dot.dot = MorseNode("7") +root.dash.dash.dot.dot.dash = MorseNode("") +root.dash.dash.dot.dash.dot = MorseNode("") +root.dash.dash.dot.dash.dash = MorseNode("") +root.dash.dash.dash.dot.dot = MorseNode("8") +root.dash.dash.dash.dot.dash = MorseNode("") +root.dash.dash.dash.dash.dot = MorseNode("9") +root.dash.dash.dash.dash.dash = MorseNode("0") +#6 +root.dot.dash.dot.dash.dot.dash = MorseNode(".") +root.dash.dash.dot.dot.dash.dash = MorseNode(",") +root.dash.dot.dash.dash.dot.dash = MorseNode(")") +root.dash.dot.dot.dot.dot.dash = MorseNode("-") +root.dash.dash.dot.dot.dot.dash = MorseNode("i") +root.dot.dot.dash.dash.dot.dash = MorseNode("_") +root.dot.dash.dash.dash.dash.dot = MorseNode("'") +root.dash.dash.dash.dot.dot.dot = MorseNode(":") +root.dot.dash.dot.dot.dash.dot = MorseNode("”") +root.dash.dot.dash.dot.dash.dash = MorseNode("!") +root.dash.dot.dash.dot.dash.dot = MorseNode(";") +root.dot.dot.dot.dash.dot.dot = MorseNode("\n") +#7 +root.dot.dot.dot.dash.dot.dot.dash = MorseNode("$") + +class BinaryTree: + def __init__(self): + # 初始化结点,因为左右结点都可能为空,所以默认设置左右结点的值为空,如果没有传入左右结点就是空,有的话则是传入的结点 + self.root = root + + def encode(self,letter): + node= self.findMorseCode(self.root,'',letter) + return node.morseCode + + def findMorseCode(self,node,morsecode,letter): + if node==None: + return None + node.morseCode=morsecode + if node.char==letter: + return node + next=self.findMorseCode(node.dot,morsecode+'.',letter) + if next!=None: + return next + else: + return self.findMorseCode(node.dash,morsecode+'-',letter) + + def decode(self,morseCode): + letter = self.root + for c in morseCode: + if c == ' ': + return letter.char + if c == '.': + letter = letter.dot + elif c == '-': + letter = letter.dash + return letter.char + +Tree=BinaryTree() + + +def encrypt(message): + cipher = '' + for letter in message: + if letter != ' ': + # 查字典并添加对应的摩斯密码 # 用空格分隔不同字符的摩斯密码 + cipher += Tree.encode(letter) + ' ' + else: + # 1个空格表示不同的字符 # 2表示不同的词 + cipher += ' ' + return cipher + +# 将字符串从摩斯解密为英文的函数 +def decrypt(message): + # 在末尾添加额外空间以访问最后一个摩斯密码 + message += ' ' + decipher = '' + citext = '' + i = 0 + for letter in message: + # 检查空间 + if letter != ' ': + # 计数器来跟踪空间 + i = 0 + # 在空格的情况下 + citext += letter + # 在空间的情况下 + else: + # 如果 i = 1 表示一个新字符 + i += 1 + # 如果 i = 2 表示一个新词 + if i == 2: + # 添加空格来分隔单词 + decipher += ' ' + else: + # 使用它们的值访问密钥(加密的反向) + decipher += Tree.decode(citext) + citext = '' + return decipher + +result=encrypt('US') +print(result) +result=decrypt(result) +print(result) +x=sys.stdin.readline(100) +print(x) +result=encrypt(x) +print(result) +result=decrypt(result) +print(result)