SAD LAB PRACTICALS (1).docx

Embed Size (px)

Citation preview

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    1/26

    COMPUTER ENGINEERING

    M. Tech.I SEMESTER

    SYSTEM SOFTWARE LAB

    List of Experiments:

    Q1. Write a program in C/C++/Java to develop AVL Tree for 10 elements.

    /* Adelson Velseky Landis Tree */

    # include using namespace std;

    template

    class avlNode{public:

    int element;avlNode *left;avlNode *right;int height;avlNode(key &,avlNode *, avlNode*, int =0);

    };

    //typedef class avlNode *avlNodeptr;template

    class avlTree{public:

    avlTree();avlTree(avlTree &);~avlTree();bool empty();int height();void preorder();void inorder();void postorder();void clear();void insert(key &);avlTree & operator =(avlTree &);

    private:avlTree *root;void recursive_preorder(avlNode *);//bstree::/recursive_void recursive_postorder(avlNode *);void recursive_inorder(avlNode *);

    void rotateWithLeftChild(avlNode *&);void rotateWithRightChild(avlNode *&);void doudleWithLeftChild(avlNode *&);void doubleWithRightChild(avlNode *&);

    };

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    2/26

    // Make a tree empty

    bool recursive_empty( ){

    avlNode d;if (p != NULL){

    makeempty(p->left);makeempty(p->right);d=p;free(d);p=NULL;

    }}

    int recursive_height( )

    { int t;if (p == NULL)

    return -1;else{

    t = p->height;return t;

    }}// preorder

    void recursive_preorder( ){

    if (p!=NULL){

    coutright);

    }}

    // Inorder Printingvoid recursive_inorder( ){

    if (p!=NULL){

    inorder(p->left);coutleft);

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    3/26

    postorder(p->right);coutelement)

    del(x,p->right);else if ((p->left == NULL) && (p->right == NULL))

    { d=p;free(d);p=NULL;coutright;coutleft;free(d);coutright);}

    // Inserting a nodevoid recursive_insert(key &){

    if (p == NULL){

    p = new node;p->element = x;p->left=NULL;p->right = NULL;p->height=0;if (p==NULL)

    cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    4/26

    insert(x,p->left);if ((bsheight(p->left) - bsheight(p->right))==2){

    if (x < p->left->element)p=srl(p);

    else

    p = drl(p);}}else if (x>p->element){

    insert(x,p->right);if ((bsheight(p->right) - bsheight(p->left))==2){if (x > p->right->element)

    p=srr(p);else

    p = drr(p);}

    }elsecoutright);d=max(m,n);p->height = d + 1;

    }

    int main(){

    nodeptr root,root1,min,max;//,flag;int a,choice,findele,delele,leftele,rightele,flag;char ch='y';bstree bst;

    root = NULL;root1=NULL;cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    5/26

    cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    6/26

    class tree

    {

    struct tree_object

    {

    tree_object *left;

    tree_object *right;

    tree_object *parent;

    int value;

    bool red;

    };

    tree_object* root;

    int data_size;

    int choose;

    int *array;

    public :

    tree( int data_size,int choose )

    {

    this->data_size=data_size;

    this->choose=choose;array=newint [data_size];

    root=NULL;

    }

    bool tree_empty() const { return root==NULL;}

    void QS_sort(int,int);

    void random_data();

    void start();

    voidTree_insert(int);

    void inorder(tree_object*);

    void drukuj_strukture_drzewa(tree_object*);

    void RB_insert(tree_object*);

    void left_rotation(tree_object*);void right_rotation(tree_object*);

    };

    void tree:: inorder(tree_object* pointer)

    {

    if(pointer!= NULL)

    {

    inorder(pointer->left);

    cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    7/26

    void tree:: RB_insert(tree_object * new_node)

    {

    tree_object * y;

    while( new_node != root && new_node->parent ->red == true)

    {

    if( new_node->parent == new_node->parent ->parent ->left )

    {

    y= new_node->parent ->parent ->right;

    if( y->red )

    {

    new_node ->parent ->red=false;

    y->red=false;

    new_node->parent->parent ->red = true;

    new_node= new_node->parent ->parent ;

    }

    else

    {

    if( new_node== new_node->parent ->right ){

    new_node=new_node->parent ;

    left_rotation(new_node);

    }

    new_node->parent -> red = false;

    new_node->parent -> parent -> red = true;

    right_rotation( new_node->parent ->parent );

    }

    }

    else

    {

    y= new_node->parent ->parent ->left;if( y->red ) **Segmentation fault,higher was all ok,here not**

    {

    new_node->parent ->red=false;

    y->red=false;

    new_node->parent ->parent ->red = true;

    new_node=new_node->parent->parent ;

    }

    else

    {

    if( new_node ==new_node->parent ->left ){

    new_node=new_node->parent ;

    right_rotation(new_node);

    }

    new_node->parent -> red= false;

    new_node->parent -> parent ->red=true;

    left_rotation( new_node->parent ->parent );

    }

    }

    }

    root->red=false;

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    8/26

    }

    void tree:: left_rotation(tree_object* x)

    {

    tree_object* y=x->right;

    x->right=y->left;

    if( y->left !=NULL)

    {

    y->left->parent =x;

    }

    y->parent = x->parent ;

    if( x->parent ==NULL)

    {

    root = y;

    }

    elseif (x == x->parent ->left)

    {

    x->parent ->left = y;

    }

    else{

    x->parent ->right = y;

    }

    y->left = x;

    x->parent = y;

    }

    void tree:: right_rotation(tree_object* y)

    {

    tree_object* x = y->left;

    y->left = x->right;

    if( x->right!= NULL)

    {x->right->parent = y ;

    }

    x->parent = y->parent;

    if( y->parent == NULL)

    {

    root = x;

    }

    elseif (y == y->parent ->right)

    {

    y->parent ->right = x;

    }

    else{

    y->parent ->left= x;

    }

    x->right= y;

    y->parent = x;

    }

    void tree::Tree_insert(int valueee)

    {

    tree_object *y=NULL;

    tree_object *new_node=new tree_object;

    new_node->value=valueee;if( tree_empty() )

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    9/26

    {

    root=new_node;

    }

    else

    {

    tree_object* x=root;

    while( x !=NULL )

    {

    y=x;

    if( new_node->value < x->value )

    {

    x=x->left;

    }

    elseif( new_node->value > x->value)

    {

    x=x->right;

    }

    }

    new_node->parent=y;if( new_node->value < y->value)

    {

    y->left =new_node;

    }

    else

    {

    y->right=new_node;

    }

    }

    new_node->left=NULL;

    new_node->right=NULL;

    new_node->red=true;RB_insert(new_node);

    }

    void tree::random_data()

    {

    if( choose ==1) // here I draw not repeated numbers

    {

    int data_size_local=0;

    int temp,error;

    for (int i=0; i

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    10/26

    this->array[ i ]=temp;

    }

    cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    11/26

    i++;

    j--;

    }

    }

    if (left < j)

    {

    QS_sort(left, j);

    }

    if (i < right)

    {

    QS_sort(i, right);

    }

    }

    int main()

    {

    int data_size,choose;coutdata_size;

    cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    12/26

    // keeping track of parent and whether node has// been visited or not.int* visited =newint[nV];int* parent =newint[nV*2];// This is a horrible hack of making a 2D arrayint* path =newint[nV*2];for(int i=0; i

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    13/26

    * Fulkerson algorithm.

    */int maxFlow(Graph *g){

    ofstream output("output.txt", ios::out);if(!output){

    coutnV;

    // Get an upper bound on the flow by summing the// capacity coming out of the source nodenodeEdge *src = rG->nodes[s];while(src!=NULL){

    int tmp = src->weight;mC=max(tmp,mC);C+=tmp;src = src->next;

    }

    //cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    14/26

    rG->updateEdgeWeight(c, p, b);

    c = p;p = augPath[c];

    }// Destroy my last bottleneck graph and make a new onedelete bG;bG = rG->bottleneckGraph(delta);

    augPath = BFS(bG, s, t, false);}// I want to see my new graph for each augmentation//rG->toString();

    rC =0;src = rG->nodes[s];while(src!=NULL){

    rC+=src->weight;src = src->next;

    }output

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    15/26

    if(myfile.is_open()){int edgeCount, counter =1;Graph* g =NULL;

    cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    16/26

    //cout

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    17/26

    continue;

    }

    if(st[i]>tq)

    st[i]=st[i]-tq;

    else

    if(st[i]>=0)

    {

    temp=st[i];

    st[i]=0;

    }

    sq=sq+temp;

    tat[i]=sq;

    }

    if(n==count)

    break;

    }

    for(i=0;i

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    18/26

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    19/26

    if(p[i]

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    20/26

    for(j=1;j

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    21/26

    float att=0,awt=0;

    for(i=0;i

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    22/26

    scanf("%d",&xa);

    printf("Enter a No \"xb\" which is lessthan value of q:");

    scanf("%d",&xb);

    for(x=0;x

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    23/26

    #include

    int ex_gcd(int a,int b,int n) //computes the GCD using the Extended Euclid method

    {

    int x=0,y=1,lastx=1,lasty=0;

    int temp,q;

    while(b!=0)

    {

    temp =b;

    q = a/b;

    b = a%b;

    a = temp;

    temp=x;

    x = lastx - q*x;

    lastx = temp;

    temp =y;

    y = lasty - q*y;

    lasty = temp;

    }

    if(n==1) return a;

    else return lasty;

    }

    long en_de(int base, int exp,int n)

    {

    int b[30],i,c=0;

    long d=1;

    for(i=0;exp!=0;exp/=2,i++)

    b[i]=exp%2;

    i--;

    for(;i>=0;i--)

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    24/26

    {

    c = 2*c;

    d = (d*d) %n;

    if(b[i]==1)

    {

    c = c+1;

    d = (d*base)%n;

    }

    }

    return d;

    }

    void main()

    {

    int p,q,i;

    int pt,ct;

    int e,d,et,n,temp;

    printf("Enter prime No.s p,q :");

    scanf("%d %d",&p,&q);

    n = p*q;

    et=(p-1)*(q-1);

    for(i=2;i

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    25/26

    printf("\nPublic Key KU = {%d,%d}\n",e,n);

    printf("Private Key KR = {%d,%d}\n",d,n);

    printf("Enter Plain text M Integer (0

  • 7/27/2019 SAD LAB PRACTICALS (1).docx

    26/26

    Q12. Draw collaboration Diagram of UML for Payroll Management System.

    Q13. Draw a Use-Case Diagram of UML for Safe Home Security System.

    Q14. Draw Collaboration Diagram of UML for Inventory Management System.

    NOTE: ATTEMPT ANY TEN QUESTIONS IN THE FILE.