MCPcopy Create free account
hub / github.com/subbarayudu-j/TheAlgorithms-Python / build_tree

Function build_tree

traversals/binary_tree_traversals.py:21–48  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

19
20
21def build_tree():
22 print("\n********Press N to stop entering at any point of time********\n")
23 print("Enter the value of the root node: ", end="")
24 check = raw_input().strip().lower()
25 if check == 'n':
26 return None
27 data = int(check)
28 q = queue.Queue()
29 tree_node = TreeNode(data)
30 q.put(tree_node)
31 while not q.empty():
32 node_found = q.get()
33 print("Enter the left node of %s: " % node_found.data, end="")
34 check = raw_input().strip().lower()
35 if check == 'n':
36 return tree_node
37 left_data = int(check)
38 left_node = TreeNode(left_data)
39 node_found.left = left_node
40 q.put(left_node)
41 print("Enter the right node of %s: " % node_found.data, end="")
42 check = raw_input().strip().lower()
43 if check == 'n':
44 return tree_node
45 right_data = int(check)
46 right_node = TreeNode(right_data)
47 node_found.right = right_node
48 q.put(right_node)
49
50
51def pre_order(node):

Callers 1

Calls 4

putMethod · 0.95
getMethod · 0.95
TreeNodeClass · 0.85
emptyMethod · 0.45

Tested by

no test coverage detected