Skip to main content

Code on Tree Operations In C++

/*   These are  c++ code on Trees All codes are commented to view the output of code uncomment it and comment all other codes to see it's output.  */

#include<bits/stdc++.h>
using namespace std;

//code 1
/*int main()
{
    int n,i;
    cin>>n;
    int a[n+1];
    for(i=0;i<n;i++)
    {
        cin>>a[i];
    }
    bool flag=true;
    for(i=0;i<(n/2)-1;i++)
    {
        if(a[i]<a[(2*i)+1] || a[i]<a[2*i+2])
        {
            flag=false;
            break;
        }
    }
    if(flag==true)
    {
        cout<<"it is max heap"<<endl;

    }
    else
    {

        cout<<"not a max heap"<<endl;
    }
    return 0;
}*/


//code 2
/*
int main()
{
    priority_queue<int,vector<int>, greater<int> > pq;
    pq.push(10);
    pq.push(100);
    pq.push(1);
    pq.push(1000);
    pq.push(10000);
    pq.push(11);
    priority_queue<int, vector<int>, greater<int> > nq=pq;
    while(!nq.empty())
    {

        cout<<nq.top()<<endl;
        nq.pop();
    }
    return 0;
}
*/

//code 3

/* A binary tree node has key, pointer to left child
and a pointer to right child */
/*
struct node {
    int data;
    node* left, *right;
};

node* newNode(int data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->left = newnode->right = NULL;
    return newnode;
};


void inorder(node* root)
{
    if (root==NULL)
        return;

    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
}

void insert(node* root, int data)
{
    queue<node*> q;
    q.push(root);

    while (!q.empty()) {
        node* root = q.front();
        q.pop();

        if (!root->left) {
            root->left = newNode(data);
            break;
        } else
            q.push(root->left);

        if (!root->right) {
            root->right = newNode(data);
            break;
        } else
            q.push(root->right);
    }
}

int main()
{
    node* root = newNode(10);
    root->left = newNode(20);
    root->right = newNode(30);
    root->left->left = newNode(40);
    root->left->right=newNode(50);

    inorder(root);
    cout << endl;
    int data = 60;
    insert(root, data);
    inorder(root);

    return 0;
}
*/

//code 4

/*struct node {
    int data;
    node* left, *right;
};

node* newNode(int data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->left = newnode->right = NULL;
    return newnode;
};


void inorder(node* root)
{
    if (root==NULL)
        return;

    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
}
// preorder
void preorder(node* root)
{
    if (root==NULL)
        return;


    cout << root->data << " ";
    inorder(root->left);
    inorder(root->right);
}

//postoder
void postorder(node* root)
{
    if (root==NULL)
        return;

    inorder(root->left);
    inorder(root->right);
    cout << root->data << " ";
}
//levelorder
void levelorder(node *root)
{
    queue<node *>q;
    q.push(root);
    while(!q.empty())
    {
        node *da=q.front();
        q.pop();
        cout<<da->data<<" ";
        if(da->left!=NULL)
        {
            q.push(da->left);
        }
        if(da->right!=NULL)
        {
            q.push(da->right);
        }
    }

}

void insert(node* root, int data)
{
    queue<node*> q;
    q.push(root);

    while (!q.empty()) {
        node* root = q.front();
        q.pop();

        if (!root->left) {
            root->left = newNode(data);
            break;
        } else
            q.push(root->left);

        if (!root->right) {
            root->right = newNode(data);
            break;
        } else
            q.push(root->right);
    }
}

int main()
{
    node* root = newNode(10);
    root->left = newNode(20);
    root->right = newNode(30);
    root->left->left = newNode(40);
    root->left->right=newNode(50);

    inorder(root);
    cout << endl;
    int data = 60;
    insert(root, data);
    cout<<endl<<"INORDER"<<endl;
    inorder(root);
    cout<<endl<<"PREORDER"<<endl;
    preorder(root);
    cout<<endl<<"POSTORDER"<<endl;
    postorder(root);
    cout<<endl<<"LEVELORDER"<<endl;
    levelorder(root);
    return 0;
}
*/
// code 6

struct node {
    int data;
    node* left, *right;
};

node* newNode(int data)
{
    node* newnode = new node;
    newnode->data = data;
    newnode->left = newnode->right = NULL;
    return newnode;
};


void inorder(node* root)
{
    if (root==NULL)
        return;

    inorder(root->left);
    cout << root->data << " ";
    inorder(root->right);
}
// preorder
void preorder(node* root)
{
    if (root==NULL)
        return;


    cout << root->data << " ";
    inorder(root->left);
    inorder(root->right);
}

//postoder
void postorder(node* root)
{
    if (root==NULL)
        return;

    inorder(root->left);
    inorder(root->right);
    cout << root->data << " ";
}
//levelorder
void levelorder(node *root)
{
    queue<node *>q;
    q.push(root);
    while(!q.empty())
    {
        node *da=q.front();
        q.pop();
        cout<<da->data<<" ";
        if(da->left!=NULL)
        {
            q.push(da->left);
        }
        if(da->right!=NULL)
        {
            q.push(da->right);
        }
    }

}

// delete from tree
void deltree(node *root,int number)
{
    queue<node *>q;
    q.push(root);
    // finding last element
    node *da;
    while(!q.empty())
    {
        da=q.front();
        q.pop();
        if(da->left!=NULL)
        {
            q.push(da->left);
        }
        if(da->right!=NULL)
        {
            q.push(da->right);
        }
    }
    int element=da->data;
    // replaced
    q.push(root);

    while(!q.empty())
    {
        da=q.front();
        q.pop();
        if(da->data==number)
        {
            da->data=element;
            break;
        }
        if(da->left!=NULL)
        {
            q.push(da->left);
        }
        if(da->right!=NULL)
        {
            q.push(da->right);
        }
    }
    cout<<endl<<"TRAVESAL OF ELEMENT"<<endl;
    queue<node *>qe;
    qe.push(root);
    while(!qe.empty())
    {
        node *da=qe.front();
        qe.pop();
        if(da->data!=element && da->left!=NULL && da->right!=NULL)
        {
            cout<<da->data<<" ";
        }

        if(da->left!=NULL)
        {
            qe.push(da->left);
        }
        if(da->right!=NULL)
        {
            qe.push(da->right);
        }
    }

}
int height(node *root)
    {
        if(root==NULL)
        {
            return 0;
        }
        return max(height(root->left),height(root->right))+1;

    }
void insert(node* root, int data)
{
    queue<node*> q;
    q.push(root);

    while (!q.empty()) {
        node* root = q.front();
        q.pop();

        if (!root->left) {
            root->left = newNode(data);
            break;
        } else
            q.push(root->left);

        if (!root->right) {
            root->right = newNode(data);
            break;
        } else
            q.push(root->right);
    }
}

int main()
{
    node* root = newNode(10);
    root->left = newNode(20);
    root->right = newNode(30);
    root->left->left = newNode(40);
    root->left->right=newNode(50);

    inorder(root);
    cout << endl;
    int data = 60;
    insert(root, data);
    cout<<endl<<"INORDER"<<endl;
    inorder(root);
    cout<<endl<<"PREORDER"<<endl;
    preorder(root);
    cout<<endl<<"POSTORDER"<<endl;
    postorder(root);
    cout<<endl<<"LEVELORDER"<<endl;
    levelorder(root);
    data=30;
    deltree(root,30);
    /*cout<<endl<<"LEVELORDER AFTER DELETION"<<endl;
    levelorder(root);*/
    cout<<endl<<"HEIGHT OF TREE "<<endl;
    cout<<height(root)<<endl;
    return 0;
}

Comments

Popular posts from this blog

Rajinikanth :Turns from actor to politician

Rajinikanth is the most successful, respected and loved actor of south Indian movies. He has entertained audience from past 22 years. His fans even take holiday from their work to watch his new movies. In these past 22 year he earned huge success and respect in movie.He got mass fan following in not only south but all over India. He had given many entertaining movies like Shivaji the boss and Robot. Along with his success in movies he was also having dreaming to enter into politics and established his name as a good politician. In 1995 for the first time he showed his interest in politics by speaking against AIADMK leader Jayalalitha. His statement against Jayalalitha created huge controversy. Later he made peace with Jayalalitha and ended his controversy. After  1995 he tried several times to find opportunity to enter into politics but failed in his attempts. Now after death of Jayalalitha head of AIADMK and political inactive due to illness leader of DMK(Political par...

Artificial Intelligence

                                              Can AI become threat to humans Technologies have developed so much and now our most of the works are done by machines very quickly and more efficiently .We are building machines for our comfort and our convenience .AI is the technology that will make gadgets more smart and our life more comfortable . It will work as all gadget will be connected by internet But now some people fear that if we keep depending on the machines/gadgets and make more advancement in this one day they will overpower us and make us our slaves or end humans species . In my opinion this rumour doesn't look possible because of many reasons ... *   Their is no current technology that can insert feeling like anger, jealousy,love,happy,sad in machines . *  We have created machines with super human technology and we are not stupid enough...
Two major components that are widely used in most of growing technology like IOT  are Sensors and Actuators. Sensor: It is a device that is used to detect any change in abident condition or in state of device. Major types of sensor are :- Light sensor :detect any change light device. Temperature sensor : It is used to detect increase or decrease of temperature of device. Sound sensor :It detect any change in sound of device. Sometimes some error in sensing some common error Offset error: output signal differ from correct value by constant factor. Hystrisis error: error in output due Heat. Quantization error :This error occurs in digital devices. Actuators: After detection of change in state of system it is the actuators which is responsible for taking action on system. It is responsible for control mechanism of system. Actuator is composed of two parts control signal which is received from sensor and source of energy with the help of which it...