Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

SOLVE THE FOLLOWING QUESTION IN HASKELL WITHOUT USING PRELUDE FUNCTIONS OR IMPORTING ANY LIBRARIES. You may make as many helper functions. According to Wikipedia, a

SOLVE THE FOLLOWING QUESTION IN HASKELL WITHOUT USING PRELUDE FUNCTIONS OR IMPORTING ANY LIBRARIES. You may make as many helper functions.

According to Wikipedia, a complete binary tree is a binary tree "where every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible."

The Wikipedia page referenced above also mentions that "Binary trees can also be stored in breadth-first order as an implicit data structure in lists, and if the tree is a complete binary tree, this method wastes no space."

Your task is to write a function that takes a list of integers and, assuming that the list is ordered according to an in-order traversal of a complete binary tree, returns a list that contains the values of the tree in breadth-first order.

Example 1: Let the input list be [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. This list contains the values of the following complete binary tree.

image text in transcribed

In this example, the input list happens to be sorted, but that is not a requirement.

Output 1: The output of the function shall be an list containing the values of the nodes of the binary tree read top-to-bottom, left-to-right. In this example, the returned list should be:

[7, 4, 9, 2, 6, 8, 10, 1, 3, 5] IT SHOULD PASS THE FOLLOWING TEST CASES: 

image text in transcribed

describe "breadthFirstTraversal" $ do it "should return the correct order of the numbers in the matrix" $ do breadthFirstTraversal [10,7,4,2,5,3,8,9] 'shouldBe' [5,4,8,7,2,3,9,10] breadthFirstTraversal [1,2,3,4,5,6,7,8,9,10] 'shouldBe [7,4,9,2,6,8,10,1,3,5] breadthFirstTraversal [1,2,2,6,7,5] 'shouldBe' [6,2,5,1,2,7]

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Securing SQL Server Protecting Your Database From Attackers

Authors: Denny Cherry

2nd Edition

1597499471, 978-1597499477

More Books

Students also viewed these Databases questions

Question

How flying airoplane?

Answered: 1 week ago