Logo

dev-resources.site

for different kinds of informations.

Binary Tree Implementation

Published at
8/23/2024
Categories
dsa
beginners
algorithms
programming
Author
Ankit Rattan
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;
}

💫🫡

Featured ones: