preorder traversal of that tree. For example, if the given traversal is (10, 5, 1, 7, 40, 50}, then the output should be following tree. 10 40 1 7 50 Hints: • Write a recursive function to do the followings: a) Get the first element of the array and make it root of the BST. b) Find the index i such that the value in i+1 is the first value bigger than the root. c) Recursively call a function to do the same for left subarray (1.. i) and right subarray (i+1..size-1). i) Get the first element of left half and make it left child of the root created in step a. ii) Get the first element of right half and make it right child of the root created in step a. d) return root.

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 6PE
icon
Related questions
Question
D. Write a program named as ProblemD_<your-student-id>.cpp that constructs a BST from a given
preorder traversal of that tree.
For example, if the given traversal is (10, 5, 1, 7, 40, 50), then the output should be following tree.
10
40
1 7 50
Hints:
• Write a recursive function to do the followings:
a) Get the first element of the array and make it root of the BST.
b) Find the index i such that the value in i+1 is the first value bigger than the root.
c) Recursively call a function to do the same for left subarray (1.. i) and right
subarray (i+1.size-1).
i) Get the first element of left half and make it left child of the root created in step a.
ii) Get the first element of right half and make it right child of the root created in step a.
d) return root.
Transcribed Image Text:D. Write a program named as ProblemD_<your-student-id>.cpp that constructs a BST from a given preorder traversal of that tree. For example, if the given traversal is (10, 5, 1, 7, 40, 50), then the output should be following tree. 10 40 1 7 50 Hints: • Write a recursive function to do the followings: a) Get the first element of the array and make it root of the BST. b) Find the index i such that the value in i+1 is the first value bigger than the root. c) Recursively call a function to do the same for left subarray (1.. i) and right subarray (i+1.size-1). i) Get the first element of left half and make it left child of the root created in step a. ii) Get the first element of right half and make it right child of the root created in step a. d) return root.
Expert Solution
steps

Step by step

Solved in 2 steps with 1 images

Blurred answer
Knowledge Booster
Quicksort
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning