Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Binary Tree.cpp #938

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions Binary Tree.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include <iostream>

// Define a basic structure for a binary tree node
struct Node {
int data;
Node* left;
Node* right;

Node(int value) : data(value), left(nullptr), right(nullptr) {}
};

// Function to insert a new node into the binary tree
Node* insert(Node* root, int value) {
if (root == nullptr) {
return new Node(value);
}

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

return root;
}

// Function to perform an inorder traversal of the binary tree
void inorderTraversal(Node* root) {
if (root == nullptr) {
return;
}

inorderTraversal(root->left);
std::cout << root->data << " ";
inorderTraversal(root->right);
}

int main() {
Node* root = nullptr;

// Insert nodes into the binary tree
root = insert(root, 50);
root = insert(root, 30);
root = insert(root, 20);
root = insert(root, 40);
root = insert(root, 70);
root = insert(root, 60);
root = insert(root, 80);

// Perform an inorder traversal to print the elements in sorted order
std::cout << "Inorder traversal: ";
inorderTraversal(root);
std::cout << std::endl;

return 0;
}