struct ArrayList
{
int *arr; // Array Pointer
int length; // No of elements of an Array.
int capacity; // size of an Array.
};
struct ArrayList* create(int size)
{
struct ArrayList *array = (struct ArrayList*)malloc(sizeof(struct ArrayList));
array->capacity = size;
array->length = 0;
array->arr = (int *)malloc(array->length * sizeof(int));
return array;
}
void addFirst(struct ArrayList *array, int data)
{
if(array->capacity == array->length)
{
printf("ArrayList overflow.\n");
}
else
{
for(int i = array->length-1; i >= 0; i--)
{
array->arr[i + 1] = array->arr[i];
}
array->arr[0] = data;
array->length++;
}
}
void addLast(struct ArrayList * array, int data)
{
if(array->capacity == array->length)
{
printf("ArrayList Overflow.\n");
}
else
{
array->arr[array->length] = data;
array->length++;
}
}
void display(struct ArrayList *array)
{
if(array->length == 0)
{
printf("ArrayList is underflow.\n");
}
else
{
for(int i = 0; i < array->length; i++)
{
printf("%d ",array->arr[i]);
}
printf("\n");
}
}
int length(struct ArrayList *array)
{
if(array->length == 0)
{
return 0;
}
else
{
return array->length;
}
}
int getFirst(struct ArrayList *array)
{
if(array->length == 0)
{
printf("ArrayList underflow.\n");
return -1;
}
else
{
return array->arr[0];
}
}
int getLast(struct ArrayList *array)
{
if(array->length == 0)
{
printf("ArrayList underflow\n");
return -1;
}
else
{
return array->arr[array->length-1];
}
}
void insertElement(struct ArrayList *array , int idx , int data)
{
if(array->length == array->capacity)
{
printf("ArrayList overflow.\n");
}
else if(idx < 0 || idx > array->length)
{
printf("Invalid Arguments.\n");
}
else
{
for(int i = array->length-1; i >= idx; i--)
{
array->arr[i + 1] = array->arr[i];
}
array->arr[idx] = data;
array->length++;
}
}
int deleteElement(struct ArrayList *array , int idx)
{
if(array->length == 0)
{
printf("ArrayList underflow.\n");
return -1;
}
else if(idx < 0 || idx >= array->length)
{
printf("Invalid Argument.\n");
return -1;
}
else
{
int data = array->arr[idx];
for(int i = idx; i < array->length -1; i++)
{
array->arr[i] = array->arr[i + 1];
}
array->length--;
return data;
}
}
int deleteFirst(struct ArrayList *array)
{
if(array->length == 0)
{
printf("ArrayList underflow.\n");
return -1;
}
else
{
int data = array->arr[0];
for(int i = 0; i < array->length - 1; i++)
{
array->arr[i] = array->arr[i + 1];
}
array->length--;
return data;
}
}
int deleteLast(struct ArrayList *array)
{
if(array->length == 0)
{
printf("ArrayList underflow.\n");
return -1;
}
else
{
array->length--;
return array->arr[array->length + 1];
}
}
void update(struct ArrayList *array , int idx , int data)
{
if(array->length == 0)
{
printf("ArrayList underflow.\n");
}
else if(idx < 0 || idx >= array->length)
{
printf("Invalid argument.\n");
}
else
{
array->arr[idx] = data;
}
}
int main()
{
struct ArrayList *array = NULL;
array = create(6);
addLast(array , 2);
addLast(array , 3);
addLast(array , 4);
display(array);
}
struct Node
{
int data;
struct Node *next;
} *head = NULL , *tail = NULL;
void create(int arr[] , int n)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = arr[0];
temp->next = NULL;
head = tail = temp;
for(int i = 1; i < n; i++)
{
struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
temp->data = arr[i];
temp->next = NULL;
tail->next = temp;
tail = temp;
}
}
void display(struct Node *head)
{
struct Node *temp = head;
while(temp != NULL)
{
printf("%d ",temp->data);
temp = temp->next;
}
printf("\n");
}
int lenght(struct Node *head)
{
int size = 0;
struct Node *temp = head;
while(temp != NULL)
{
size++;
temp = temp->next;
}
return size;
}
void insert(int idx , int data)
{
struct Node *temp = (struct Node*) malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(idx < 0 || idx > lenght(head))
{
printf("Invalid Arguments.\n");
return;
}
if(idx == 0)
{
temp->next = head;
head = temp;
if(tail == NULL)
{
tail = temp;
}
}
else
{
struct Node *tHead = head;
for(int i = 0; i < idx -1; i++)
{
tHead = tHead->next;
}
if(tHead == tail)
{
tail->next = temp;
tail = temp;
}
else
{
temp->next = tHead->next;
tHead->next = temp;
}
}
}
void addFirst(int data)
{
struct Node *temp = (struct Node *) malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = tail = temp;
}
else
{
temp->next = head;
head = temp;
}
}
void addLast(int data)
{
struct Node *temp = (struct Node*)malloc(sizeof(struct Node));
temp->data = data;
temp->next = NULL;
if(head == NULL)
{
head = tail = temp;
}
else
{
tail->next = temp;
tail = temp;
}
}
int find(int data)
{
struct Node *tHead = head;
while(tHead != NULL)
{
if(tHead->data == data)
{
return tHead;
}
tHead = tHead->next;
}
return NULL;
}
int sumNode()
{
if(head == NULL)
{
return 0;
}
else
{
struct Node *tHead = head;
int sum = 0;
while(tHead != NULL)
{
sum += tHead->data;
tHead = tHead->next;
}
return sum;
}
}
int max()
{
if(head == NULL)
{
return NULL;
}
int res = head->data;
struct Node *tHead = head->next;
while(tHead != NULL)
{
if(tHead->data > res)
{
res = tHead->data;
}
tHead = tHead->next;
}
return res;
}
#void moveToHead(int data)
{
if(head == NULL)
{
return;
}
if(data == head->data)
{
return;
}
else
{
struct Node *tHead , *pre;
tHead = head;
while(tHead != NULL)
{
if(tHead->data == data)
{
struct Node *temp = pre;
pre->next = head;
pre = tHead->next; null
tHead->next = head->next;
head->next = pre;
head = tHead;
break;
}
pre = tHead;
tHead = tHead->next;
}
}
}
....
....
....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
struct Stack
{
int top;
int size;
int *arr;
};
void create(struct Stack *st , int size)
{
st->size = size;
st->top = -1;
st->arr = (int *)malloc(st->size * sizeof(int));
}
void push(struct Stack *st , int data)
{
if(st->top == st->size -1)
{
printf("Stack overflow.\n");
}
else
{
st->top++;
st->arr[st->top] = data;
}
}
int pop(struct Stack *st)
{
if(st->top == -1)
{
printf("Stack underflow.\n");
}
else
{
int data = st->arr[st->top];
st->top--;
return data;
}
}
int peek(struct Stack st)
{
if(st.top == -1)
{
printf("Stack underflow.\n");
}
else
{
return (st.arr[st.top]);
}
}
void display(struct Stack st)
{
for(int i = st.top; i >= 0; i--)
{
printf("%d ",st.arr[i]);
}
printf("\n");
}
int size(struct Stack st)
{
return (st.top + 1);
}
int main()
{
struct Stack st;
create(&st , 5);
push(&st , 1);
push(&st , 2);
push(&st , 3);
push(&st , 4);
push(&st , 5);
display(st);
int res = peek(st);
printf("%d \n",res);
res = size(st);
printf("%d \n",res);
display(st);
}
#include <stdio.h>
#include <stdlib.h>
struct Stack{
int top;
int size;
int *arr;
};
void create(struct Stack *st , int size)
{
st->size = size;
st->top = -1;
st->arr = (int *)malloc(st->size * sizeof(int));
}
void push(struct Stack *st , int data)
{
if(st->top == st->size -1)
{
printf("Stack overflow.\n");
}
else
{
st->top++;
st->arr[st->top] = data;
}
}
int pop(struct Stack *st)
{
if(st->top == -1)
{
printf("Stack underflow.\n");
}
else
{
int data = st->arr[st->top];
st->top--;
return data;
}
}
int size(struct Stack st)
{
return (st.top + 1);
}
int peek(struct Stack st)
{
if(st.top == -1)
{
printf("Stack underflow.\n");
}
else
{
return (st.arr[st.top]);
}
}
void display(struct Stack st)
{
for(int i = st.top; i >= 0; i--)
{
printf("%d ",st.arr[i]);
}
printf("\n");
}
int main()
{
struct Stack st;
create(&st , 5);
push(&st , 1);
push(&st , 2);
push(&st , 3);
push(&st , 4);
push(&st , 5);
display(st);
int res = peek(st);
printf("%d \n",res);
res = size(st);
printf("%d \n",res);
display(st);
}
#Loading....
struct Queue
{
int data , size , front , rear;
int *Q;
};
// Create Queue
void createQueue(struct Queue *q , int size)
{
// q = (struct Queue *)malloc(sizeof(struct Queue));
q->size = size;
q->front = q->rear = -1;
q->Q = (int *)malloc(q->size * sizeof(int));
}
// enqueue code.
void enqueue(struct Queue *q , int key)
{
if(q->rear == q->size -1)
{
printf("Queue overflow.\n");
}
else
{
q->Q[++q->rear] = key;
}
}
// dequeue code.
int dequeue(struct Queue *q)
{
if(q->rear == q->front)
{
printf("Queue underflow.\n");
return -1;
}
else
{
return q->Q[++q->front];
}
}
int peek(struct Queue q)
{
if(q.rear == q.front)
{
printf("Queue underflow.\n");
}
else
{
return q.Q[q.front + 1];
}
}
int isEmpty(struct Queue q)
{
if(q.front == q.rear)
{
return 0;
}
else
{
return 1;
}
}
void display(struct Queue q)
{
for(int i = q.front + 1; i <= q.rear; i++)
{
printf("%d ",q.Q[i]);
}
printf("\n\n");
}
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
int data , size , front , rear;
int *Q;
};
// Create Queue
void createQueue(struct Queue *q , int size)
{
// q = (struct Queue *)malloc(sizeof(struct Queue));
q->size = size;
q->front = q->rear = -1;
q->Q = (int *)malloc(q->size * sizeof(int));
}
// enqueue code.
void enqueue(struct Queue *q , int key)
{
if(q->rear == q->size -1)
{
printf("Queue overflow.\n");
}
else
{
q->Q[++q->rear] = key;
}
}
// dequeue code.
int dequeue(struct Queue *q)
{
if(q->rear == q->front)
{
printf("Queue underflow.\n");
return -1;
}
else
{
return q->Q[++q->front];
}
}
int peek(struct Queue q)
{
if(q.rear == q.front)
{
printf("Queue underflow.\n");
}
else
{
return q.Q[q.front + 1];
}
}
int isEmpty(struct Queue q)
{
if(q.front == q.rear)
{
return 0;
}
else
{
return 1;
}
}
void display(struct Queue q)
{
for(int i = q.front + 1; i <= q.rear; i++)
{
printf("%d ",q.Q[i]);
}
printf("\n\n");
}
int main()
{
struct Queue q;
createQueue(&q , 5);
enqueue(&q , 2);
enqueue(&q , 3);
enqueue(&q , 2);
enqueue(&q , 4);
enqueue(&q , 9);
display(q);
printf("%d ",dequeue(&q));
printf("%d ",dequeue(&q));
printf("%d ",dequeue(&q));
printf("%d ",dequeue(&q));
printf("%d ",dequeue(&q));
printf("%d\n",dequeue(&q));
display(q);
int res = isEmpty(q);
printf("%d \n",res);
}
// Circular Queue.
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
int data , size , front , rear;
int *Q;
};
void createQueue(struct Queue *q , int size)
{
q->size = size;
q->front = q->rear = 0;
q->Q = (int *)malloc(q->size * sizeof(int));
}
void enqueue(struct Queue *q , int key)
{
if(q->front == (q->rear + 1)% q->size)
{
printf("Queue overflow.\n");
}
else
{
q->rear = (q->rear + 1) % q->size;
q->Q[q->rear] = key;
}
}
int dequeue(struct Queue *q)
{
if(q->front == q->rear)
{
printf("Queue underflow.\n");
return -1;
}
else
{
q->front = (q->front + 1) % q->size;
return q->Q[q->front];
}
}
int isEmpty(struct Queue q)
{
if(q.front == q.rear)
{
printf("Queue underflow.\n");
return 0;
}
else
{
return 1;
}
}
int peek(struct Queue q)
{
if(q.rear == q.front)
{
printf("Queue underflow.\n");
return -1;
}
else
{
return q.Q[(q.front + 1) % q.size];
}
}
void display(struct Queue q)
{
if(q.front == q.rear)
{
printf("\n");
return;
}
else
{
while(q.front != q.rear)
{
q.front = (q.front + 1) % q.size;
printf("%d ",q.Q[q.front]);
}
printf("\n");
}
}
int size(struct Queue q)
{
int count = 0;
while(q.front != q.rear)
{
q.front = (q.front + 1) % q.size;
count++;
}
return count;
}
int main()
{
struct Queue q;
createQueue(&q , 5);
enqueue(&q , 143);
enqueue(&q , 2);
enqueue(&q , 2);
enqueue(&q , 2);
// enqueue(&q , 5);
display(q);
int res = dequeue(&q);dequeue(&q);dequeue(&q);dequeue(&q);dequeue(&q);
printf("size is %d.\n",size(q));
display(q);
enqueue(&q , 1);
enqueue(&q , 2);
enqueue(&q , 3);
enqueue(&q , 3);
enqueue(&q , 4);
display(q);
printf("size is %d.\n",size(q));
}
#Loading....
#Loading....
#Loading....
struct Queue
{
int front , size , rear;
struct Node **Q;
};
void createQueue(struct Queue *q, int size)
{
q->front = q->rear = 0;
q->size = size;
q->Q = (struct Node **) malloc(q->size * sizeof(struct Node*));
};
void enqueue(struct Queue *q , struct Node *key)
{
if(q->front == (q->rear + 1) % q->size)
{
return;
}
else
{
q->rear = (q->rear + 1) % q->size;
q->Q[q->rear] = key;
}
}
struct Node* dequeue(struct Queue *q)
{
if(q->front == q->rear)
{
return NULL;
}
else
{
q->front = (q->front + 1) % q->size;
return q->Q[q->front];
}
}
int isEmpty(struct Queue q)
{
if(q.front == q.rear)
{
return 0;
}
else
{
return 1;
}
}
struct Node
{
int data;
struct Node *lchild , *rchild;
};
void createTree(struct Node *root)
{
struct Node *temp, *r;
int x;
struct Queue que;
createQueue(&que , 100);
printf("Enter the Element of Root.\n");
scanf("%d",&x);
root->data = x;
root->lchild = root->rchild = NULL;
enqueue(&que , root);
while(isEmpty(que))
{ temp = dequeue(&que);
printf("Enter the LeftChild Element of %d\n",temp->data);
scanf("%d",&x);
// for left_child.
if(x != -1)
{
r = (struct Node *)malloc(sizeof(struct Node));
r->data = x;
r->lchild = r->rchild = NULL;
temp->lchild = r;
enqueue(&que , r);
}
printf("Enter the RightChild Element of %d\n",temp->data);
scanf("%d",&x);
// for right_child.
if(x != -1)
{
r = (struct Node *)malloc(sizeof(struct Node));
r->data = x;
r->lchild = r->rchild = NULL;
temp->rchild = r;
enqueue(&que , r);
}
}
}
void preOrder(struct Node *q)
{
if(q == NULL)
{
return;
}
else
{
printf("%d ",q->data);
preOrder(q->lchild);
preOrder(q->rchild);
}
}
void postOrder(struct Node *q)
{
if(q == NULL)
{
return;
}
else
{
postOrder(q->lchild);
postOrder(q->rchild);
printf("%d ",q->data);
}
}
void inOrder(struct Node *q)
{
if(q == NULL)
{
return;
}
else
{
inOrder(q->lchild);
printf("%d ",q->data);
inOrder(q->rchild);
}
}
int height(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int hlc = height(root->lchild);
int hrc = height(root->rchild);
if(hlc > hrc)
return (hlc + 1);
else
return (hrc + 1);
}
}
int nodeCount(struct Node *root)
{
if(root == NULL)
return 0;
else
{
int clcn = nodeCount(root->lchild);
int crcn = nodeCount(root->rchild);
return (clcn + crcn + 1);
}
}
void levelOrderTraversal(struct Node *root)
{
if(root == NULL)
{
return;
}
else
{
struct Queue q;
createQueue(&q , 100);
printf("%d ",root->data);
enqueue(&q , root);
while(isEmpty(q))
{
struct Node *p = dequeue(&q);
if(p->lchild != NULL)
{
printf("%d ",p->lchild->data);
enqueue(&q , p->lchild);
}
if(p->rchild != NULL)
{
printf("%d ",p->rchild->data);
enqueue(&q , p->rchild);
}
}
printf(".\n");
}
}
int numberOfLeafNode(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int llc = numberOfLeafNode(root->lchild);
int rlc = numberOfLeafNode(root->rchild);
if(root->lchild == NULL && root->rchild == NULL)
{
return llc + rlc + 1;
}
else
{
return llc + rlc;
}
}
}
int oneDegreeNode(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int lc = oneDegreeNode(root->lchild);
int rc = oneDegreeNode(root->rchild);
if((root->lchild == NULL && root->rchild != NULL) || (root->lchild != NULL && root->rchild == NULL))
{
return (lc + rc + 1);
}
else
{
return (lc + rc);
}
}
}
int TwoDegreeNode(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int lc = TwoDegreeNode(root->lchild);
int rc = TwoDegreeNode(root->rchild);
if(root->lchild != NULL && root->rchild != NULL)
{
return lc + rc + 1;
}
else
{
return lc + rc;
}
}
}
// Code for binary Tree.
#include <stdio.h>
#include <stdlib.h>
struct Queue
{
int front , size , rear;
struct Node **Q;
};
void createQueue(struct Queue *q, int size)
{
q->front = q->rear = 0;
q->size = size;
q->Q = (struct Node **) malloc(q->size * sizeof(struct Node*));
};
void enqueue(struct Queue *q , struct Node *key)
{
if(q->front == (q->rear + 1) % q->size)
{
return;
}
else
{
q->rear = (q->rear + 1) % q->size;
q->Q[q->rear] = key;
}
}
struct Node* dequeue(struct Queue *q)
{
if(q->front == q->rear)
{
return NULL;
}
else
{
q->front = (q->front + 1) % q->size;
return q->Q[q->front];
}
}
int isEmpty(struct Queue q)
{
if(q.front == q.rear)
{
return 0;
}
else
{
return 1;
}
}
// Create Tree..
struct Node
{
int data;
struct Node *lchild , *rchild;
};
void createTree(struct Node *root)
{
struct Node *temp, *r;
int x;
struct Queue que;
createQueue(&que , 100);
printf("Enter the Element of Root.\n");
scanf("%d",&x);
root->data = x;
root->lchild = root->rchild = NULL;
enqueue(&que , root);
while(isEmpty(que))
{ temp = dequeue(&que);
printf("Enter the LeftChild Element of %d\n",temp->data);
scanf("%d",&x);
// for left_child.
if(x != -1)
{
r = (struct Node *)malloc(sizeof(struct Node));
r->data = x;
r->lchild = r->rchild = NULL;
temp->lchild = r;
enqueue(&que , r);
}
printf("Enter the RightChild Element of %d\n",temp->data);
scanf("%d",&x);
// for right_child.
if(x != -1)
{
r = (struct Node *)malloc(sizeof(struct Node));
r->data = x;
r->lchild = r->rchild = NULL;
temp->rchild = r;
enqueue(&que , r);
}
}
}
void preOrder(struct Node *q)
{
if(q == NULL)
{
return;
}
else
{
printf("%d ",q->data);
preOrder(q->lchild);
preOrder(q->rchild);
}
}
void inOrder(struct Node *q)
{
if(q == NULL)
{
return;
}
else
{
inOrder(q->lchild);
printf("%d ",q->data);
inOrder(q->rchild);
}
}
void postOrder(struct Node *q)
{
if(q == NULL)
{
return;
}
else
{
postOrder(q->lchild);
postOrder(q->rchild);
printf("%d ",q->data);
}
}
int height(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int hlc = height(root->lchild);
int hrc = height(root->rchild);
if(hlc > hrc)
return (hlc + 1);
else
return (hrc + 1);
}
}
int numberOfLeafNode(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int llc = numberOfLeafNode(root->lchild);
int rlc = numberOfLeafNode(root->rchild);
if(root->lchild == NULL && root->rchild == NULL)
{
return llc + rlc + 1;
}
else
{
return llc + rlc;
}
}
}
int TwoDegreeNode(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int lc = TwoDegreeNode(root->lchild);
int rc = TwoDegreeNode(root->rchild);
if(root->lchild != NULL && root->rchild != NULL)
{
return lc + rc + 1;
}
else
{
return lc + rc;
}
}
}
int oneDegreeNode(struct Node *root)
{
if(root == NULL)
{
return 0;
}
else
{
int lc = oneDegreeNode(root->lchild);
int rc = oneDegreeNode(root->rchild);
if((root->lchild == NULL && root->rchild != NULL) || (root->lchild != NULL && root->rchild == NULL))
{
return (lc + rc + 1);
}
else
{
return (lc + rc);
}
}
}
int nodeCount(struct Node *root)
{
if(root == NULL)
return 0;
else
{
int clcn = nodeCount(root->lchild);
int crcn = nodeCount(root->rchild);
return (clcn + crcn + 1);
}
}
void levelOrderTraversal(struct Node *root)
{
if(root == NULL)
{
return;
}
else
{
struct Queue q;
createQueue(&q , 100);
printf("%d ",root->data);
enqueue(&q , root);
while(isEmpty(q))
{
struct Node *p = dequeue(&q);
if(p->lchild != NULL)
{
printf("%d ",p->lchild->data);
enqueue(&q , p->lchild);
}
if(p->rchild != NULL)
{
printf("%d ",p->rchild->data);
enqueue(&q , p->rchild);
}
}
printf(".\n");
}
}
int main()
{
struct Node root;
createTree(&root);
levelOrderTraversal(&root);
printf("Total number of Nodes is %d",nodeCount(&root));
}
struct Node
{
int data , height;
struct Node *lchild , *rchild;
}*head = NULL;
int nodeHeight(struct Node *p)
{
int hlc = p && p->lchild ? p->lchild->height : 0;
int hrc = p && p->rchild ? p->rchild->height : 0;
return hlc > hrc ? hlc + 1 : hrc + 1;
}
int balanceFactor(struct Node *p)
{
int hlc = p && p->lchild ? p->lchild->height : 0;
int hrc = p && p->rchild ? p->rchild->height : 0;
return (hlc - hrc);
}
struct Node* llRotation(struct Node *root)
{
struct Node *rootLC = root->lchild;
struct Node *rootRC = rootLC->rchild;
rootLC->rchild = root;
root->lchild = rootRC;
root->height = nodeHeight(root);
rootLC->lchild->height = nodeHeight(rootLC->lchild);
rootLC->height = nodeHeight(rootLC);
if(head == root)
{
head = rootLC;
}
return rootLC;
}
struct Node* lrRotation(struct Node *root)
{
struct Node *rootLC = root->lchild;
struct Node *rootLCR = rootLC->rchild;
root->lchild = rootLCR->rchild;
rootLC->rchild = rootLCR->lchild;
rootLCR->lchild = rootLC;
rootLCR->rchild = root;
rootLCR->lchild->height = nodeHeight(rootLCR->lchild);
rootLCR->rchild->height = nodeHeight(rootLCR->rchild);
rootLCR->height = nodeHeight(rootLCR);
if(head == root)
{
head = rootLCR;
}
return rootLCR;
}
struct Node* rrRotation(struct Node *root)
{
struct Node *rootRC = root->rchild;
struct Node *rootRCL = rootRC->lchild;
root->rchild = rootRCL;
rootRC->lchild = root;
root->height = nodeHeight(root);
rootRC->rchild->height = nodeHeight(rootRC->rchild);
rootRC->height = nodeHeight(rootRC);
if(head == root)
{
head = rootRC;
}
return rootRC;
}
struct Node* rlRotation(struct Node *root)
{
struct Node* rootRC = root->rchild;
struct Node* rootRCL = rootRC->lchild;
root->rchild = rootRCL->lchild;
rootRC->lchild = rootRCL->rchild;
rootRCL->lchild = root;
rootRCL->rchild = rootRC;
root->height = nodeHeight(root);
rootRC->height = nodeHeight(rootRC);
rootRCL->height = nodeHeight(rootRCL);
if(head == root)
{
head = rootRCL;
}
return rootRCL;
}
struct Node* insert(struct Node *root , int key)
{
if(root == NULL)
{
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
p->data = key;
p->lchild = p->rchild = NULL;
p->height = 1;
return p;
}
else if(root->data > key)
{
root->lchild = insert(root ->lchild , key);
}
else if(root->data < key)
{
root->rchild = insert(root->rchild , key);
}
root->height = nodeHeight(root);
if(balanceFactor(root) == 2 && balanceFactor(root->lchild) == 1)
{
return llRotation(root);
}
else if(balanceFactor(root) == 2 && balanceFactor(root->lchild) == -1)
{
return lrRotation(root);
}
else if(balanceFactor(root) == -2 && balanceFactor(root->rchild) == -1)
{
return rrRotation(root);
}
else if(balanceFactor(root) == -2 && balanceFactor(root->rchild) == 1)
{
return rlRotation(root);
}
return root;
}
// AVL Tree Code.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data , height;
struct Node *lchild , *rchild;
}*head = NULL;
int nodeHeight(struct Node *p)
{
int hlc = p && p->lchild ? p->lchild->height : 0;
int hrc = p && p->rchild ? p->rchild->height : 0;
return hlc > hrc ? hlc + 1 : hrc + 1;
}
int balanceFactor(struct Node *p)
{
int hlc = p && p->lchild ? p->lchild->height : 0;
int hrc = p && p->rchild ? p->rchild->height : 0;
return (hlc - hrc);
}
//llRotation code.
struct Node* llRotation(struct Node *root)
{
struct Node *rootLC = root->lchild;
struct Node *rootRC = rootLC->rchild;
rootLC->rchild = root;
root->lchild = rootRC;
root->height = nodeHeight(root);
rootLC->lchild->height = nodeHeight(rootLC->lchild);
rootLC->height = nodeHeight(rootLC);
if(head == root)
{
head = rootLC;
}
return rootLC;
}
// lrRotation code.
struct Node* lrRotation(struct Node *root)
{
struct Node *rootLC = root->lchild;
struct Node *rootLCR = rootLC->rchild;
root->lchild = rootLCR->rchild;
rootLC->rchild = rootLCR->lchild;
rootLCR->lchild = rootLC;
rootLCR->rchild = root;
rootLCR->lchild->height = nodeHeight(rootLCR->lchild);
rootLCR->rchild->height = nodeHeight(rootLCR->rchild);
rootLCR->height = nodeHeight(rootLCR);
if(head == root)
{
head = rootLCR;
}
return rootLCR;
}
//rrRotation code..
struct Node* rrRotation(struct Node *root)
{
struct Node *rootRC = root->rchild;
struct Node *rootRCL = rootRC->lchild;
root->rchild = rootRCL;
rootRC->lchild = root;
root->height = nodeHeight(root);
rootRC->rchild->height = nodeHeight(rootRC->rchild);
rootRC->height = nodeHeight(rootRC);
if(head == root)
{
head = rootRC;
}
return rootRC;
}
// rlRotation code.
struct Node* rlRotation(struct Node *root)
{
struct Node* rootRC = root->rchild;
struct Node* rootRCL = rootRC->lchild;
root->rchild = rootRCL->lchild;
rootRC->lchild = rootRCL->rchild;
rootRCL->lchild = root;
rootRCL->rchild = rootRC;
root->height = nodeHeight(root);
rootRC->height = nodeHeight(rootRC);
rootRCL->height = nodeHeight(rootRCL);
if(head == root)
{
head = rootRCL;
}
return rootRCL;
}
// insert function.
struct Node* insert(struct Node *root , int key)
{
if(root == NULL)
{
struct Node *p = (struct Node*)malloc(sizeof(struct Node));
p->data = key;
p->lchild = p->rchild = NULL;
p->height = 1;
return p;
}
else if(root->data > key)
{
root->lchild = insert(root ->lchild , key);
}
else if(root->data < key)
{
root->rchild = insert(root->rchild , key);
}
root->height = nodeHeight(root);
if(balanceFactor(root) == 2 && balanceFactor(root->lchild) == 1)
{
return llRotation(root);
}
else if(balanceFactor(root) == 2 && balanceFactor(root->lchild) == -1)
{
return lrRotation(root);
}
else if(balanceFactor(root) == -2 && balanceFactor(root->rchild) == -1)
{
return rrRotation(root);
}
else if(balanceFactor(root) == -2 && balanceFactor(root->rchild) == 1)
{
return rlRotation(root);
}
return root;
}
// inOrder Traversal code.
void inOrder(struct Node *root)
{
if(root == NULL)
{
return;
}
else
inOrder(root->lchild);
printf("%d ",root->data);
inOrder(root->rchild);
}
int main()
{
head = insert(head , 10);
head = insert(head , 20);
head = insert(head , 30);
head = insert(head , 40);
head = insert(head , 50);
head = insert(head , 60);
head = insert(head , 70);
head = insert(head , 80);
head = insert(head , 90);
head = insert(head , 100);
// insert(head , 3);
// insert(head , 10);
// insert(head , 1);
// insert(head , 6);
// insert(head , 14);
// insert(head , 4);
// insert(head , 7);
// insert(head , 13);
inOrder(head);
printf("\nheight is %d.\n",nodeHeight(head));
}
//OUTPUT
//10 20 30 40 50 60 70 80 90 100
//height is 4.
#Loading....
....
....
....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
....
....
....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
#Loading....
// code for hashTable.
#include <stdio.h>
#include <stdlib.h>
struct Node
{
int data;
struct Node *next;
};
void sortedInsert(struct Node **H , int key)
{
struct Node *t , *p = NULL , *q = *H;
t=(struct Node*)malloc(sizeof(struct Node));
t->data = key;
t->next = NULL;
if(*H == NULL)
{
*H = t;
}
else
{
while(q && q->data < key)
{
p = q;
q = q->next;
}
if(p == *H)
{
t->next = *H;
*H = t;
}
else
{
t->next = p->next;
p->next = t;
}
}
}
struct Node* search(struct Node *p , int key)
{
while(p != NULL)
{
if(p->data == key)
{
return p;
}
p = p->next;
}
return NULL;
}
struct Node* sortedDelete(struct Node **h , int key)
{
struct Node *p = NULL , *q = *h , *temp;
if(q->data == key)
{
*h = NULL;
return q;
}
else
{
while(p && p->data != key)
{
q = p;
p = p->next;
}
temp = p;
if(p->next != NULL)
{
q->next = q->next->next;
}
else
{
q->next = NULL;
}
free(p);
return temp;
}
}
int hashKey(int key)
{
return key % 10;
}
void insert(struct Node *h[] , int key)
{
int x = hashKey(key);
sortedInsert(&h[x] , key);
}
struct Node* deleteElement(struct Node *h[] , int key)
{
int x = hashKey(key);
return sortedDelete(&h[x] , key);
}
int main()
{
struct Node *HT[10];
for(int i = 0; i < 10; i++)
HT[i]= NULL;
insert(HT , 5);
insert(HT , 10);
insert(HT , 15);insert(HT , 8);
for(int i = 0; i < 10; i++)
printf("%d ", HT[i]);
printf("\n");
// struct Node *temp = search(HT[hashKey(8)] , 8);
struct Node *temp = deleteElement(HT , 10);
printf("%d \n",temp->data);
temp = deleteElement(HT , 8);
printf("%d \n",temp->data);
for(int i = 0; i < 10; i++)
printf("%d ", HT[i]);
printf("\n");
}