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;
}

Friday, November 30, 2018

PROGRAM TO IMPLEMENT THE TREE TRAVERSELS.



#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);
    }

    void Pre_Traverse(Node* T)
    {
        if(T != NULL)
        {
            cout << T->Info << " ";

            Pre_Traverse(T->Left);
            Pre_Traverse(T->Right);
        }
    }

    void Post_Traverse(Node *T)
    {
        if(T != NULL)
        {
            Post_Traverse(T->Left);
            Post_Traverse(T->Right);

            cout << T->Info << " ";
        }
    }

    void In_Traverse(Node *T)
    {
        if(T != NULL)
        {
            In_Traverse(T->Left);

            cout << T->Info << " ";

            In_Traverse(T->Right);
        }
    }

    void Traverses()
    {
        cout << "\nTree In PreOrder Traverse Is: " << endl;
        Pre_Traverse(Root);

        cout << "\n\nTree In InOrder Traverse Is: " << endl;
        In_Traverse(Root);

        cout << "\n\nTree In PostOrder Traverse Is: " << endl;
        Post_Traverse(Root);
    }
};

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

    B_Tree Obj;

    Obj.Make_Tree();
    Obj.Traverses();

    getch();
    return 0;
}

PROGRAM TO IMPLEMENT THE DOUBLY LINKLIST.



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

using namespace std;

class Double_LL;

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

    friend class Double_LL;
};

class Double_LL
{
private:
    Node *First;

public:
    Double_LL()
    {
        First = NULL;
    }

    void First_Node(int Value)
    {
           Node *Temp = new Node;
           Temp->Info = Value;

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

          First = Temp;
    }

    void Insert_Right(int Value1, int Value2)
    {
        Node *R, *P, *Temp;

        Temp = new Node;
        Temp->Info = Value2;

            P = Search(Value1);
            R = P->Right;

            if(P == NULL)
            {
                cout << "\nCannot Insert On Right Of It.....!!!\n" << endl;
                exit(1);
            }

            Temp->Left = P;
            P->Right = Temp;

            Temp->Right = R;

            if(R != NULL)
                R->Left = Temp;
    }

    void Insert_Left(int Value1, int Value2)
    {
        Node *L, *P, *Temp;

        Temp = new Node;
        Temp->Info = Value2;

        P = Search(Value1);
        L = P->Left;

        if(P == NULL)
        {
            cout << "\nCannot Insert On Left Of It.....!!!\n" << endl;
            exit(1);
        }

        Temp->Right = P;
        P->Left = Temp;

        Temp->Left = L;

        if(L != NULL)
            L->Right = Temp;
    }

    void Remove(int Value)
    {
        Node *L, *R, *Temp;

        Temp = Search(Value);

        if(Temp == NULL || IsEmpty())
        {
            cout << "\nDeletion is Not Possible...!!!\n" << endl;
            exit(1);
        }

        L = Temp->Left;
        R = Temp->Right;

        cout << "Value To Be Deleted is: " << Temp->Info << endl;

        if(L == NULL)
            R->Left = NULL;

        else if(R == NULL)
            L->Right = NULL;

        else
        {
            L->Right = R;
            R->Left = L;
        }
    }

    Node* Search(int Value)
    {
        Node *Temp;

        for(Temp = First; Temp != NULL ;Temp = Temp->Right)
        {
            if(Temp->Info == Value)
                return Temp;
        }

        return NULL;
    }

    void Print()
    {
        Node *Temp = new Node;

        for(Temp = First; Temp != NULL; Temp = Temp->Right)
            cout << "Value Of Node Is " << Temp->Info << endl;
    }

    inline bool IsEmpty()
    {
        return First == NULL;
    }
};

int main()
{
    Double_LL Obj;

    Obj.First_Node(1);

    Obj.Insert_Right(1,2);
    Obj.Insert_Right(2,3);
    Obj.Insert_Right(3,4);
    Obj.Insert_Right(4,5);

    Obj.Insert_Left(5,6);
    Obj.Insert_Left(6,7);
    Obj.Insert_Left(7,8);
    Obj.Insert_Left(8,9);

    Obj.Remove(1);
    Obj.Remove(2);
    Obj.Remove(3);
    Obj.Remove(4);
    Obj.Remove(5);
    Obj.Remove(6);
    Obj.Remove(7);

    ///Obj.Print();

    getch();
    return 0;
}