answersLogoWhite

0

#include<iostream>

template<typename T>

struct node

{

T data;

node* left {nullptr};

node* right {nullptr};

node (const T& value): data (value) {}

~node () { delete left; delete right; }

void insert (const T& value)

{

if (value < data)

{

if (left)

left->insert (value);

else

left = new node (value);

}

else

{

if (right)

right->insert (value);

else

right = new node (value);

}

}

};

template<typename T>

struct bin_tree

{

node<T>* root {nullptr};

~bin_tree () { delete root; }

void insert (const T& value)

{

if (!root)

root = new node<T> (value);

else

root->insert (value);

}

};

template<typename T>

std::ostream& operator<< (std::ostream& os, const node<T>& n)

{

if (n.left) os << (*n.left);

os << n.data << ' ';

if (n.right) os << (*n.right);

return os;

}

template<typename T>

std::ostream& operator<< (std::ostream& os, const bin_tree<T>& t)

{

if (t.root)

return os << (*t.root);

return os;

}

int main()

{

int arr[10] = {42, 56, 30, 5, 12, 60, 40, 8, 1, 99};

bin_tree<int> tree;

for (size_t e=0; e!=10; ++e)

tree.insert (arr[e]);

std::cout << tree << std::endl;

}

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

'How does a C plus plus code execute on a computer system' like does the computer has a library or such that can understand C plus language and translate it into binary?

By execution time, the code has already been translated into binary ("compiled"). However, the program may still rely on outside libraries (.dlls for example) that have also been pre-compiled.


What is a source code file in c plus plus?

A source code file is a plain-text file containing C++ instructions. The instructions must be compiled and linked to create a native machine code executable.


What is the c plus plus code for application of linked list in mobile phones?

If people started writing these codes here, then what software engineers will do?


Zelda oracle of ages heroes code plus all rings?

The Hero Code is NNNNN NNNNN NNNNN NNNNN and the Ring code is dependent on your linked game.


Can C plus plus read binary?

yes it can very much so read binary.


How do you convert from assembly to binary in c plus plus?

Use inline assembly instructions. Then compile your C++ program to produce the machine code.


In the binary system what is 1 plus 1?

1 plus 1 = 2


What is plus operator is it unary or binary?

There is no unary plus in C, but if there were, it would have only one operand, unlike the binary plus which has two: x = a + b; /* binary plus */ x = + b; /* unary plus -- not in C*/ x = a - b; /* unary plus */ x = - b; /* unary minus */


What is 1 plus 1 in binary?

1 + 1 = 10 in binary numbers.


Is C plus plus interpreted as the program is executed?

No. Neither C nor C++ are interpreted. Both need to be compiled and linked to produce highly-optimised machine code, which is then executed.


What is 1 plus 1 plus?

2 decimal, or 10 binary.


What is the sum of the binary numbers of 1001 plus 10 in both binary and decimal?

easy, 1011. in binary of course. convert 1011 binary to decimal you get 11.