Using Python Insert each letter of this string as a key from left to right into
ID: 3568150 • Letter: U
Question
Using Python Insert each letter of this string as a key from left to right into a binary search tree:
FADCEB
and then do a in order traversal of the resulting tree and print each letter out with no spaces between in the order of the traversal, what will the output be?
Explanation / Answer
class Node: def __init__(self, val): self.l_child = None self.r_child = None self.data = val def binary_insert(root, node): if root is None: root = node else: if root.data > node.data: if root.l_child == None: root.l_child = node else: binary_insert(root.l_child, node) else: if root.r_child == None: root.r_child = node else: binary_insert(root.r_child, node) def order_print(root): if not root: return print root.data pre_order_print(root.l_child) pre_order_print(root.r_child) r = Node(6) binary_insert(F, Node(1)) binary_insert(A, Node(2)) binary_insert(D, Node(3)) binary_insert(C, Node(4)) binary_insert(E, Node(5)) binary_insert(B, Node(6)) print "in order:" order_print(r)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.