Question

In: Computer Science

C++ How can I print out the subtrees in my self-balancing tree? What I would like...

C++ How can I print out the subtrees in my self-balancing tree? What I would like to do is have it display something like " ROOT = X, LEFT PARENT = X, X,X, RIGHT PARENT = X,X,X.

Here is my code:

#include <iostream>
using namespace std;


class TreeNode
{
public:
   int data;
   TreeNode* left;
   TreeNode* right;
};

TreeNode* newNode(int data);

/* A function that constructs Balanced
Binary Search Tree from a sorted array */
TreeNode* convertToBTS(int arr[],
   int start, int end)
{
  
   if (start > end)
       return NULL;

   //make the middle number the root
   int mid = (start + end) / 2;
   TreeNode *root = newNode(arr[mid]);

  
   root->left = convertToBTS(arr, start,
       mid - 1);

  
   root->right = convertToBTS(arr, mid + 1, end);

   return root;
}

/* Helper function that allocates a new node
with the given data and NULL left and right
pointers. */
TreeNode* newNode(int data)
{
   TreeNode* node = new TreeNode();
   node->data = data;
   node->left = NULL;
   node->right = NULL;

   return node;
}

//The array will be sorted in PreOrder
void preOrder(TreeNode* node)
{
   if (node == NULL)
       return;
   cout << node->data << " ";
   preOrder(node->left);
   preOrder(node->right);
}

int main()
{
  
   const int SIZE = 7;
   int arr[SIZE];
   int count = 7, num = 0;

   cout << "We will construct a Self-Balancing Tree!\n Enter 6 numbers for the nodes." << endl;

   for (int i = 0; i < SIZE; i++)
   {
       cout << "Enter " << count << " more items!" << endl;      
       cin >> arr[i];
       count--;
   }

   int n = sizeof(arr) / sizeof(arr[0]);

   /* Convert List to BST */
   TreeNode *root = convertToBTS(arr, 0, n - 1);
   cout << "The tree will be displayed in PreOrder!" << endl
       << "First number will be the ROOT. Then LEFT SUBTREE, and RIGHT SUBTREE." << endl;
   preOrder(root);

   return 0;
}

Solutions

Expert Solution

Store the preorder traversal in an array called result and print that in the main function.

#include <iostream>

using namespace std;


class TreeNode

{

public:

int data;

TreeNode* left;

TreeNode* right;

};

TreeNode* newNode(int data);

/* A function that constructs Balanced

Binary Search Tree from a sorted array */

TreeNode* convertToBTS(int arr[],

int start, int end)

{

if (start > end)

return NULL;

//make the middle number the root

int mid = (start + end) / 2;

TreeNode *root = newNode(arr[mid]);

root->left = convertToBTS(arr, start,

mid - 1);

root->right = convertToBTS(arr, mid + 1, end);

return root;

}

/* Helper function that allocates a new node

with the given data and NULL left and right

pointers. */

TreeNode* newNode(int data)

{

TreeNode* node = new TreeNode();

node->data = data;

node->left = NULL;

node->right = NULL;

return node;

}

//The array will be sorted in PreOrder

int result[7];

int index = 0;

void preOrder(TreeNode* node) {

  if (node == NULL)

    return;

result[index++] = node->data;

  preOrder(node->left);

  preOrder(node->right);

}

int main()

{

const int SIZE = 7;

int arr[SIZE];

int count = 7, num = 0;

cout << "We will construct a Self-Balancing Tree!\n Enter 6 numbers for the nodes." << endl;

for (int i = 0; i < SIZE; i++)

{

cout << "Enter " << count << " more items!" << endl;

cin >> arr[i];

count--;

}

int n = sizeof(arr) / sizeof(arr[0]);

/* Convert List to BST */

TreeNode *root = convertToBTS(arr, 0, n - 1);

cout << "The tree will be displayed in PreOrder!" << endl

<< "First number will be the ROOT. Then LEFT SUBTREE, and RIGHT SUBTREE." << endl;

preOrder(root);

cout<<endl;

cout <<"root " << root->data <<endl;

cout <<"left parent " << result[1]<<","<<result[2]<<","<<result[3] <<endl;

cout <<"right parent " << result[4]<<","<<result[5]<<","<<result[6] <<endl;

return 0;

}


Related Solutions

I actually would like you to take a look at my code and point it out...
I actually would like you to take a look at my code and point it out if there is anything wrong with it. I think there might be because there are some values that are the same in different arrays. QUESTION: C++ Use the random number generator in class Random to store a list of 1000 random integer values in an array. Create 3 arrays using this method. Apply each of the insertion, bubble, selection and shell sort algorithms to...
If i can have the chart filled out with work for my understanding. I would greatly...
If i can have the chart filled out with work for my understanding. I would greatly appreciate it. An agent for a residential real estate company in a large city would like to be able to predict the monthly rental cost for apartments, based on the size of an apartment, as defined by square footage. The agent selects a sample of 25 apartments in a particular residential neighborhood and collects the data below. Apartment         Monthly Rent ($)       Size (Sq. Feet)...
How can I write a simple MIPS program to print out the following elements of an...
How can I write a simple MIPS program to print out the following elements of an array of the size of 10: 5,10,15,20,25,30,35,40,45,50
How would I work the following problems out? I would like someone to show their work...
How would I work the following problems out? I would like someone to show their work so I could better understand the answers and thought process. Genetics Problems For the first couple of problems, you will be working with guinea pigs. Their coat color shows an example of complete dominance - black (B) is dominant over brown (b). 1. A heterozygous black male is crossed with a heterozygous black female. What would be the resulting phenotypic ratio in the offspring?...
• What is a decision tree? • What is line balancing? • What is a group...
• What is a decision tree? • What is line balancing? • What is a group technology?
Balancing School and Life - My Quality of Life Self-Care Plan. The purpose of developing this...
Balancing School and Life - My Quality of Life Self-Care Plan. The purpose of developing this Plan is to set a framework and a plan to maintain wellness and to stay motivated and engaged throughout your Program. Doing this will help you achieve success during your coursework and as a professional nurse. The goal of the Project is to help you become self-aware and reflective as a means of identifying personal self-care strategies that will increase your energy and help...
I would like to test my hardware under vibration that can appear on a highway gantry....
I would like to test my hardware under vibration that can appear on a highway gantry. If someone has a model of such vibration. i.e period and amplitude. In my lab I have a motor that can be regulated according to its RPM.
Hello, How can I make the program print out "Invalid data!!" if the file has a...
Hello, How can I make the program print out "Invalid data!!" if the file has a grade that is not an A, B, C, D, E, or F or a percentage below zero. The program should sort a file containing data about students like this for five columns: one for last name, one for first name, one for student ID, one for student grade percentage, one for student grade. Smith Kelly 438975 98.6 A Johnson Gus 210498 72.4 C Reges...
Hi, I have to write an essay on what i believe the self is in my...
Hi, I have to write an essay on what i believe the self is in my psychology class, and this is my thesis statement , however I need help in the sentence structure. Thesis statement The self is a unique soul unreplicated that belongs to an individual. The manner in which one depicts themselves including ; how they present, commerce, along with experiences and beliefs/values are the elements to who an individual might be. Thank you
My question is little bit philosophical. I would like to explain my ideas with a 2...
My question is little bit philosophical. I would like to explain my ideas with a 2 dimensional universe model. If we had lived in 2 dimensional universe like a plane, What could we observe when seeing a 3d object? For example: If a square pyramid that is inside full of material comes to the plane universe in right angle, what could the people who live in 2d universe observe? Firstly, we could see small square and slowly it would enlarge...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT