DEV Community

Cover image for Binary Tree Implementation
Ankit Rattan
Ankit Rattan

Posted on

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

πŸ’«πŸ«‘

Sentry image

See why 4M developers consider Sentry, β€œnot bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

πŸ‘‹ Kindness is contagious

Please leave a ❀️ or a friendly comment on this post if you found it helpful!

Okay