Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PLEASE CODE IN C++ ------------------------------- As you know, when you insert values to a tree, the values greater will go to the right side and

PLEASE CODE IN C++

-------------------------------

image text in transcribed

image text in transcribed

As you know, when you insert values to a tree, the values greater will go to the right side and the values smaller go to the left side. The tree will be extremely skewed if you insert values in the order from the smallest to the largest or from the largest to the smallest. For example, if you insert 31 values 1, 2, 3,.. 31 in the listed order, the tree will look like 3 The tree becomes a list (with the depth of 30) and it is not optimized for the search Your task is to write a member function to optimize the tree to make the depth of the tree to be (Log.(n+1)-1 (n is the number of nodes). For example, the above tree will be optimized to: 12 20 10 26 15 23 25 27 31 You can see the depth of the tree becomes 4. The tree of this structure is the best for the search. Please keep it in mind: your optimizing algorithm should not be hardcoded and should be able to apply to all binary trees regardless the content of the trees. Instruction and Test: Use your assignment 8 as the base. Develop an algorithm and implement it in a public member function called Optimize) with void return. To test your code, insert following code into your main function: Tree optTree; As you know, when you insert values to a tree, the values greater will go to the right side and the values smaller go to the left side. The tree will be extremely skewed if you insert values in the order from the smallest to the largest or from the largest to the smallest. For example, if you insert 31 values 1, 2, 3,.. 31 in the listed order, the tree will look like 3 The tree becomes a list (with the depth of 30) and it is not optimized for the search Your task is to write a member function to optimize the tree to make the depth of the tree to be (Log.(n+1)-1 (n is the number of nodes). For example, the above tree will be optimized to: 12 20 10 26 15 23 25 27 31 You can see the depth of the tree becomes 4. The tree of this structure is the best for the search. Please keep it in mind: your optimizing algorithm should not be hardcoded and should be able to apply to all binary trees regardless the content of the trees. Instruction and Test: Use your assignment 8 as the base. Develop an algorithm and implement it in a public member function called Optimize) with void return. To test your code, insert following code into your main function: Tree optTree

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

Students also viewed these Databases questions

Question

Please show steps

Answered: 1 week ago