In: Computer Science
// C++ Program of above implementation
#include <iostream>
using namespace std;
// Struct declaration
struct Node {
int data;
struct Node* left;
struct Node* right;
};
// Node creation
struct Node* newNode(int data)
{
struct Node* nn
= new Node;
nn->data = data;
nn->left = NULL;
nn->right = NULL;
return nn;
}
// Function to insert data in BST
struct Node* insert(struct Node* root, int data)
{
if (root == NULL)
return newNode(data);
else {
if (data < root->data)
root->left = insert(root->left, data);
if (data > root->data)
root->right = insert(root->right, data);
return root;
}
}
// InOrder function to display value of array
// in sorted order
void inOrder(struct Node* root)
{
if (root == NULL)
return;
else {
inOrder(root->left);
cout << root->data << " ";
inOrder(root->right);
}
}
// Driver code
int main()
{
int arr[] = { 199, 666, 930, 751, 120, 411, 534, 716, 134, 379, 908, 97, 740, 904, 575, 437, 983, 835, 931, 768, 405, 544, 553, 821, 160, 312, 228, 324, 675, 622, 768, 312, 310, 18, 558, 170, 842, 214, 545, 857, 6, 71, 421, 909, 908, 377, 488, 837, 884, 382, 1, 654, 207, 751, 459, 450, 431, 462, 40, 926, 304, 594, 966, 386, 760, 39, 297, 557, 125, 327, 278, 328, 937, 195, 346, 419, 953, 681, 203, 173, 807, 710, 428, 415, 601, 258, 981, 464, 568, 546, 308, 883, 431, 948, 529, 55, 651, 73, 411, 719, 172, 43, 249, 695, 636, 603, 447, 652, 211, 736, 923, 27, 198, 427, 154, 572, 671, 314, 25, 428, 900, 930, 378, 806, 281, 805, 627, 748, 23, 961, 244, 587, 352, 939, 880, 69, 52, 702, 856, 990, 19, 743, 132, 793, 279, 500, 776, 921, 583, 487, 638, 753, 209, 870, 506, 995, 691, 743, 501, 831, 549, 369, 718, 780, 17, 636, 217, 486, 548, 500, 381, 116, 84, 683, 908, 151, 633, 404, 4, 426, 92, 614, 851, 714, 90, 789, 441, 985, 539, 470, 891, 384, 962, 259, 938, 211, 683, 560, 630, 893, 106, 841, 974, 114, 239, 896, 796, 524, 896, 974, 583, 729, 577, 863, 696, 530, 670, 467, 841, 401, 209, 596, 862, 702, 21, 219, 902, 271, 293, 350, 357, 182, 179, 909, 874, 769, 192, 39, 7, 487, 571, 155, 565, 231, 36, 544, 443, 24, 596, 570, 129, 585, 529, 557, 833, 270, 241, 732, 569, 839, 762, 881, 177, 149, 413, 806, 545, 591, 721, 289, 453, 53, 470, 215, 480, 218, 787, 915, 532, 626, 18, 349, 216, 338, 572, 979, 939, 76, 55, 49, 727, 524, 68, 403, 941, 234, 85, 991, 698, 561 };
// Finding size of array arr[]
int n = sizeof(arr) / sizeof(arr[0]);
struct Node* root = NULL;
for (int i = 0; i < n; i++) {
// Insert element of arr[] in BST
root = insert(root, arr[i]);
}
// Inorder Traversal to print nodes of Tree
inOrder(root);
return 0;
}