Created
March 24, 2022 19:56
-
-
Save splch/0e0456d8abee1f855e81152b402acc5e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Node: | |
def __init__(self, info): | |
self.info = info | |
self.left = None | |
self.right = None | |
self.level = None | |
def __str__(self): | |
return str(self.info) | |
class BinarySearchTree: | |
def __init__(self): | |
self.root = None | |
def create(self, val): | |
if self.root == None: | |
self.root = Node(val) | |
else: | |
current = self.root | |
while True: | |
if val < current.info: | |
if current.left: | |
current = current.left | |
else: | |
current.left = Node(val) | |
break | |
elif val > current.info: | |
if current.right: | |
current = current.right | |
else: | |
current.right = Node(val) | |
break | |
else: | |
break | |
""" | |
Node is defined as | |
self.left (the left child of the node) | |
self.right (the right child of the node) | |
self.info (the value of the node) | |
""" | |
def levelOrder(root): | |
queue = [root] | |
s = '' | |
while queue: | |
t = queue.pop(0) | |
s += str(t.info) + ' ' | |
if t.left: | |
queue.append(t.left) | |
if t.right: | |
queue.append(t.right) | |
print(s[:-1]) | |
tree = BinarySearchTree() | |
t = int(input()) | |
arr = list(map(int, input().split())) | |
for i in range(t): | |
tree.create(arr[i]) | |
levelOrder(tree.root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment