Sunday 23 December 2012

Download Message Protection System for Android by Hitesh Toliya



Messege Protaction android

Developer Name :
hitesh toliya
Project Name :
Messege Protaction android
Project Type :
desktop Application
Project Language :
android
Project Description :
message protraction in android phone. first time set password is by default password. hide massage and protraction with password.
Upload Date :
2011-11-14
Source Code Link :
Download Here (0.4127MB)
read more...

Deleting in Binary Tree

/* Deleting in Binary Tree */
/* DELET_BT.C */

# include<stdio.h>
# include<malloc.h>

struct NODE
{
    int Info;
    struct NODE *Left_Child;
    struct NODE *Right_Child;
};

int depth;
void Output (struct NODE *, int );
struct NODE *Delet_Node (struct NODE *, int );
struct NODE *Create_Tree (int , struct NODE *);
struct NODE * DELE(struct NODE *, struct NODE *);

/* Output function */

void Output(struct NODE *T, int Level)
{
    int i;
    if (T)
    {
        Output(T->Right_Child, Level+1);
        printf("\n");
        for (i = 0; i < Level; i++)
            printf("  ");
        printf("%c", T->Info);
        Output(T->Left_Child, Level+1);
    }
}

/* Delete a node in the binary tree */

struct NODE * DELE(struct NODE *Node1, struct NODE *Node)
{
    struct NODE *DNode;
    if (Node1->Right_Child != NULL)
        Node1->Right_Child = DELE(Node1->Right_Child, Node);
    else
    {
        DNode = Node1;
        Node->Info = Node1->Info;
        Node1 = Node1->Left_Child;
        free(DNode);
    }
    return (Node1);
}

/* Deletion in binary tree */

struct NODE * Delet_Node (struct NODE *Node, int Info)
{
    struct NODE *Temp;

    if (Node == NULL)
    {
        printf("\n Information does not exist in the above tree");
        return (Node);
    }
    else
    {
        if (Info < Node->Info )
            Node->Left_Child = Delet_Node (Node->Left_Child, Info);
        else
            if (Info > Node->Info )

                Node->Left_Child = Delet_Node (Node->Right_Child, Info);
            else
            {
                Temp = Node;
                if (Temp->Right_Child == NULL)
                {
                    Node = Temp->Left_Child;
                    free(Temp);
                }
                else
                    if (Temp->Left_Child == NULL)
                    {
                        Node = Temp->Right_Child;
                        free(Temp);
                    }
                    else
                        Temp->Left_Child = DELE(Temp->Left_Child, Temp);
            }
    }
    return(Node);
}

/* Create binary tree */

struct NODE *  Create_Tree (int Info, struct NODE *Node)
{
    if (Node == NULL)
    {
        Node = (struct NODE *) malloc(sizeof(struct NODE));
        Node->Info = Info;
        Node->Left_Child = NULL;
        Node->Right_Child = NULL;
        return (Node);
    }

    /* Test for the left child */

    if (Info < Node->Info)

        Node->Left_Child = Create_Tree (Info, Node->Left_Child);

    else

        /* Test for the right child */

        if (Info >= Node->Info)

            Node->Right_Child = Create_Tree (Info, Node->Right_Child);
    return(Node);
}

/* Function main */

void main()
{
    int Number = 0;
    int Info ;
    char choice;
    int depth;
    struct NODE *T = (struct NODE *) malloc(sizeof(struct NODE));
    T = NULL;
    printf("\n Input choice 'b' to break:");
    choice = getchar();

    while(choice != 'b')
    {
        fflush(stdin);
        printf("\n Input information of the node: ");
        scanf("%c", &Info);
        T = Create_Tree(Info, T);
        Number++;
        fflush(stdin);
        printf("\n Input choice 'b' to break:");
        choice = getchar();
    }
    fflush(stdin);
    printf("\n Number of elements in the list is %d", Number);
    printf("\n Tree is \n");
    Output(T, 1);

    printf("\n Input the information to which want remove from the above tree: ");
    scanf("%c", &Info);

    T = Delet_Node(T, Info);
    printf("\n Tree after deletion of a node: ");
    Output(T, 1);
}
read more...

Create Binary TREE

/* Create Binary TREE */
/* CREAT_BT.C */

# include<stdio.h>
# include<malloc.h>

struct NODE
{
    int Info;
    struct NODE *Left_Child;
    struct NODE *Right_Child;
};

struct NODE *Binary_Tree (char *, int, int);
void Output(struct NODE *, int );

/* Function to insert an element into tree */

struct  NODE *  Binary_Tree (char *List, int Lower, int Upper)
{
    struct NODE *Node;
    int Mid = (Lower + Upper)/2;
    Node = (struct NODE *) malloc(sizeof(struct NODE));

    Node->Info = List [Mid];
    if ( Lower>= Upper)
    {
        Node->Left_Child = NULL;
        Node->Right_Child = NULL;
        return (Node);
    }

    if (Lower <= Mid - 1)
        Node->Left_Child = Binary_Tree (List, Lower, Mid - 1);
    else
        Node->Left_Child = NULL;
    if (Mid + 1 <= Upper)
        Node->Right_Child = Binary_Tree (List, Mid + 1, Upper);
    else
        Node->Right_Child = NULL;
    return(Node);
}

/* Output function */

void Output(struct NODE *T, int Level)
{
    int i;
    if (T)
    {
        Output(T->Right_Child, Level+1);
        printf("\n");
        for (i = 0; i < Level; i++)
            printf("   ");
        printf("%c", T->Info);
        Output(T->Left_Child, Level+1);
    }
}

/* Function main */

void main()
{
    char List[100];
    int Number = 0;
    char Info ;
    char choice;
    struct NODE *T = (struct NODE *) malloc(sizeof(struct NODE));
    T = NULL;
    printf("\n Input choice 'b' to break:");
    choice = getchar();
    while(choice != 'b')
    {
        printf("\n Input information of the node: ");
        scanf("%c", &Info);
        List[Number++] = Info;
        fflush(stdin);
        printf("\n Input choice 'b' to break:");
        choice = getchar();
    }
    Number --;
    printf("\n Number of elements in the lsit is %d", Number);
    T = Binary_Tree(List, 0, Number);
    Output(T,1);
}
read more...

C Example: Binary tree traversal

/*
 Data Structure Example in C for
 Binary tree traversal */
/* BT_TRAV.C */

# include<stdio.h>

struct NODE
{
    char Info;
    struct NODE *Left_Child;
    struct NODE *Right_Child;
};

struct NODE *Binary_Tree (char *, int, int);
void Output(struct NODE *, int );
void Pre_order(struct NODE *);
void In_order(struct NODE *);
void Post_order(struct NODE *);

/* Function to create an binary tree */

struct NODE *  Binary_Tree (char *List, int Lower, int Upper)
{
    struct NODE *Node;
    int Mid = (Lower + Upper)/2;
    Node = (struct NODE*) malloc(sizeof(struct NODE));

    Node->Info = List [Mid];
    if ( Lower>= Upper)
    {
        Node->Left_Child = NULL;
        Node->Right_Child = NULL;
        return (Node);
    }

    if (Lower <= Mid - 1)
        Node->Left_Child = Binary_Tree (List, Lower, Mid - 1);
    else
        Node->Left_Child = NULL;
    if (Mid + 1 <= Upper)
        Node->Right_Child = Binary_Tree (List, Mid + 1, Upper);
    else
        Node->Right_Child = NULL;
    return(Node);
}

/* Output function */

void Output(struct NODE *T, int Level)
{
    int i;
    if (T)
    {
        Output(T->Right_Child, Level+1);
        printf("\n");
        for (i = 0; i < Level; i++)
            printf("   ");
        printf(" %c", T->Info);
        Output(T->Left_Child, Level+1);
    }
}

/* Pre-order traversal */

void Pre_order (struct NODE *Node)
{
    if (Node)
    {
        printf(" %c", Node->Info);
        Pre_order(Node->Left_Child);
        Pre_order(Node->Right_Child);
    }
}

/* In-order traversal */

void In_order (struct NODE *Node)
{
    if (Node)
    {
        In_order(Node->Left_Child);
        printf(" %c", Node->Info);
        In_order(Node->Right_Child);
    }
}

/* Post-order traversal */

void Post_order (struct NODE *Node)
{
    if (Node)
    {
        Post_order(Node->Left_Child);
        Post_order(Node->Right_Child);
        printf(" %c", Node->Info);
    }
}

/*  Function main */

void main()
{
    char List[100];
    int Number = 0;
    char Info;
    char choice;
    struct NODE *T = (struct NODE *) malloc(sizeof(struct NODE));
    T = NULL;
    printf("\n Input choice 'b' to break:");
    choice = getchar();
    fflush(stdin);
    while(choice != 'b')
    {
        printf("\n Input information of the node: ");
        scanf("%c", &Info);
        List[Number++] = Info;
        fflush(stdin);
        printf("\n Input choice 'b' to break:");
        choice = getchar();
        fflush(stdin);
    }
    Number --;
    printf("\n Number of elements in the list is %d", Number);
    T = Binary_Tree(List, 0, Number);
    Output(T,1);
    printf("\n Pre-order traversal\n");
    Pre_order (T);
    printf("\n In-order traversal\n");
    In_order (T);
    printf("\n Post-order traversal\n");
    Post_order (T);
}
read more...

C Example : binary search tree (BST)

/* binary search tree */
/* BST.C */
#include <stdio.h>
#include <stdlib.h>

#define compLT(a,b) (a < b)
#define compEQ(a,b) (a == b)

/* implementation dependend declarations */
typedef enum {
    STATUS_OK,
    STATUS_MEM_EXHAUSTED,
    STATUS_DUPLICATE_KEY,
    STATUS_KEY_NOT_FOUND
} statusEnum;

typedef int keyType;            /* type of key */

/* user data stored in tree */
typedef struct {
    int stuff;                  /* optional related data */
} recType;

typedef struct nodeTag {
    struct nodeTag *left;       /* left child */
    struct nodeTag *right;      /* right child */
    struct nodeTag *parent;     /* parent */
    keyType key;                /* key used for searching */
    recType rec;                /* user data */
} nodeType;

nodeType *root = NULL;          /* root of binary tree */

statusEnum insert(keyType key, recType *rec) {
    nodeType *x, *current, *parent;

   /***********************************************
    *  allocate node for data and insert in tree  *
    ***********************************************/

    /* find future parent */
    current = root;
    parent = 0;
    while (current) {
        if (compEQ(key, current->key))
            return STATUS_DUPLICATE_KEY;
        parent = current;
        current = compLT(key, current->key) ?
            current->left : current->right;
    }

    /* setup new node */
    if ((x = malloc (sizeof(*x))) == 0) {
        return STATUS_MEM_EXHAUSTED;
    }
    x->parent = parent;
    x->left = NULL;
    x->right = NULL;
    x->key = key;
    x->rec = *rec;

    /* insert x in tree */
    if(parent)
        if(compLT(x->key, parent->key))
            parent->left = x;
        else
            parent->right = x;
    else
        root = x;

    return STATUS_OK;
}

statusEnum delete(keyType key) {
    nodeType *x, *y, *z;

   /***************************
    *  delete node from tree  *
    ***************************/

    /* find node in tree */
    z = root;
    while(z != NULL) {
        if(compEQ(key, z->key))
            break;
        else
            z = compLT(key, z->key) ? z->left : z->right;
    }
    if (!z) return STATUS_KEY_NOT_FOUND;

    /* find tree successor */
    if (z->left == NULL || z->right == NULL)
        y = z;
    else {
        y = z->right;
        while (y->left != NULL) y = y->left;
    }

    /* x is y's only child */
    if (y->left != NULL)
        x = y->left;
    else
        x = y->right;

    /* remove y from the parent chain */
    if (x) x->parent = y->parent;
    if (y->parent)
        if (y == y->parent->left)
            y->parent->left = x;
        else
            y->parent->right = x;
    else
        root = x;

    /* if z and y are not the same, replace z with y. */
    if (y != z) {
        y->left = z->left;
        if (y->left) y->left->parent = y;
        y->right = z->right;
        if (y->right) y->right->parent = y;
        y->parent = z->parent;
        if (z->parent)
            if (z == z->parent->left)
                z->parent->left = y;
            else
                z->parent->right = y;
        else
            root = y;
        free (z);
    } else {
        free (y);
    }
    return STATUS_OK;
}

statusEnum find(keyType key, recType *rec) {

   /*******************************
    *  find node containing data  *
    *******************************/

    nodeType *current = root;
    while(current != NULL) {
        if(compEQ(key, current->key)) {
            *rec = current->rec;
            return STATUS_OK;
        } else {
            current = compLT(key, current->key) ?
                current->left : current->right;
        }
    }
    return STATUS_KEY_NOT_FOUND;
}

int main(int argc, char **argv) {
    int i, maxnum, random;
    recType *rec;
    keyType *key;
    statusEnum status;

    /* command-line:
     *
     *   bin maxnum random
     *
     *   bin 5000        // 5000 sequential
     *   bin 2000 r      // 2000 random
     *
     */
    maxnum = atoi(argv[1]);
    random = argc > 2;

    if ((rec = malloc(maxnum * sizeof(recType))) == 0) {
        fprintf (stderr, "insufficient memory (rec)\n");
        exit(1);
    }
    if ((key = malloc(maxnum * sizeof(keyType))) == 0) {
        fprintf (stderr, "insufficient memory (key)\n");
        exit(1);
    }

    if (random) { /* random */
        /* fill "key" with unique random numbers */
        for (i = 0; i < maxnum; i++) key[i] = rand();
        printf ("ran bt, %d items\n", maxnum);
    } else {
        for (i=0; i<maxnum; i++) key[i] = i;
        printf ("seq bt, %d items\n", maxnum);
    }


    for (i = 0; i < maxnum; i++) {
        status = insert(key[i], &rec[i]);
        if (status) printf("pt1, i=%d: %d\n", i, status);
    }

    for (i = maxnum-1; i >= 0; i--) {
        status = find(key[i], &rec[i]);
        if (status) printf("pt2, i=%d: %d\n", i, status);
    }

    for (i = maxnum-1; i >= 0; i--) {
        status = delete(key[i]);
        if (status) printf("pt3, i=%d: %d\n", i, status);
    }
    return 0;
}
read more...

implement binary tree

// implement binary  tree

#include<stdio.h>
#include<conio.h>
#define N 10
#include<stdlib.h>
#include<process.h>
struct node
{
  struct node* lp;
  struct node* rp;
  int info;
};
struct stack
{
  int top;
  int st[N];
};
void main()
{
  int x,ch;
  char c;
  struct node* insert(struct node*,int);
  struct node* delet(struct node*,int);
  void retrieve(struct node*);
   void inorder(struct node*);

  struct node *root,*cur;
  root= '\0';
  clrscr();
  do
  {
    printf("\n1.insert\n 2.retrieve\n 3.delete\n 4.exit\n");
    printf("\nenter your choice\n");
    scanf("%d",&ch);
    switch(ch)
    {
      case 1:  printf("enter data\n");
           scanf("%d",&x);
           root=insert(root,x);
           break;

      case 2:  //retrieve(root);
          inorder(root);
           break;

      case 3:  printf("enter data u want to delete:\n");
           scanf("%d",&x);
           root=delet(root,x);
          // free(cur);
           break;

      case 4: exit(0);
    }
    printf("\ndo u want to continue(y/n)?");
    c=getche();
  }while(c=='y'||c=='Y');
  getch();
}

struct node* insert(struct node*root,int x)
{
  struct node* makenode(int);
  struct node *p,*q;
  if(root=='\0')
  {
    root=makenode(x);
    return(root);
  }
  p=q=root;
  while(q!='\0' && x!=p->info)
  {
    p=q;
    if(x<p->info)
     q=p->lp;
    else
     q=p->rp;
   }
   if(x==p->info)
     printf("duplication not allowed\n");
   else if(x<p->info)
   {
     p->lp=makenode(x);
   }
   else
   {
     p->rp=makenode(x);
    }
   return(root);
}

struct node* makenode(int x)
{
  struct node* nev;
  nev=(struct node*)malloc(sizeof(struct node));
  nev->info=x;
  nev->lp=nev->rp='\0';
  return(nev);
}


/* RETRIEVING IN POST-ORDER USING STACK. */
void retrieve(struct node *root)
{
   struct node *p,*x;
   int y;
   struct stack s;
   void push(struct stack *,int);
   int pop(struct stack *);
   if(root==NULL)
   {
     printf("Empty Tree");
   }
   else
   {
   p=root;
   s.top=-1;
   while(p!='\0' || s.top>-1)
     {
      while(p!=NULL)
      {
     push(&s,p->info);
     p=p->lp;
      }
      if(s.top>-1)
      {
     p=pop(&s);
     printf("/ \n");
     printf("%10d",p->info);
     p=p->rp;
      }
    }
  return;

}
void inorder(struct node *root)
{
   if(root==NULL)
     printf("Empty Tree");
   else
   {
     if(root->lp!=NULL)
    inorder(root->lp);
     printf("%d\n",root->info);
     if(root->rp!=NULL)
    inorder(root->rp);
   }
}

void push(struct stack *s1,int no)
{
 if(s1->top==N-1)
  {
   printf("Stack overflow\n");
   return;
  }
 else
  {
   s1->top++;
   s1->st[s1->top]=no;
   return;
  }
}

int pop(struct stack *s1)
{
  if(s1->top==-1)
   {
    printf("stack is empty\n");
    return(-1);
   }
  else
   {
    return(s1->st[s1->top--]);
   }
}

struct node* delet(struct node *root,int x)
{
   struct node *cur,*par,*suc,*pred,*q;
   char d;
   int found=0;
   if(root=='\0')
   {
     printf("empty tree\n");
     return(root);
   }
   else
   {
    cur=root;
    found=0;
    par=NULL;
   while(!found && cur!='\0')
   {
     if(cur->info==x)
     {
       found=1;
     }
     else if(x<cur->info)
     {
       par=cur;
       cur=par->lp;
       d='l';
     }
     else
     {
       par=cur;
       cur=par->rp;
       d='r';
     }
   }
   if(found==0)
   {
    printf("node not found\n");
    return(root);
   }
   else if(cur->lp=='\0')
     q=cur->rp;
   else if(cur->rp=='\0')
     q=cur->lp;
   else
   {
     suc=cur->rp;
     if(suc->lp=='\0')
     {
       q=suc;
       q->lp=cur->lp;
     }
     else
     {
       pred=cur->rp;
       suc=pred->lp;
       while(suc->lp!='\0')
       {
     pred=suc;
     suc=pred->lp;
       }
       pred->lp=suc->rp;
       suc->lp=cur->lp;
       suc->rp=cur->rp;
       q=suc;
       }
     }
   }
     if(par==NULL)
     {
    root=q;
    return(root);
     }
     if(d=='l')
      par->lp=q;
     else
      par->rp=q;
     printf("deleted data is=%d",cur->info);
     free(cur);
     return(root);
}


read more...

Create AVL Binary TREE Example in C

/* Create AVL Binary TREE */
/* AVL_TREE.C */

# include<stdio.h>
# include<malloc.h>

# define F 0
# define T 1

struct NODE
{
    char Info;
    int Flag;
    struct  NODE *Left_Child;
    struct  NODE *Right_Child;
};

struct NODE *Binary_Tree (char , struct NODE *, int *);
void Output(struct NODE *, int );
struct  NODE *Balance_Right_Heavy(struct NODE *, int *);
struct NODE *Balance_Left_Heavy(struct NODE *, int *);
struct NODE *DELETE(struct NODE *, struct NODE *, int *);
struct NODE *Delete_Element(struct NODE *, char , int *);

/* Function to insert an element into tree */

struct NODE *  Binary_Tree (char Info, struct NODE *Parent, int *H)
{
    struct NODE *Node1;
    struct NODE *Node2;
    if(!Parent)
    {
        Parent = (struct NODE *) malloc(sizeof(struct NODE));
        Parent->Info = Info;
        Parent->Left_Child = NULL;
        Parent->Right_Child = NULL;
        Parent->Flag = 0;
        *H = T;
        return (Parent);
    }

    if(Info < Parent->Info)
    {
        Parent->Left_Child = Binary_Tree(Info, Parent->Left_Child, H);
        if(*H)
        /* Left branch has grown higher */
        {
            switch(Parent->Flag)
            {
            case 1: /* Right heavy */
                Parent->Flag = 0;
                *H = F;
                break;
            case 0: /* Balanced tree */
                Parent->Flag = -1;
                break;
            case -1: /* Left heavy */
                Node1 = Parent->Left_Child;
                if(Node1->Flag == -1)
                {
                    printf("\n Left to Left Rotation\n");
                    Parent->Left_Child= Node1->Right_Child;
                    Node1->Right_Child = Parent;
                    Parent->Flag = 0;
                    Parent = Node1;
                }
                else
                {
                    printf("\n Left to right rotation\n");
                    Node2 = Node1->Right_Child;
                    Node1->Right_Child = Node2->Left_Child;
                    Node2->Left_Child = Node1;
                    Parent->Left_Child = Node2->Right_Child;
                    Node2->Right_Child = Parent;
                    if(Node2->Flag == -1)
                        Parent->Flag = 1;
                    else
                        Parent->Flag = 0;
                    if(Node2->Flag == 1)
                        Node1->Flag = -1;
                    else
                        Node1->Flag = 0;
                    Parent = Node2;
                }

                Parent->Flag = 0;
                *H = F;
            }
        }
    }

    if(Info > Parent->Info)
    {
        Parent->Right_Child = Binary_Tree(Info, Parent->Right_Child, H);
        if(*H)
        /* Right branch has grown higher */
        {
            switch(Parent->Flag)
            {
            case -1: /* Left heavy */
                Parent->Flag = 0;
                *H = F;
                break;
            case 0: /* Balanced tree */
                Parent->Flag = 1;
                break;

            case 1: /* Right heavy */
                Node1 = Parent->Right_Child;
                if(Node1->Flag == 1)
                {
                    printf("\n Right to Right Rotation\n");
                    Parent->Right_Child= Node1->Left_Child;
                    Node1->Left_Child = Parent;
                    Parent->Flag = 0;
                    Parent = Node1;
                }
                else
                {
                    printf("\n Right to Left Rotation\n");
                    Node2 = Node1->Left_Child;
                    Node1->Left_Child = Node2->Right_Child;
                    Node2->Right_Child = Node1;
                    Parent->Right_Child = Node2->Left_Child;
                    Node2->Left_Child = Parent;

                    if(Node2->Flag == 1)
                        Parent->Flag = -1;
                    else
                        Parent->Flag = 0;
                    if(Node2->Flag == -1)
                        Node1->Flag = 1;
                    else
                        Node1->Flag = 0;
                    Parent = Node2;
                }

                Parent->Flag = 0;
                *H = F;
            }
        }
    }
    return(Parent);
}

/* Output function */

void Output(struct NODE *Tree,int Level)
{
    int i;
    if (Tree)
    {
        Output(Tree->Right_Child, Level+1);
        printf("\n");
        for (i = 0; i < Level; i++)
            printf("   ");
        printf("%c", Tree->Info);
        Output(Tree->Left_Child, Level+1);
    }
}

/* Balancing Right Heavy */

struct NODE * Balance_Right_Heavy(struct NODE *Parent, int *H)
{
    struct NODE *Node1, *Node2;

    switch(Parent->Flag)
    {
    case -1:
        Parent->Flag = 0;
        break;

    case 0:
        Parent->Flag = 1;
        *H= F;
        break;

    case 1: /* Rebalance */
        Node1 = Parent->Right_Child;
        if(Node1->Flag >= 0)
        {
            printf("\n Right to Right Rotation\n");
            Parent->Right_Child= Node1->Left_Child;
            Node1->Left_Child = Parent;
            if(Node1->Flag == 0)
            {
                Parent->Flag = 1;
                Node1->Flag = -1;
                *H = F;
            }
            else
            {
                Parent->Flag = Node1->Flag = 0;
            }
            Parent = Node1;
        }
        else
        {
            printf("\n Right to Left Rotation\n");
            Node2 = Node1->Left_Child;
            Node1->Left_Child = Node2->Right_Child;
            Node2->Right_Child = Node1;
            Parent->Right_Child = Node2->Left_Child;
            Node2->Left_Child = Parent;

            if(Node2->Flag == 1)
                Parent->Flag = -1;
            else
                Parent->Flag = 0;
            if(Node2->Flag == -1)
                Node1->Flag = 1;
            else
                Node1->Flag = 0;
            Parent = Node2;
            Node2->Flag = 0;
        }
    }
    return(Parent);
}

/* Balancing Left Heavy */

struct NODE * Balance_Left_Heavy(struct NODE *Parent, int *H)
{
    struct NODE *Node1, *Node2;

    switch(Parent->Flag)
    {
    case 1:
        Parent->Flag = 0;
        break;

    case 0:
        Parent->Flag = -1;
        *H= F;
        break;

    case -1: /*  Rebalance */
        Node1 = Parent->Left_Child;
        if(Node1->Flag <= 0)
        {
            printf("\n Left to Left Rotation\n");
            Parent->Left_Child= Node1->Right_Child;
            Node1->Right_Child = Parent;
            if(Node1->Flag == 0)
            {
                Parent->Flag = -1;
                Node1->Flag = 1;
                *H = F;
            }
            else
            {
                Parent->Flag = Node1->Flag = 0;
            }
            Parent = Node1;
        }
        else
        {
            printf("\n Left to Right Rotation\n");
            Node2 = Node1->Right_Child;
            Node1->Right_Child = Node2->Left_Child;
            Node2->Left_Child = Node1;
            Parent->Left_Child = Node2->Right_Child;
            Node2->Right_Child = Parent;

            if(Node2->Flag == -1)
                Parent->Flag = 1;
            else
                Parent->Flag = 0;

            if(Node2->Flag == 1)
                Node1->Flag = -1;
            else
                Node1->Flag = 0;
            Parent = Node2;
            Node2->Flag = 0;
        }
    }
    return(Parent);
}

/* Replace the node at which key is found with last right key of a left child */

struct NODE * DELETE(struct NODE *R, struct NODE *Temp, int *H)
{
    struct NODE *Dnode = R;
    if( R->Right_Child != NULL)
    {
        R->Right_Child = DELETE(R->Right_Child, Temp, H);
        if(*H)
            R = Balance_Left_Heavy(R, H);
    }
    else
    {
        Dnode = R;
        Temp->Info = R->Info;
        R = R->Left_Child;
        free(Dnode);
        *H = T;
    }
    return(R);
}
/* Delete the key element from the tree */

struct NODE * Delete_Element(struct NODE *Parent, char Info, int *H)
{
    struct NODE *Temp;
    if(!Parent)
    {
        printf("\n Information does not exist");
        return(Parent);
    }
    else
    {
        if (Info < Parent->Info )
        {
            Parent->Left_Child = Delete_Element(Parent->Left_Child, Info, H);
            if(*H)
                Parent = Balance_Right_Heavy(Parent, H);
        }
        else
            if(Info > Parent->Info)
            {
                Parent->Right_Child = Delete_Element(Parent->Right_Child, Info, H);
                if(*H)
                    Parent = Balance_Left_Heavy(Parent, H);
            }
            else
            {
                Temp= Parent;
                if(Temp->Right_Child == NULL)
                {
                    Parent = Temp->Left_Child;
                    *H = T;
                    free(Temp);
                }
                else
                    if(Temp->Left_Child == NULL)
                    {
                        Parent = Temp->Right_Child;
                        *H = T;
                        free(Temp);
                    }
                    else
                    {
                        Temp->Left_Child = DELETE(Temp->Left_Child, Temp, H);
                        if(*H)
                            Parent = Balance_Right_Heavy(Parent, H);
                    }
            }
    }
    return(Parent);
}

/* Function main */

void main()
{
    int H;
    char Info ;
    char choice;
    struct NODE *Tree = (struct NODE *)malloc(sizeof(struct NODE));
    Tree =  NULL;
    printf("\n Input choice 'b' to break:");
    choice = getchar();
    while(choice != 'b')
    {
        fflush(stdin);
        printf("\n Input information of the node: ");
        scanf("%c", &Info);
        Tree = Binary_Tree(Info, Tree, &H);
        printf("\n Tree is:\n");
        Output(Tree, 1);
        fflush(stdin);
        printf("\n Input choice 'b' to break:");
        choice = getchar();
    }
    fflush(stdin);
    while(1)
    {
        printf("\n Input choice 'b' to break:");
        printf("\n Input the key value want to deletedir:");
        scanf("%c", &Info);
        if (Info == 'b')
            break;
        Tree = Delete_Element(Tree, Info, &H);
        printf("\n Tree is:\n");
        Output(Tree, 1);
    }
}
read more...

MERGE SORT

/* MERGE SORT */
/* merge.c */

# include<stdio.h>
# include<stdlib.h>

void merge_sort(float *, int , int , int );
void merge_pass(float *, int , int );

/* Definition of the function */

void merge_sort(float l[], int top, int size, int bottom)
{
    float temp[1000];
    int f = top;
    int s = size +1 ;
    int t = top ;
    int upper;
    while(( f <= size)&&(s <=bottom))
    { 
        if(l[f] <= l[s])
        {
            temp[t] = l[f] ;
            f ++;
        }

        else
        {
            temp[t] = l[s] ;
            s ++;
        }

        t ++;
    }

    if(f <=size)
    {
        for(f = f ; f<=size; f++)
        {
            temp[t] = l[f];
            t++;
        }
    }

    else
    {
        for(s = s ; s<= bottom ; s++)
        {
            temp[t]=l[s];
            t++;
        }

    }

    for(upper = top; upper <= bottom ; upper++)
    {
        l[upper]=temp[upper];
    }

}

/* Definition of the function */

void merge_pass( float append[], int m, int n )
{
    if( m!= n)
    {
        int mid = (m+n)/2;
        merge_pass( append, m, mid );

        merge_pass( append, mid+1, n );
        merge_sort( append, m, mid, n );
    }
}

/* main function */

void main()
{
    float list[1000];
    int i, n = 30;
    printf("\n Size of the list: %d", n);

    for(i = 0 ; i < n ; i++)
    {
        list[i] = (float) (rand() %100);
    }

    printf("\n Entered list as follows:\n");
    for( i = 0 ; i<n ; i++)
        printf(" %d ", (int) list[i]);

    i = 0 ;

    merge_pass(list, i, n-1);

    printf("\n Merge sorted list is as follows:\n");
    for( i = 0 ; i<n ; i++)
        printf(" %d", (int) list[i]);
}
read more...

HEAP SORT

/* HEAP SORT */
/* HEAP.C */

# include<stdio.h>

void  heap_sort(int *, int );
void create_heap(int *, int);
void display(int *, int);

/*     Definition of the function */

void create_heap(int list[], int n )
{

    int k, j, i, temp;

    for(k = 2 ; k <= n;  ++k)
    {
        i = k ;
        temp = list[k];
        j = i / 2 ;

        while((i > 1) && (temp > list[j]))
        {
            list[i] = list[j];
            i = j ;
            j = i / 2 ;
            if ( j < 1 )
                j = 1 ;
        }

        list[i] = temp ;
    }
}

/* End of heap creation function */

/* Definition of the function */

void heap_sort(int list[], int n)
{
    int k, temp, value, j, i, p;
    int step = 1;
    for(k = n ; k >= 2; --k)
    {
        temp = list[1] ;
        list[1] = list[k];
        list[k] = temp ;

        i = 1 ;
        value = list[1];
        j = 2 ;

        if((j+1) < k)
            if(list[j+1] > list[j])
                j ++;
        while((j <= ( k-1)) && (list[j] > value))
        {
            list[i] = list[j];
            i = j ;
            j = 2*i ;
            if((j+1) < k)
                if(list[j+1] > list[j])
                    j++;
                else
                    if( j > n)
                        j = n ;
            list[i] = value;
        } /* end of while statement */
       
        printf("\n Step = %d ", step);
        step++;   
        for(p = 1; p <= n; p++)
            printf(" %d", list[p]);
    } /* end for loop */
}

/* Display function */

void display(int list[], int n)
{
    int i;
    for(i = 1 ; i <= n; ++ i)
    {
        printf("  %d", list[i]);
    }
}

/* Function main */

void main()
{
    int list[100];
    int i, size = 13 ;

    printf("\n Size of the list: %d", size);

    for(i = 1 ; i <= size ; ++i)
    {
        list[i] = rand() % 100;
    }
    printf("\n Entered list is as follows:\n");
    display(list, size);
    create_heap(list, size);
    printf("\n Heap\n");
    display(list, size);
    printf("\n\n");

    heap_sort(list,size);

    printf("\n\n Sorted list is as follows :\n\n");
    display(list,size);

}

read more...

bubble sort in C

/* bubble.c */

#include <stdio.h>
#include <stdlib.h>

void bubble_sort(int array[], int size)
{
    int temp, i, j;

    for (i = 0; i < size; i++)
        for (j = 0; j < size; j++)
            if (array[i] < array[j])
            {
                temp = array[i];
                array[i] = array[j];
                array[j] = temp;
            }
}

void main(void)
{
    int values[30], i;
    printf("\n Unsorted list is as follows\n");

    for (i = 0; i < 10; i++)
    {
        values[i] = rand() % 100;
        printf(" %d", rand()%100);
    }
    bubble_sort(values, 10);
    printf("\n Sorted list is as follows\n");

    for (i = 0; i < 10; i++)
        printf("%d ", values[i]);
}
read more...

Popular Posts