SlideShare a Scribd company logo
1 of 5
Download to read offline
I have C++ question that I do not know how to do, Can you teach me this question:
Draw a CLASS diagram for Binary Search Tree (BTS) :
can you explain me each step, thank you
Solution
//program to convert Binary Tree to Binary Search Tree
#include
#include
// A binary tree node structure
struct node
{
int data;
struct node *left;
struct node *right;
};
void storeInorder (struct node* node, int inorder[], int *index_ptr)
{
// Base Case
if (node == NULL)
return;
// first store the left subtree
storeInorder (node->left, inorder, index_ptr);
// Copy the root's data
inorder[*index_ptr] = node->data;
(*index_ptr)++; // increase index for next entry
// finally store the right subtree
storeInorder (node->right, inorder, index_ptr);
}
/* A helper function to count nodes in a Binary Tree */
int countNodes (struct node* root)
{
if (root == NULL)
return 0;
return countNodes (root->left) +
countNodes (root->right) + 1;
}
// Following function is needed for library function qsort()
int compare (const void * a, const void * b)
{
return ( *(int*)a - *(int*)b );
}
/* A helper function that copies contents of arr[] to Binary Tree.
This functon basically does Inorder traversal of Binary Tree and
one by one copy arr[] elements to Binary Tree nodes */
void arrayToBST (int *arr, struct node* root, int *index_ptr)
{
// Base Case
if (root == NULL)
return;
/* first update the left subtree */
arrayToBST (arr, root->left, index_ptr);
/* Now update root's data and increment index */
root->data = arr[*index_ptr];
(*index_ptr)++;
/* finally update the right subtree */
arrayToBST (arr, root->right, index_ptr);
}
// This function converts a given Binary Tree to BST
void binaryTreeToBST (struct node *root)
{
// base case: tree is empty
if(root == NULL)
return;
/* Count the number of nodes in Binary Tree so that
we know the size of temporary array to be created */
int n = countNodes (root);
// Create a temp array arr[] and store inorder traversal of tree in arr[]
int *arr = new int[n];
int i = 0;
storeInorder (root, arr, &i);
// Sort the array using library function for quick sort
qsort (arr, n, sizeof(arr[0]), compare);
// Copy array elements back to Binary Tree
i = 0;
arrayToBST (arr, root, &i);
// delete dynamically allocated memory to avoid meory leak
delete [] arr;
}
/* Utility function to create a new Binary Tree node */
struct node* newNode (int data)
{
struct node *temp = new struct node;
temp->data = data;
temp->left = NULL;
temp->right = NULL;
return temp;
}
/* Utility function to print inorder traversal of Binary Tree */
void printInorder (struct node* node)
{
if (node == NULL)
return;
/* first recur on left child */
printInorder (node->left);
/* then print the data of node */
printf("%d ", node->data);
/* now recur on right child */
printInorder (node->right);
}
/* Driver function to test above functions */
int main()
{
struct node *root = NULL;
/* Constructing tree given in the above figure
10
/ 
30 15
/ 
20 5 */
root = newNode(10);
root->left = newNode(30);
root->right = newNode(15);
root->left->left = newNode(20);
root->right->right = newNode(5);
// convert Binary Tree to BST
binaryTreeToBST (root);
printf("Following is Inorder Traversal of the converted BST:  ");
printInorder (root);
return 0;
}

More Related Content

Similar to I have C++ question that I do not know how to do, Can you teach me t.pdf

Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfezzi552
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfmohammadirfan136964
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfhadpadrrajeshh
 
Templated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdfTemplated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdfmanjan6
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfarchiesgallery
 
Write a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdfWrite a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdfinfo824691
 
To create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdfTo create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdfbhim1213
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfJUSTSTYLISH3B2MOHALI
 
Add these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfAdd these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfindiaartz
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfkostikjaylonshaewe47
 
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxJAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxolsenlinnea427
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfamarndsons
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdfmumnesh
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfshanki7
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfadityastores21
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxdebishakespeare
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfmichardsonkhaicarr37
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxnoreendchesterton753
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfpratikradia365
 

Similar to I have C++ question that I do not know how to do, Can you teach me t.pdf (20)

Please i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdfPlease i need help on following program using C++ Language.Add the.pdf
Please i need help on following program using C++ Language.Add the.pdf
 
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdfPart 1)#include stdio.h #include stdlib.h #include pthrea.pdf
Part 1)#include stdio.h #include stdlib.h #include pthrea.pdf
 
Given a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdfGiven a newly created Binary Search Tree with the following numerica.pdf
Given a newly created Binary Search Tree with the following numerica.pdf
 
Templated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdfTemplated Binary Tree implementing function help I need to im.pdf
Templated Binary Tree implementing function help I need to im.pdf
 
Need Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdfNeed Help with this Java Assignment. Program should be done in JAVA .pdf
Need Help with this Java Assignment. Program should be done in JAVA .pdf
 
Write a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdfWrite a function in C++ to generate an N-node random binary search t.pdf
Write a function in C++ to generate an N-node random binary search t.pdf
 
To create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdfTo create a node for a binary search tree (BTNode, ) and to create a .pdf
To create a node for a binary search tree (BTNode, ) and to create a .pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
Add these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdfAdd these three functions to the class binaryTreeType (provided).W.pdf
Add these three functions to the class binaryTreeType (provided).W.pdf
 
This is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdfThis is a c++ binary search program I worked so far but still cant g.pdf
This is a c++ binary search program I worked so far but still cant g.pdf
 
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docxJAVA - Design a data structure IntSet that can hold a set of integers-.docx
JAVA - Design a data structure IntSet that can hold a set of integers-.docx
 
DS Code (CWH).docx
DS Code (CWH).docxDS Code (CWH).docx
DS Code (CWH).docx
 
Please write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdfPlease write the C++ code that would display the exact same output a.pdf
Please write the C++ code that would display the exact same output a.pdf
 
4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf4. The size of instructions can be fixed or variable. What are advant.pdf
4. The size of instructions can be fixed or variable. What are advant.pdf
 
Please read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdfPlease read the comment ins codeExpressionTree.java-------------.pdf
Please read the comment ins codeExpressionTree.java-------------.pdf
 
MAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdfMAINCPP include ltiostreamgt include ltstringgt u.pdf
MAINCPP include ltiostreamgt include ltstringgt u.pdf
 
Required to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docxRequired to augment the authors Binary Search Tree (BST) code to .docx
Required to augment the authors Binary Search Tree (BST) code to .docx
 
in this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdfin this assignment you are asked to write a simple driver program an.pdf
in this assignment you are asked to write a simple driver program an.pdf
 
Write a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docxWrite a C++ function to delete the given value from the binary search.docx
Write a C++ function to delete the given value from the binary search.docx
 
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdfmain.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
main.cpp#include TreeNode.h GIVEN void inorderTraversal(.pdf
 

More from fasttrackscardecors

On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdfOn a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdffasttrackscardecors
 
Molecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdfMolecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdffasttrackscardecors
 
Missy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdfMissy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdffasttrackscardecors
 
Justify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdfJustify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdffasttrackscardecors
 
Inventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdfInventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdffasttrackscardecors
 
IncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdfIncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdffasttrackscardecors
 
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdfIf you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdffasttrackscardecors
 
Identify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdfIdentify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdffasttrackscardecors
 
I need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdfI need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdffasttrackscardecors
 
How is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdfHow is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdffasttrackscardecors
 
Fungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdfFungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdffasttrackscardecors
 
For each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdfFor each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdffasttrackscardecors
 
For each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdfFor each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdffasttrackscardecors
 
Explain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdfExplain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdffasttrackscardecors
 
Find all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdfFind all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdffasttrackscardecors
 
Describe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdfDescribe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdffasttrackscardecors
 
describe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdfdescribe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdffasttrackscardecors
 
Connect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdfConnect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdffasttrackscardecors
 
Consider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdfConsider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdffasttrackscardecors
 
Case History- An 11-month old Boy whos blue and short of breathC.pdf
Case History- An 11-month old Boy whos blue and short of breathC.pdfCase History- An 11-month old Boy whos blue and short of breathC.pdf
Case History- An 11-month old Boy whos blue and short of breathC.pdffasttrackscardecors
 

More from fasttrackscardecors (20)

On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdfOn a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
On a Sunday in April, dog bite victims arrive at Carver Memorial Hos.pdf
 
Molecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdfMolecules like glucose usually need a special transporter protein to.pdf
Molecules like glucose usually need a special transporter protein to.pdf
 
Missy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdfMissy Crane opened a public relations firm called Goth on August 1, 2.pdf
Missy Crane opened a public relations firm called Goth on August 1, 2.pdf
 
Justify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdfJustify that the following circuit represents the functionality of a .pdf
Justify that the following circuit represents the functionality of a .pdf
 
Inventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdfInventor classwhat is the purpose of using assemply constraints.pdf
Inventor classwhat is the purpose of using assemply constraints.pdf
 
IncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdfIncompressibilityI cant understand some parts in a book.The .pdf
IncompressibilityI cant understand some parts in a book.The .pdf
 
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdfIf you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
If you were to illuminate an Elodea leaf with light at 550 nm, would.pdf
 
Identify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdfIdentify the three major types of controls that organizations can us.pdf
Identify the three major types of controls that organizations can us.pdf
 
I need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdfI need proper and details explanation for this case study Financial .pdf
I need proper and details explanation for this case study Financial .pdf
 
How is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdfHow is Vietas contribution to mathematics distinctly modern in spi.pdf
How is Vietas contribution to mathematics distinctly modern in spi.pdf
 
Fungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdfFungal associations with plants Examine a type of fungal association .pdf
Fungal associations with plants Examine a type of fungal association .pdf
 
For each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdfFor each problem in this homework, your assignment is to determine .pdf
For each problem in this homework, your assignment is to determine .pdf
 
For each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdfFor each story problem, identofy the opertaion and the interpretatio.pdf
For each story problem, identofy the opertaion and the interpretatio.pdf
 
Explain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdfExplain a. casting b. overloading c. sentinel d. echoSolution.pdf
Explain a. casting b. overloading c. sentinel d. echoSolution.pdf
 
Find all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdfFind all the square roots of the complex number 14i. Write the squar.pdf
Find all the square roots of the complex number 14i. Write the squar.pdf
 
Describe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdfDescribe the flow of oxygenated blood from the placenta to the fe.pdf
Describe the flow of oxygenated blood from the placenta to the fe.pdf
 
describe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdfdescribe two different forms of bindingSolutionTwo forms Co.pdf
describe two different forms of bindingSolutionTwo forms Co.pdf
 
Connect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdfConnect onnect mheducation.co.pdf
Connect onnect mheducation.co.pdf
 
Consider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdfConsider the following interrupting system. The active-edge inputs o.pdf
Consider the following interrupting system. The active-edge inputs o.pdf
 
Case History- An 11-month old Boy whos blue and short of breathC.pdf
Case History- An 11-month old Boy whos blue and short of breathC.pdfCase History- An 11-month old Boy whos blue and short of breathC.pdf
Case History- An 11-month old Boy whos blue and short of breathC.pdf
 

Recently uploaded

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsNbelano25
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Pooja Bhuva
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Celine George
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxCeline George
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSAnaAcapella
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningMarc Dusseiller Dusjagr
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfNirmal Dwivedi
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17Celine George
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsSandeep D Chaudhary
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 

Recently uploaded (20)

HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
Sensory_Experience_and_Emotional_Resonance_in_Gabriel_Okaras_The_Piano_and_Th...
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17Model Attribute _rec_name in the Odoo 17
Model Attribute _rec_name in the Odoo 17
 
How to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptxHow to setup Pycharm environment for Odoo 17.pptx
How to setup Pycharm environment for Odoo 17.pptx
 
Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17How to Manage Call for Tendor in Odoo 17
How to Manage Call for Tendor in Odoo 17
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 

I have C++ question that I do not know how to do, Can you teach me t.pdf

  • 1. I have C++ question that I do not know how to do, Can you teach me this question: Draw a CLASS diagram for Binary Search Tree (BTS) : can you explain me each step, thank you Solution //program to convert Binary Tree to Binary Search Tree #include #include // A binary tree node structure struct node { int data; struct node *left; struct node *right; }; void storeInorder (struct node* node, int inorder[], int *index_ptr) { // Base Case if (node == NULL) return; // first store the left subtree storeInorder (node->left, inorder, index_ptr); // Copy the root's data inorder[*index_ptr] = node->data; (*index_ptr)++; // increase index for next entry // finally store the right subtree storeInorder (node->right, inorder, index_ptr); }
  • 2. /* A helper function to count nodes in a Binary Tree */ int countNodes (struct node* root) { if (root == NULL) return 0; return countNodes (root->left) + countNodes (root->right) + 1; } // Following function is needed for library function qsort() int compare (const void * a, const void * b) { return ( *(int*)a - *(int*)b ); } /* A helper function that copies contents of arr[] to Binary Tree. This functon basically does Inorder traversal of Binary Tree and one by one copy arr[] elements to Binary Tree nodes */ void arrayToBST (int *arr, struct node* root, int *index_ptr) { // Base Case if (root == NULL) return; /* first update the left subtree */ arrayToBST (arr, root->left, index_ptr); /* Now update root's data and increment index */ root->data = arr[*index_ptr]; (*index_ptr)++; /* finally update the right subtree */ arrayToBST (arr, root->right, index_ptr); } // This function converts a given Binary Tree to BST
  • 3. void binaryTreeToBST (struct node *root) { // base case: tree is empty if(root == NULL) return; /* Count the number of nodes in Binary Tree so that we know the size of temporary array to be created */ int n = countNodes (root); // Create a temp array arr[] and store inorder traversal of tree in arr[] int *arr = new int[n]; int i = 0; storeInorder (root, arr, &i); // Sort the array using library function for quick sort qsort (arr, n, sizeof(arr[0]), compare); // Copy array elements back to Binary Tree i = 0; arrayToBST (arr, root, &i); // delete dynamically allocated memory to avoid meory leak delete [] arr; } /* Utility function to create a new Binary Tree node */ struct node* newNode (int data) { struct node *temp = new struct node; temp->data = data; temp->left = NULL; temp->right = NULL; return temp; }
  • 4. /* Utility function to print inorder traversal of Binary Tree */ void printInorder (struct node* node) { if (node == NULL) return; /* first recur on left child */ printInorder (node->left); /* then print the data of node */ printf("%d ", node->data); /* now recur on right child */ printInorder (node->right); } /* Driver function to test above functions */ int main() { struct node *root = NULL; /* Constructing tree given in the above figure 10 / 30 15 / 20 5 */ root = newNode(10); root->left = newNode(30); root->right = newNode(15); root->left->left = newNode(20); root->right->right = newNode(5); // convert Binary Tree to BST binaryTreeToBST (root);
  • 5. printf("Following is Inorder Traversal of the converted BST: "); printInorder (root); return 0; }