Binary Tree Implementation

ankit_rattan

Ankit Rattan

Posted on August 23, 2024

Binary Tree Implementation

Well! One by one I will be posting all the relevant codes required for basic DSA.
Starting with Binary Tree, there are various videos explaing what Binary tree is all about, well here is one line which I like the most : Binary tree is one of the simple form of Graphs!
If you don't know what Graphs are... no worries! I will be soon after this, going to post all necessary codes and details related to graphs.

Here you go for Basic implementation of Graphs!

#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(node* root){
    cout<<"Enter the data: "<<endl;
    int data;
    cin>>data;

    root = new node(data);

    if(data == -1) return NULL;

    cout<<"Enter the data for the left of "<<data<<endl;
    root -> left = buildTree(root -> left);
    cout<<"Enter the data for the right of "<<data<<endl;
    root -> right = buildTree(root -> right);
    return root;
}

int main()
{
    node* root = NULL;
    root = buildTree(root);


    return 0;
}
Enter fullscreen mode Exit fullscreen mode

šŸ’«šŸ«”

šŸ’– šŸ’Ŗ šŸ™… šŸš©
ankit_rattan
Ankit Rattan

Posted on August 23, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Binary Tree Implementation
dsa Binary Tree Implementation

August 23, 2024