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

Island Where Nobody Is Allowed To Enter

Islands are the most beautiful place to spend vacations . In oceans and sea thousand of island have been discovered and many of them are used as a tourist place . Every year large number of people from different part of the world visit island .But their is an island in Indian ocean where nobody is allowed to enter .If someone try to enter island he/she will be killed because of a community who lives in this island and dislike outsider entering their island. Even Indian government has given up trying to make contact with community of island .In 2005 administration declared them as "independent " and stated that they do not have any intention to interfere lifestyle of native of that island . The community that live on this island is 6000 old . This community has rejected modern civilisation and oppose any kind of communication . They don't like outsider in their island and so they attack on outsider and even kill them . They have several time atta...
                                                          Mental health- a serious issue In today's world many kind of  situations are creating their own niche in our life and giving us a reason to thinkers about.. All the problems, situations have one thing in common that they are occupying space in our mind , affecting our mental health badly. People suffering from these issues  got depressed and feels like getting into swamp from which they are unable to come out.proper counseling and help from near ones is needed for them to come out of situations .

Online Microsoft PowerPoint

STEP BY STEP GUIDELINES FOR USING ONLINE MICROSOFT POWER POINT 1. Open browser and search   online power point  . 2. Page will open showing various option click first link   Microsoft PowerPoint online-Work together on PowerPoint...  3. For using first time it will ask option of sign-in or sign-up. 4. If already have account then click sign-in and fill email id/ mobile number  and password otherwise create an account first . 5. After a few second a page will load up with lots of PowerPoint templates. 6. Click on first template   New blank Presentation. 7. After this a new blank presentation will load up on browser. 8. Now start creating your presentation here . 9. After this ,  to download to your computer click    File   option on top-left corner. 10. Click Download as that is 3 last option . 11. It will ask for Download as click first option Download a Copy . So finally PowerP...