defIsUnambiguous(student_Test_Code): # 根据所读code建立一棵树,在树叶子结点存code,下一个读取的code看看能不能正确插到叶子结点上。 new_Tree = Node() for code ,weight in student_Test_Code: for i in code: if i == 0: if new_Tree.left == None: new_Tree.left = Node(None) new_Tree = new_Tree.left else: new_Tree = new_Tree.left if i == 1: if new_Tree.right == None: new_Tree.right = Node(None) new_Tree = new_Tree.right else: new_Tree = new_Tree.right new_Tree.data = code
defpreordertraversal(BT,list_=[]): if BT: list_.append(BT.data) if BT.data != None : if BT.left != Noneor BT.right != None: #非叶子结点有存code。 list_.append( 'ambigurous') preordertraversal(BT.left,list_) preordertraversal(BT.right,list_) return list_ result = preordertraversal(new_Tree) if result == ['ambigurous']: return'Ambigurous' else: return'Unambigurous'
defcheck_Len(student_Test_Code): codetest = 0 for i inlen(H): for j in student_Test_Code.weight: codetest += (student_Test_Code[i] * j) return codetest == codeLen