0% found this document useful (0 votes)
21 views11 pages

Adobe Scan 5 Dec 2023

Uploaded by

Megha Raju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views11 pages

Adobe Scan 5 Dec 2023

Uploaded by

Megha Raju
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

#include <stdio .

h>
void insert (int a[] , int size , int ele , int flag )
{
int i, key, index;
key = ele % size ;
if (flag == 1)
{
for (i = 0; i < size ; i ++)
{
index = (key + i) % size ;
if (a[index] == - 1)
{
a[index] = ele ;
break ;
}
}
if (i == size)
{
printf( 11 element can't be inserted\ n");
}
}
if (flag == 2)
{
for (i = 0; i < size ; i ++)
{
index = (key + i * i) % size ;
if (a[index] - 1)
{
a[index] = ele ;
break ;
}
}
if (i == size)
{
pr intf ("element can ' t be inserted\n "
}
}
}
int main()
{
int size, choice, element, flag, i;
printf( "enter t he tabl e size:");
scanf("%d ", &size);
int a[size];
for (i = 0; i < size; i ++)
{
a[i] = - 1;
}
pri ntf( "enter\nl)li near probing\n2)quadratic probing\n ");
~,:a_ I oj}
• ·r - - - - - - - - - - - - - - - -- - ---___:.7
i scanf( 11 %d 11 , &flag);
while (choice != 3)
{
printf("enter the choice\nl)insert\n2)display\ n3)exit\ n");
scanf( 11 %d 11 , &choice);
switch (choice)
{
case 1:
{
pri ntf("enter the element: \n ");
scanf("%d ", &element);
insert(a, size, element, flag);
break ;
}
case 2:
{
printf("KEY :\tVALUE: \ n");
for (i = 0; i < size; i ++)
{
pri ntf("%d\t%d\n", i, a[i]);
}
}
}
}
}

Linear Probing:
PROBLEMS OUTPUT TERM INAL DEBUG CONSOLE 0 Code + v 8 ® < X

PS C: \Users\Adith\Dropbox\Se -C\Code-playground> ed 11 c : \Users\


Adith\Dropbox\Sem 3\DS- C\ de-playground\ 11 if ( $? ) { gee hashtabl
e.e -o hashtable} ; if $? ) { .\hashtable }
enter the table size :
enter
l)linear probing
2)quadratic probing
1
enter the choice
l)insert
2)display
3)exit
1
enter the element :
10
"
~

enter the choice


l)insert
2)display
-
c
3)exit II
1
enter the element :
20
enter the choice
l)insert
2)display
3)exit
1
enter the element :
30
enter the choice
l)insert
2)display
3)exit
2
HEY : VALUE :
0 30
1 10
2 20
enter the choice
l)insert
2)display
3)exit
1
enter the element :
1'0
element can't be inserted
enter the choice
l)insert
2)display
3)exit
3
Quadratic Probing:
enter the t able size :3
enter
l)linear probing
2)quadratic pr obing
2
enter the choice
l)insert
2)display
3)exit
1
enter the element:
10
enter the choice
l)insert
2)display
3)exit
1
enter the element:
16
enter the choice
!)insert
2)display
3)exit
2
KEY: VALUE:
0 -1
1 16
2 -1
enter the
l)insert
2)display
3)exit
3
t EYERc,~~ : ,~

•~
~ O O ~ \-.a" at, cl~ 4t.n,t C,t\AU
, ~te. .A C f'r05YQ~ ~ -·""",... tM~ n , ,... . , -

~ AtM'

' -ro w~Q. ~


~ ~~~ -
~
t A~oe ITHM ._

~ ~' ', ~
~

.,,-u-qJ '5""" : Cem~~ ..:nu. 'Yo&M.L-


..w~ .A~ p~ ·

~~ b . :r; 'V~ ~ ~ .1~ ~


lW~A, ~ ~ ~ .

~ ~
D
• ~~ ~eltt ~ ~pQ.U'"\ed -tvo \d .
0
.-U-Q.f> 8. • t,MU I\ ..tiu, ~c<:. e.wa

~
M?-L-
D
°4
e:t:o
~ ~
~ Q.,,<,<:,O"(dL.1, 'trll'W ~~

~-
~Q,\'>°' ~ '<"'-f~ ~ ~ l\)"n,,1..~

.5t~ 10 •. ~~ -
#include <stdio .h>
int siz e = 0·I
voi d swap( int *a int *b)
{
I

int temp = *b;


*b = *a·I
*a = temp;
}
void heapify(i nt array[] int size , in ti)
{
I

i f (size == 1)
{
pr i ntf ( 11 Single element in the heap")
} ;
els e
{
int largest = i ;
int l = 2 * i + 1·I
i nt r = 2 * i + 2·I
i f (l < siz e && array[l ] > array[ lar ges t])
lar ges t = l;
if Cr < siz e && array[ r] > array[l arg est ])
largest = r;
if (la rge st != i )
{
swap( &array [i ], &array [la rge st] );
heapif y(array , size , lar ges t);
}
}
}
void inser t (in t array [] , int newNum)
{
if (si ze == 0)
{
array [0] = newNum ;
siz e += 1;
}
els e
{
array [si ze] = newNum ;
siz e += 1;
for (in t i = siz e / 2 - 1 ; i >= 0 ; i
--)
{
heapify (array , siz e, i);
}
}
}
void deleteRoot( int arr ay[] , int num
)
{
' - -_ _ _ _ _ _
_______
_ _ _ _ _ __
_ _ ____ _, i.
int i;
for (i = 0; i < size; i ++)
{
if (num == array [i])
break ;

!wap(&array[i], &array [size - 1]);


size -= 1;
for (int i = size I 2 - l; i >= e; i - - )
{
heapify(array , size, i);
}
}
void printArray(int array[] , int size)
{
for (int i = 0; i < size ; ++i)
printf( "%d ", array[i]);
printf( "\n");
}
int main()
{
int array[10];
insert (array, 20);
insert(array, 13);
insert(array, 3q);
insert(array, 56);
insert(array, 12);
insert(array, 10);
printf( "Max-Heap array: 11 ) ;
printArray(array, size);
deleteRoot(array, 13) ·
printf("After delet· g an element : 11
);

printArray(array size);

P~OBLEMS OUTPUT TERMINAL DEBUG CONSOLE (8 Code + "' 8 @ < X

PS C:\Users\Adith\Dropbox\Sem 3\0S-C\Code~playground> cd 11 c:\Users\


Adith\Dropbox\ Sem 3\DS-C\Code-playground\" ; if ($?) { gee heap . c -
o heap} ; if ($?) { . \heap }
ax- Heap array : 56 3q 20 13 12 10
After deleting an element: 56 3q 20 10 12
' Rq__t)t.T:

~d 0

~ t,____ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ___J
-- - \ ,<
J

I CXEQCIS E I~ •
t

• • ~
a
• IN)\."-~ 0. 0, ~5-ro.Y'f' -to
~ft'p\~rf'~Y\-r ~ O'()el\O.t1 tit\ °"'

j:::p f'mr>\<2XY\~
~ -rO t,0-"A"\ Q.. 0.. (1 {>~y OJ'\'\

• c-tiA..L -l~Q.. .9t,'Y\) ~ -

t
t A~O R 1TH f-\ ~

- t "'<eM e. .,c,... c..t -n"e. no&.e.


~ ~
lo en&. wf~ lt.'.)O ')" cl

0
:to I~~ I ~ h)

-tYl"e, gt ~ ~ •
;

r
!M.'Yc~ c.'.> .f..1 ~ tto

H!Lp b : l'Y e°'-1Q ~ m CA ,o"' t ') a,_"\- ~<l.u!u ~.r


t ,j 0
6--' ('('\ e.-v.., ~ o,.t
r
Ctdlci "1-lkru± 0 ~ -
l ,0
f'~ tu<l ~ ¼,(.,
tJ.s\~
,t,
.;lnQ l\.'Y\..\ ~ ,
t

! e-v- not•

g~ 1 . (_l\)Q.
#include <stdio.h>
#include <stdlib . h>
#include <string.h>
#define N 26
typedef struct Tr ieNode TrieNode;
struct Tr ieNode
{
char data;
TrieNode *Children[N];
int is_leaf;
; }
TrieNode *make_trienode(char dat a)
{
TrieNode *node = (TrieNode *)calloc( l , sizeof(Tr i eNode));
for (int i= 0; i < N; i ++)
node->children[i] = NULL ;
node->is_leaf = 0 ;
node~>data = data ;
return node;
}
void f ree_trienode(Tr ieNode *node)
{
for (int i = 0; i < N; i ++)
{
if (node->ch ildren[i] != NULL)
{
f r ee_tr ienode(node->child ren[i]);
}
else
{
continue ;
}
}
free (node);
}
TrieNode *inser t _trie(TrieNode *root , char *Word )
{
TrieNode *temp = root ;
for (int i = 0; word [i] != ' \0 '; i ++)
{
int idx = (int)word [i] - 'a' ;
if (temp->children[idx] == NULL)
I
{
temp- >children [idx] = make_trie node(word [i]);
}
else
{
}
temp = temp->children[idx];
}
temp->is_leaf = 1;
return root ;
}
int search_t r ie (Tr i eNode *root , char *Word)
{
Tr ieNode *temp = r oot ;
for (int i = 0; word [i] != ' \ 0'; i ++)
{
int position = word [i] - 'a';
if (temp->children[position] == NULL)
return 0;
temp = temp->children[position];
}
if (temp != NULL && temp->is_leaf == 1)
return 1;
return 0;
}
void print _trie(TrieNode *root)
{
if ( !root)
return ;
TrieNode *temp = root ;
printf("%c -> ", temp->data);
for (int i = 0; i < N; i ++)
{
pri nt_trie(temp->children[i]);
}
I }
void print_search(TrieNode *root , char *Word)
' {
printf("Searching for %s : ", word);
if (search_trie(root , word) == 0)
printf("Not Found\n ");
else
printf("Found! \n ");
}
int main()
{
TrieNode *root = make_trienode(' \0 ');
root = insert_trie(root, "hello");
root = insert_trie(root, "hi");
root = insert_trie(root, "teabag");
root = insert_trie(root, "teacan");
print_search(root , "tea");
print_search(root, "t eabag");
print_search(root, "teacan");
print_search(root , "hi");
print_search(root , "hey");
print_search(root, "hello");
print_trie (root);
free_tri enode(root);
return 0 I·
}

ill Code + v 8 @ ( X I
PROclLEMS OUTPUT TERMINAL DEBUG CONSOLE

PS _C: \Users\Adith\Dropbox\Sem 3\DS-C\Code-playground> cd "c:\users\


11
Adit~\Dropbox\Sem 3\DS-C\Code-playground\ if ($?) { gee trie . c -
;

o tr1e} ; if ($? ) { .\trie }


Searching for tea: Not Found
Searching for teabag : Found!
Searching for teacan: Found!
Searching for hi: Found!
Searching for hey: Not Found
Searching for hello: Found!
-> h -> e -> l -> l -> o -> i ->a-> b ->a-> g -> c -
>a-> n ->

:Jl,\..L 0'IMP ' ~


~o.:t1t,Y\.

:Xi . C-e-r'<'f>,~e.,n;te_J

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy