MCPcopy Index your code
hub / github.com/TheAlgorithms/Python / build_tree

Function build_tree

searches/binary_tree_traversal.py:17–39  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15
16
17def build_tree() -> TreeNode:
18 print("\n********Press N to stop entering at any point of time********\n")
19 check = input("Enter the value of the root node: ").strip().lower()
20 q: queue.Queue = queue.Queue()
21 tree_node = TreeNode(int(check))
22 q.put(tree_node)
23 while not q.empty():
24 node_found = q.get()
25 msg = f"Enter the left node of {node_found.data}: "
26 check = input(msg).strip().lower() or "n"
27 if check == "n":
28 return tree_node
29 left_node = TreeNode(int(check))
30 node_found.left = left_node
31 q.put(left_node)
32 msg = f"Enter the right node of {node_found.data}: "
33 check = input(msg).strip().lower() or "n"
34 if check == "n":
35 return tree_node
36 right_node = TreeNode(int(check))
37 node_found.right = right_node
38 q.put(right_node)
39 raise ValueError("Something went wrong")
40
41
42def pre_order(node: TreeNode) -> None:

Callers 1

Calls 4

putMethod · 0.95
getMethod · 0.95
TreeNodeClass · 0.70
emptyMethod · 0.45

Tested by

no test coverage detected