DataStructure Concepts
Heap Data Structures
• Heap is a special case of balanced binary tree data structure where
the root-node key is compared with its children and arranged
accordingly.
If α has child node β then
key(α) ≥ key(β)
• Based on this criteria, a heap can be of two types −
Max Heap
Min Heap
Min-Heap − Where the value of the root node
is less than or equal to either of its children.
Max-Heap − Where the value of the root node is
greater than or equal to either of its children.
Max Heap Construction Algorithm
Max Heap Deletion Algorithm
1.#include <stdio.h>
2.#include <stdlib.h>
3.#define MAX 20
4.void maxheapify(int *, int, int);
5.int* buildmaxheap(int *, int);
6.void main()
7.{
8.int i, t, n;
9.int *a = calloc(MAX, sizeof(int));
10.int *m = calloc(MAX, sizeof(int));
11.printf("Enter no of elements in the arrayn");
12.scanf("%d", &n);
13.printf("Enter the arrayn");
14.for (i = 0; i < n; i++) {
15.scanf("%d", &a[i]);
16.}
17.m = buildmaxheap(a, n);
18.printf("The heap isn");
19.for (t = 0; t < n; t++) {
20.printf("%dn", m[t]);
21.}
22.}
1.int* buildmaxheap(int a[], int n)
2.{
3.int heapsize = n;
4.int j;
5.for (j = n/2; j >= 0; j--) {
6.maxheapify(a, j, heapsize);
7.}
8.return a;
9.}
1.void maxheapify(int a[], int i, int heapsize)
2.{
3.int temp, largest, left, right, k;
4.left = (2*i+1);
5.right = ((2*i)+2);
6.if (left >= heapsize)
7.return;
8.else {
9.if (left < (heapsize) && a[left] > a[i])
10.largest = left;
11.else
12.largest = i;
13.if (right < (heapsize) && a[right] > a[largest])
14.largest = right;
15.if (largest != i) {
16.temp = a[i];
17.a[i] = a[largest];
18.a[largest] = temp;
19.maxheapify(a, largest, heapsize);
20.}
21.}
22.}
Hash Table
• Hash Table is a data structure which stores data in an associative
manner. In a hash table, data is stored in an array format, where each
data value has its own unique index value.
Hashing:
• Hashing is a technique to convert a range of key values into a range of
indexes of an array. We're going to use modulo operator to get a
range of key values. Consider an example of hash table of size 20, and
the following items are to be stored. Item are in the (key,value)
format.
• (1,20)
• (2,70)
• (42,80)
• (4,25)
• (12,44)
• (14,32)
• (17,11)
• (13,78)
• (37,98)
After Linear probing
DataStructure Concepts-HEAP,HASH,Graph

DataStructure Concepts-HEAP,HASH,Graph

  • 1.
  • 2.
    Heap Data Structures •Heap is a special case of balanced binary tree data structure where the root-node key is compared with its children and arranged accordingly. If α has child node β then key(α) ≥ key(β) • Based on this criteria, a heap can be of two types − Max Heap Min Heap
  • 3.
    Min-Heap − Wherethe value of the root node is less than or equal to either of its children.
  • 4.
    Max-Heap − Wherethe value of the root node is greater than or equal to either of its children.
  • 5.
  • 7.
  • 9.
    1.#include <stdio.h> 2.#include <stdlib.h> 3.#defineMAX 20 4.void maxheapify(int *, int, int); 5.int* buildmaxheap(int *, int); 6.void main() 7.{ 8.int i, t, n; 9.int *a = calloc(MAX, sizeof(int)); 10.int *m = calloc(MAX, sizeof(int)); 11.printf("Enter no of elements in the arrayn"); 12.scanf("%d", &n); 13.printf("Enter the arrayn"); 14.for (i = 0; i < n; i++) { 15.scanf("%d", &a[i]); 16.} 17.m = buildmaxheap(a, n); 18.printf("The heap isn"); 19.for (t = 0; t < n; t++) { 20.printf("%dn", m[t]); 21.} 22.}
  • 10.
    1.int* buildmaxheap(int a[],int n) 2.{ 3.int heapsize = n; 4.int j; 5.for (j = n/2; j >= 0; j--) { 6.maxheapify(a, j, heapsize); 7.} 8.return a; 9.}
  • 11.
    1.void maxheapify(int a[],int i, int heapsize) 2.{ 3.int temp, largest, left, right, k; 4.left = (2*i+1); 5.right = ((2*i)+2); 6.if (left >= heapsize) 7.return; 8.else { 9.if (left < (heapsize) && a[left] > a[i]) 10.largest = left; 11.else 12.largest = i; 13.if (right < (heapsize) && a[right] > a[largest]) 14.largest = right; 15.if (largest != i) { 16.temp = a[i]; 17.a[i] = a[largest]; 18.a[largest] = temp; 19.maxheapify(a, largest, heapsize); 20.} 21.} 22.}
  • 12.
    Hash Table • HashTable is a data structure which stores data in an associative manner. In a hash table, data is stored in an array format, where each data value has its own unique index value. Hashing: • Hashing is a technique to convert a range of key values into a range of indexes of an array. We're going to use modulo operator to get a range of key values. Consider an example of hash table of size 20, and the following items are to be stored. Item are in the (key,value) format.
  • 13.
    • (1,20) • (2,70) •(42,80) • (4,25) • (12,44) • (14,32) • (17,11) • (13,78) • (37,98)
  • 14.