from queue import Queue from TreeNode import * from collections import * class BinaryTree: def __init__(self, nodes): self.root = self.createBinaryTree(nodes) def createBinaryTree(self, nodes): if len(nodes) == 0: return None # Create the root node of the binary tree root = TreeNode(nodes[0].data) # Create a queue and add the root node to it queue = Queue() queue.put(root) # Start iterating over the list of nodes starting from the second node i = 1 while i < len(nodes): # Get the next node from the queue curr = queue.get() # If the node is not None, create a new TreeNode object for its left child, # set it as the left child of the current node, and add it to the queue if nodes[i]: curr.left = TreeNode(nodes[i].data) queue.put(curr.left) i += 1 # If there are more nodes in the list and the next node is not None, # create a new TreeNode object for its right child, set it as the right child # of the current node, and add it to the queue if i < len(nodes) and nodes[i]: curr.right = TreeNode(nodes[i].data) queue.put(curr.right) i += 1 # Return the root of the binary tree return root def find(self, value): current_node = self.root while current_node: if current_node.data == value: return current_node elif value < current_node.data: current_node = current_node.left else: current_node = current_node.right return None