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:
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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started