#include <iostream>
using namespace std;

struct Node {
    float* data;   // pointer to a float
    Node* left;
    Node* right;
};

// Create a new node
Node* createNode(float* value) {
    Node* newNode = new Node();
    newNode->data = value;
    newNode->left = nullptr;
    newNode->right = nullptr;
    return newNode;
}

// Insert into binary tree (not balancing, causes O(n²) if input sorted)
Node* insert(Node* root, float* value) {
    if (root == nullptr) return createNode(value);

    if (*value < *(root->data))
        root->left = insert(root->left, value);
    else
        root->right = insert(root->right, value);

    return root;
}

// In-order traversal (Left → Root → Right)
void inOrder(Node* root) {
    if (!root) return;
    inOrder(root->left);
    cout << *(root->data) << " ";
    inOrder(root->right);
}

int main() {
    int n;
    cout << "Enter number of elements: \n";
    cin >> n;

    float* arr = new float[n]; // allocate array dynamically
    Node* root = nullptr;

    cout << "Enter \n" << n << " float numbers (preferably sorted to simulate O(n²)): " << endl;
    for (int i = 0; i < n; i++) {
        cin >> arr[i];
        root = insert(root, &arr[i]); // insert address of each float
    }

    cout << "In-order Traversal: ";
    inOrder(root);
    cout << endl;

    delete[] arr; // clean up
    return 0;
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: