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

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...

Website Worth Remembering

Following are the links of the website which are really helpful Maths and geometry  : Through this website you can perform all Geometry operation very easily online. Online image size reducer  : This Website is helpful to reduce size of image and is used when we want to reduse the image to size to very less kb without loosing its quality. Online Microsoft PowerPoint  : This website is very help in order to make online power point presentations without any need to install  PowerPoint in Laptop/PC. To know how to use   Step by Step Guide to use Online Microsoft PowerPoint    this website to make PowerPoint Presentation this link will help you.

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...