creation of a binary tree
Thu Oct 17 2024 17:22:10 GMT+0000 (Coordinated Universal Time)
Saved by
@E23CSEU1151
#include <iostream>
using namespace std;
class node {
public:
int data;
node* left;
node* right;
node(int d) {
this->data = d;
this->left = NULL;
this->right = NULL;
}
};
node* buildTree() {
cout << "Enter the data (-1 for no node): "<<endl;
int data;
cin >> data;
if (data == -1) {
return NULL;
}
node* root = new node(data); // Create a new node
cout << "Enter data for left child of " << data << ": "<<endl;
root->left = buildTree();
cout << "Enter data for right child of " << data << ": "<<endl;
root->right = buildTree();
return root;
}
int main() {
node* root = buildTree(); // Create the tree starting from root
return 0;
}
content_copyCOPY
Comments