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

Why excess 3 is called self complimentary code?

Excess-3 (XS-3) is called a self-complementary code because the code for a digit and its complement (the digit subtracted from 9) can be derived directly from its representation. In XS-3, each decimal digit is represented by a 4-bit binary number that is equivalent to the digit plus three. For example, the XS-3 encoding of the digit '0' is '0011' (which is 3 in binary) and the encoding for '9' is '1100' (which is 12 in binary). When you take the 4-bit binary representation of a digit and its complement, they effectively mirror each other across the halfway point of the code, making the system self-complementary.


'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.