would you show me the to create and insert into balanced tree in C++ and conside
ID: 3802632 • Letter: W
Question
would you show me the to create and insert into balanced tree in C++ and consider that i am a begineer
ant tree. addchildren (plant & parentPlant, plant &left; Plant,plant 6rightPiant if (!root) root new treenode; root- plants parent Plant root- left root- right NULL root- countr-root- countl-0; root- left new treenode; root- left- plants left Plant; root- left- left-root- left-right NULL; root- countr-root- countl-0; root->right new treenode; root- right- plants rightPlant; root- right- left-root- right- right NULL; root- countr root- countl-0; tLue return 1; return else if (root- countl root->countr) root- countl addchildren (parent Plant, left Plant, rightPlant, root- left); else if (root- countl root- countr) root- countl++I left) addchildren (parentPlant, left Plant,rightPlant, root- else root- countr addchildren (parentPlant,leftPlant, rightPlant, root- right): E Plant entPla ildren right P antExplanation / Answer
#include <iostream>
#include<conio.h>
#include<fstream.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
typedef struct b_tree_node
{
int u;
struct b_tree_node *left;
struct b_tree_node *right;
} BNTNode;
BNTNode *create_b_tree_node(int u)
{
BNTNode *m = new BNTNode;
if (m != empty)
{
m->u = u;
m->left = empty;
m->right = empty;
}
return m;
}
void create_bal_b_tree(BNTNode **root, int array[], int begin, int quit)
{
if (begin <= quit)
{
int middle = (begin + quit + 1) / 2;
*root = create_b_tree_node(array[middle]);
create_bal_b_tree(&((*root)->left), array, begin, middle - 1);
create_bal_b_tree(&((*root)->right), array, middle + 1, quit);
}
}
void print_b_tree(BNTNode *root)
{
if (root != empty)
{
cout << root->u << " ";
print_b_tree(root->left);
print_b_tree(root->right);
}
}
void print_b_tree1(BNTNode *root)
{
if (root != empty)
{
print_b_tree1(root->left);
cout << root->u << " ";
print_b_tree1(root->right);
}
}
int main(int argc, char* argv[])
{
int arr[40];
for (int k = 0; k< 40; k++)
{
arr[k] = k;
}
BNTNode *root = EMPTY;
create_bal_b_tree(&root, array, 0, 39);
cout << "Preorder of bal tree is: "<<endl;
print_b_tree(root);
cout << "Inorder of bal tree is: "<<endl;
print_b_tree1(root);
return 0;
}
Output:
$ g++ BalBTree.cpp
$ a.out
Preorder of balanced tree is: 15 7 3 1 0 2 36 5 4 6 11 37 9 8 10 13 31 12 14 34 23 19 32 17 16 33 18 21 35 20 22 38 27 25 24 39 26 29 28 30
Inorder of balanced tree is: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
(program exited with code: 0)
Press return to continue
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.