Question: [PYTHON] Define the get_breadth_first_traversal(bs_tree) function which takes a binary search tree as a parameter and returns a list of the data values in the tree

[PYTHON] Define the get_breadth_first_traversal(bs_tree) function which takes a binary search tree as a parameter and returns a list of the data values in the tree in breadth-first order (i.e. as read from top to bottom, left to right). For example, consider the following binary tree:

[PYTHON] Define the get_breadth_first_traversal(bs_tree) function which takes a binary search tree as

The code below creates this tree, and then calls the get_breadth_first_traversal() function:

bs_tree = create_new_bst([55, 24, 8, 51, 25, 72, 78]) nodes = get_breadth_first_traversal(bs_tree) print('Breadth-first order:', nodes)

The output of the above code is:

Breadth-first order: [55, 24, 72, 8, 51, 78, 25]

HINT: To perform a breadth-first traversal, you should make use of a Queue (enqueue and dequeue the subtrees as you traverse). You can assume the Queue class is available (so you can call methods enqueue(), dequeue(), is_empty() and size()). You should not include this class in your answer.

NOTE: You can assume the BinarySearchTree class is available to you (so you can call methods get_data(), get_left() and get_right()). You should not include this class in your answer.

The header for the function is:

def get_breadth_first_traversal(bs_tree):

For example:

Test Result
t = create_new_bst([1, 2, 3, 4, 5]) print(get_breadth_first_traversal(t))
[1, 2, 3, 4, 5]
t = create_new_bst([3, 1, 5, 2, 4]) print(get_breadth_first_traversal(t))
[3, 1, 5, 2, 4] 

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!