Question

Define a function called breadth_first0 which takes a binary search tree as input, and which returns a list of the data valueHINT: To perform a breadth-first traversal, you should make use of a Queue. You will need to include your Queue class as part

Previous code:

class BinarySearchTree:

def __init__(self, data):
self.data = data
self.left = None
self.right = None

def search(self, find_data):
if self.data == find_data:
return self
elif find_data < self.data and self.left != None:
return self.left.search(find_data)
elif find_data > self.data and self.right != None:
return self.right.search(find_data)
else:
return None
  
def get_left(self):
return self.left

def get_right(self):
return self.right

def set_left(self, tree):
self.left = tree

def set_right(self, tree):
self.right = tree

def set_data(self, data):
self.data = data

def get_data(self):
return self.data
def traverse(root,order):
if order=="in":
return Inorder(root)
elif order=="pre":
return Preorder(root)
elif order=="post":
return Postorder(root)
else:
return ("Not a correct input")

def insert(root, data):
if not root:
return BinarySearchTree(data)
elif root.data == data:
root.count += 1
elif data < root.data:
root.left = insert(root.left, data)
else:
root.right = insert(root.right, data)

return root

def create_new_bst(seq):
root = None
for number in seq:
root = insert(root, number)
return root
def findSum(root):
if root is None:
return 0
else:
return root.data+findSum(root.left)+findSum(root.right)

def sum_beneath(root,data):
node=root.search(data)
if node is None:
return 0
total=0

total+=findSum(node.left)
total+=findSum(node.right)
return total

def Inorder(root):
path=[]
if root:
path.extend(Inorder(root.left))
path.append(root.data),
path.extend(Inorder(root.right))
return path
def Postorder(root):
path=[]
if root:
path.extend(Postorder(root.left) )
path.extend(Postorder(root.right) )
path.append(root.data)
return path
def Preorder(root):
path=[]
if root:
path.append(root.data)
path.extend(Preorder(root.left))
path.extend(Preorder(root.right))
return path
def traverse(root,order):
if order=="in":
return Inorder(root)
elif order=="pre":
return Preorder(root)
elif order=="post":
return Postorder(root)
else:
return ("Not a correct input")

Define a function called breadth_first0 which takes a binary search tree as input, and which returns a list of the data values in the tree in breadth-first order (i.e. as read For example, consider the following binary tree: from top to bottom, and left to right). 72 24 78 8 51 25 The code below creates this tree, and then calls the breadth_first0 function: t -create_new_bst(L55, 24, 8, 51, 25, 72, 78]) nodes - breadth-first(t) printC'Breadth-first order: ', nodes) This should produce the output: Breadth-first order: [55, 24, 72, 8, 51, 78, 25]
HINT: To perform a breadth-first traversal, you should make use of a Queue. You will need to include your Queue class as part of your answer Define your breadth first0 function (and Queue class) below. functions like get_data0, get_left0 and get_right0) For example: Test Result t create_new_bst([1, 2, 3, 4, 5]) [1, 2, 3, 4, 5] print(breadthfirst(t)) t - create_new_bst([3, 1, 5, 2, 4]) [3, 1, 5, 2, 4] print(breadthfirst(t))
0 0
Add a comment Improve this question Transcribed image text
Answer #1

def breadth_first(root):

    if root is None:

        return

    q = []

    ans = []

    q.append(root)

    while(len(q) > 0):

        ans.append(q[0].data)

        node = q.pop(0)

        if node.left is not None:

            q.append(node.left)

        if node.right is not None:

            q.append(node.right)

    return ans



===================================
SEE OUTPUT

main.py saved https://SlightRigidCategories 64 Python 3.6.1 (default, [GCC 4.8.2] on linux 65 E def breadth_first(root): 66曰
Thanks, PLEASE COMMENT if there is any concern.

Add a comment
Know the answer?
Add Answer to:
Previous code: class BinarySearchTree: def __init__(self, data): self.data = data self.left = None self.right = None de...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT