# [Solved] How to build a binary tree based on morse code for a random set of letters?

Lucas Breno de Souza Noronha B Asks: How to build a binary tree based on morse code for a random set of letters?
I need to build a binary tree based on a random set of letters… That is, for example for the following combination of letters:

Code:
``````A .
C -
R .-
T ..
! -.``````

Must produce the following tree

Code:
``````       *
/
A     C
/    /
T   R !``````

That is, for each node of the tree the letter on the left must be inserted to represent the point “.” and right to represent a dash “−”

I wrote the following code but it’s incomplete… I don’t actually know how to implement the recursion

Code:
``````class BinaryTree:
def __init__(self, key):
self.esq = None
self.dir = None

def parse_tree(root, letter):
if alphabet[letter] == '.':
root.esq = BinaryTree(letter)
elif alphabet[letter] == '-':
root.dir = BinaryTree(letter)

N = int(input())
alphabet = {}

tree = BinaryTree('*')
for n in range(N):
letter, morse_code = input().split()
alphabet[letter] = morse_code
parse_tree(tree, letter)``````

For the following data sample

Code:
``````5
A .
C -
R .-
T ..
! -.``````

Ten-tools.com may not be responsible for the answers or solutions given to any question asked by the users. All Answers or responses are user generated answers and we do not have proof of its validity or correctness. Please vote for the answer that helped you in order to help others find out which is the most helpful answer. Questions labeled as solved may be solved or may not be solved depending on the type of question and the date posted for some posts may be scheduled to be deleted periodically. Do not hesitate to share your response here to help other visitors like you. Thank you, Ten-tools.