If Any Required Program Please Ask In Comment I Will Help You(any Program in JAVA or C++) . . THANKS FOR VISITING MY BLOG!

If U LIKE MY PROFILE RAISE YOUR HAND IF U NOT RAISE UR STANDARD. Powered by Blogger.

Saturday, December 29, 2018

PROGRAM FOR BINARY SEARCHING IN TREES using RECURSION.



#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

class B_Tree;

class Node
{
private:
    int Info;
    Node *Left, *Right;

    friend class B_Tree;
};

class B_Tree
{
private:
    Node *Root;

public:
    B_Tree()
    {
        Root = NULL;
    }

    Node* Make_Root(int Value)
    {
        Node *Temp = new Node;

         Temp->Info = Value;

         Temp->Left = NULL;
         Temp->Right = NULL;

         return Temp;
    }

    void Make_Tree()
    {
        int No;
        Node *P, *Q;

        cout << "Enter  Number To Make Root:  ";
        cin >> No;

        cout << endl;

        Root = Make_Root(No);

        while(No != 25)
        {
            cout << "Enter Any Number: ";
            cin >> No;

            P = Q = Root;

            while(P->Info != No && Q != NULL)
            {
                P = Q;

                if(No < P->Info)
                Q = P->Left;

                else
                    Q = P->Right;
            }

            if(P->Info == No)
            {
                cout << "\nDuplicate Numbers Occurred...!!!\n" << endl;
                exit(1);
            }

            else if(No < P->Info)
                Set_Left(P, No);

            else
                Set_Right(P, No);
        }
    }

        void Set_Left(Node *P, int Value)
    {
        if(P == NULL || P->Left != NULL)
        {
            cout << "\nCannot Insert On Left Of It...!!!\n" << endl;
            exit(1);
        }

        P->Left = Make_Root(Value);
    }

    void Set_Right(Node *P, int Value)
    {
        if(P == NULL || P->Right != NULL)
        {
            cout << "\nCannot Insert On Right OF It...!!!\n" << endl;
            exit(1);
        }

        P->Right = Make_Root(Value);
    }

    int BTree_Search(Node *P, int Value)
    {
        if(P == NULL)
        return 0;

        else if(P->Info == Value)
            return Value;

        else if(Value > P->Info)
            BTree_Search(P->Right,Value);

        else
            BTree_Search(P->Left, Value);
    }

    void Searching()
    {
        int No;

        cout << "\nEnter Any Value to Search: " ;
        cin >> No;

        if(BTree_Search(Root, No) == 0)
            cout << "\nValue Not Found...!!!\n" << endl;

        else
            cout << "\nValue Found...!!!\n" << endl;
    }
};

int main()
{
    cout << "\t\t\tEnter 25 To End......" << endl;

    B_Tree Obj;

    Obj.Make_Tree();
     Obj.Searching();

    getch();
    return 0;
}

PROGRAM TO COUNT SUM OF ALL NODES using RECURSION.



#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

class B_Tree;

class Node
{
private:
    int Info;
    Node *Left, *Right;

    friend class B_Tree;
};

class B_Tree
{
private:
    Node *Root;

public:
    B_Tree()
    {
        Root = NULL;
    }

    Node* Make_Root(int Value)
    {
        Node *Temp = new Node;

         Temp->Info = Value;

         Temp->Left = NULL;
         Temp->Right = NULL;

         return Temp;
    }

    void Make_Tree()
    {
        int No;
        Node *P, *Q;

        cout << "Enter  Number To Make Root:  ";
        cin >> No;

        cout << endl;

        Root = Make_Root(No);

        while(No != 25)
        {
            cout << "Enter Any Number: ";
            cin >> No;

            P = Q = Root;

            while(P->Info != No && Q != NULL)
            {
                P = Q;

                if(No < P->Info)
                Q = P->Left;

                else
                    Q = P->Right;
            }

            if(P->Info == No)
            {
                cout << "\nDuplicate Numbers Occurred...!!!\n" << endl;
                exit(1);
            }

            else if(No < P->Info)
                Set_Left(P, No);

            else
                Set_Right(P, No);
        }
    }

        void Set_Left(Node *P, int Value)
    {
        if(P == NULL || P->Left != NULL)
        {
            cout << "\nCannot Insert On Left Of It...!!!\n" << endl;
            exit(1);
        }

        P->Left = Make_Root(Value);
    }

    void Set_Right(Node *P, int Value)
    {
        if(P == NULL || P->Right != NULL)
        {
            cout << "\nCannot Insert On Right OF It...!!!\n" << endl;
            exit(1);
        }

        P->Right = Make_Root(Value);
    }

    int Recursion_Sum(Node *P)
    {
        if(P == NULL)
            return 0;

            int Total = P->Info + Add(P->Left) + Add(P->Right);

        return Total;
    }

    void Add_Sum()
    {
        cout << "\nSum Of All Nodes Is: " << Recursion_Sum(Root) << endl;
    }
};

int main()
{
    cout << "\t\t\tEnter 25 To End......" << endl;

    B_Tree Obj;

    Obj.Make_Tree();
     Obj.Add_Sum();

    getch();
    return 0;
}

PROGRAM FOR SORTING ARRAY USING QUICK SORT.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Right;

public:
Array(int Siz)
{
Arr = new int[Siz];

for (int I = 0; I <Siz; I++)
Arr[I] = 0;

Right = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "Enter Size Of Array: ";
cin >> No;

Right = No;

cout << "\nEnter Elements Of Array: " << endl;

for (int I = 0; I <Right; I++)
cin >> Arr[I];
}

void Quick_Sort()
{
    Sort(Arr, 0, Right -1);
}

    void Sort(int Temp[], int Beg, int End)
    {
        if(Beg >= End)
            return;

        int Mid = Partition(Temp, Beg, End);

        Sort(Temp, Beg, Mid -1);
        Sort(Temp, Mid +1, End);
    }

     int Partition (int Temp[], int Beg, int End)
    {
        int Pivot = Temp[Beg];
        int Index = Beg ;

        for (int J = Beg +1; J <= End; J++)
        {
            if (Temp[J] < Pivot)
            {
                Index++;
                Swap(&Temp[Index], &Temp[J]);
            }
        }

        Swap(&Temp[Index], &Temp[Beg]);
        return Index;
    }

    void Swap(int *Value1, int *Value2)
    {
        int Temp = *Value1;
        *Value1 = *Value2;
        *Value2 = Temp;
    }

void Print()
{
cout << "\nAfter Sorting Elements Of Array is: " << endl;

for (int I = 0; I <Right; I++)
cout << Arr[I] << endl;
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(20);

Obj.Input();
Obj.Quick_Sort();
Obj.Print();

getch();
return 0;
}

PROGRAM FOR SORTING ARRAY USING MERGE SORT.



#include <iostream>
#include <conio.h>

using namespace std;

class Array
{
private:
int *Arr, Size, Right;

public:
Array(int Siz)
{
Arr = new int[Siz];
for (int I = 0; I <Siz; I++)
{
Arr[I] = 0;
}

Right = 0;
Size = Siz;
}

void Input()
{
int No;

cout << "Enter Size Of Array: ";
cin >> No;

Right = No;

cout << "\nEnter Elements Of Array: " << endl;
for (int I = 0; I <Right; I++)
{
cin >> Arr[I];
}
}

void Merge_Sort()
{
    Sorting(Arr, 0, Right -1);
}

void Sorting(int Temp[], int Beg, int End)
{
    int Mid;
    Mid = (Beg + End) /2;

    if(Beg >= End)
            return;

        Sorting(Temp, Beg, Mid);
        Sorting(Temp, Mid +1, End);

        Merge_Array(Temp, Beg, Mid, End);
}

void Merge_Array(int Temp[], int Beg, int Mid, int End)
{
    int Size1 = Mid - Beg +1;
    int Size2 = End - Mid;

    int Array1[Size1], Array2[Size2];

    for(int I = 0; I <Size1; I++)
            Array1[I] = Temp[Beg +I];

        for(int J = 0; J <Size2; J++)
            Array2[J] = Temp[Mid +1 +J];

    int I, J, K;
        I = J = 0;
        K = Beg;

        while(I < Size1 && J < Size2)
        {
            if(Array1[I] < Array2[J])
                Temp[K] = Array1[I++];

                else
                    Temp[K] = Array2[J++];

                K++;
        }

        while(I <Size1)
            Temp[K++] = Array1[I++];

        while(J <Size2)
            Temp[K++] = Array2[J++];
}

void Print()
{
cout << "\nElements Of Array is: " << endl;

for (int I = 0; I <Right; I++)
cout << Arr[I] << endl;
}

~Array()
{
cout << "\n\n\n\n************PROGRAM ENDED***************";
}
};

int main()
{
Array Obj(20);

Obj.Input();
Obj.Merge_Sort();
Obj.Print();

getch();
return 0;
}

Saturday, December 1, 2018

PROGRAM TO IMPLEMENT INORDER TRAVERSAL using STACK.



#include <iostream>
#include <stdlib.h>
#include <conio.h>

using namespace std;

class Node;
class B_Tree;

template <class Type>

class Stack
{
private:
Type *Arr;
int Top, Size;

public:
Stack(int Siz)
{
Size = Siz;

Arr = new Type[Size];
for (int I = 0; I <Size; I++)
{
Arr[I] = 0;
}

Top = -1;
}

void Push(Type Num)
{
if (Top == Size - 1)
{
cout << "\nStack Overflowed..........." << endl;
exit(1);
}

Arr[++Top] = Num;
}

Type Pop()
{
if (IsEmpty())
{
cout << "\nStack Underflowed..........." << endl;
return 0;
}

return Arr[Top--];
}

inline bool IsEmpty()
{
return (Top == -1);
}
};

class Node
{
private:
    int Info;
    Node *Left, *Right;

    friend class B_Tree;
};

class B_Tree
{
private:
    Node *Root;

public:
    B_Tree()
    {
        Root = NULL;
    }

    Node* Make_Root(int Value)
    {
        Node *Temp = new Node;

        Temp->Info = Value;

        Temp->Left = NULL;
        Temp->Right = NULL;

        return Temp;
    }

    void Set_Left(Node *P, int Value)
    {
        if(P == NULL || P->Left != NULL)
        {
            cout << "\nCannot Insert On Left Of It...!!!\n" << endl;
            exit(1);
        }

        P->Left = Make_Root(Value);
    }

    void Set_Right(Node *P, int Value)
    {
        if(P == NULL || P->Right != NULL)
        {
            cout << "\nCannot Insert On Right Of It...!!!\n" << endl;
            exit(1);
        }

        P->Right = Make_Root(Value);
    }

    void Make_Tree()
    {
        int No;
        Node *P, *Q;

        cout << "\nEnter Number To Make Root: ";
        cin >> No;

        cout << endl;

        Root = Make_Root(No);

        while(No != 25)
        {
            cout << "Enter Any Number: ";
            cin >> No;

            P = Q = Root;

            while(P->Info != No && Q != NULL)
            {
                P = Q;

                if(No < P->Info)
                    Q = P->Left;

                else
                    Q = P->Right;
            }

            if(No == P->Info)
            {
                cout << "\nDuplicate Numbers Occurred...!!!\n" << endl;
                exit(1);
            }

            else if(No < P->Info)
                Set_Left(P, No);

            else
                Set_Right(P, No);
        }
    }

    void InTraverse_2()
    {
        Stack <Node*> Obj(20);

        Node *New = Root;

        do
        {
            while(New != NULL)
            {
                Obj.Push(New);
                New = New->Left;
            }

            if(!Obj.IsEmpty())
            {
                New = Obj.Pop();

                cout <<  New->Info << " ";

                New = New->Right;
            }
        }
        while(!Obj.IsEmpty() || New != NULL);
    }
};


int main()
{
    B_Tree Obj;

    cout << "\t\t\tEnter 25 to End Making Nodes....." << endl;

    Obj.Make_Tree();

    cout << "\nTree In InOrder Traverse is: ";
    Obj.InTraverse_2();

    getch();
    return 0;
}