Ds Impp
Ds Impp
:, I~I
~'{:•.,;'::..-,:~
ha.s-
3. In hashing, large keys ~e;i!icoft.;9Wt~.~ ·=r nto small keys by using hash
4.
5.
~c!:i::s are the~~~stru cture called
'nle task ofhashingls t.iJitl,tof>ute entries (key/valu~i..r·-, _Jl~if~mly
·,.i·;-· .~ • •❖... w:•:~::O;;.. •"•f.,.,:,~::~:~~---···
across an array; '.~ .
···;,..
: ~~f§~$~:~€!:'.:.::::e:~:
• 4 .to _f#.ig,: :~ e item us1ng{flict. shdltQY'ha$.hed key than to find 1t
C.
step l : Divide the key into ~qu '. . 'ized parts (e.g., two digits each) :
Parts : 12, 34, 56, 78, 9 <wr .·
Step 2 : Sum these parts together :
,S um =12 + 34 + 56 + 78 + 9 =189
Step 3: Compute the hash value by taking the remainder when
dividing by 10 (size of the hash table):
HashValue = 189 % 10 = 9
Thus, the key "123456789" is mapped to slot 9 in the hash table.
. Wb~t is h_a•hi:ng ? Give the characteristics of hash
fuaet1on. El<plmn collision reaolution technique in h&11hing. .
@~;~l!~y,.~,i
t;llit;Jm
OR
Data Structure S-9 E (CMT-Sem-3)
3.
4.
5.
c. · . · , shing
Hashing witli separate chai . .,,,,,,
1. This method maintains the c·ain of ~timents which have same hash
address. ·+:c ,.dfY
2. We can take the hash table as an lt~ay of pointers
3. Size of hash table can be nun1ber of records. ·
4. Here each pointer will poi~t to one_ linked list and the elements which
have same hash address will be n1 aintained in the 11. k .d 1. t
5 w ·t · h .
. . e can_mau\a1n t ~ 1inked list in sortecl order and ea h
n e 1s
.
~ hst_w1H contain the whole record with key. . 1 n
c e e1nents o1
6. For inserting one element, first we have to get th h --
hash function which will map in the hash tabl t~ ashh value through
be inserted in the linked list. e, en t at element will
7. Searching a key is also same, fir 8 t we will get the ha .
. J !lb~e .through hash function, then we will sh key value 1n hash
corresponding linked list. search the element in
( Qui k
\1 c~
,~t.R~ sorts
~l'-~
~)-T ~~·; rm
~L an array
A~,. R,r~i,~_.11 '
~ - " ~ nts.
! '!:~7 ::~i!:]TO~U~, y 8
Sef/.13EG,w~y·LoWE R [TOPJ,if!ll ~~!Q:1rl;ril i r -f{TOP],
TOP = TOP - 1 '\i:f@ff,j}Jt,JF*
o;;:,*:-·94,.,,...•
1
h t \ T """--
'····,.·~½:,l,it\.J~{" l"\C"n-)
v l'
~~\
l, / A Ip+ i- l]
endfor
5. forj=lton 2
.d0
Rl,1 =A[q + j]
end(or
6. =
~[n1 +. 1) oo, R[n2 ·+ 1] =oo
7. i = 1,J = 1
8. for k = p to r
do
if L[i] sR(/1
then Ark] ~ L[i]
i=i+l
else Ark]= RUJ
i=J + 1
endif
e'6~
g ~lt e')(,t
u.'t)l;S,\j ~ ~ Ol{\ "--~ ~\
(\-\\~. 01}.,e. ~ a l~~l\\
EX'bcfCl m~~~~Ol'n)
~"~ ~~~
MAi \-\tA~1~ tA :\YJ 3-29 1
Data Stru~ture •
t.. t "- \@y\:; t,1
2. r ~ right [i]
3. if l < heap-size IA.] and A [l] > A [i]
4. then largest +- l
5. else largest +- i
6.. if r < heap-size [A] and A [r] > A [larg,est]
7. then largest +- r
8. if largest -:;; i
9. then exchange A[i] ~ A[largest),
~ '~JJ~ ~
3.
4.
do exchangeA(l] +.+A[i]
heap-size (A] ~ h~
,ti~--
❖%.
e , .~~
-~
~~~$'?
5
··
? '>:tft'fc?!•<<i·•"'f•XW
~-HEAP~ (A, 1)
~ ~'f\\9•~ ~ ~
~~
.Rot.~~'ln) 1
~
t.;.;~%
;:,:;;;,~
!~
11~~'. Write a sl£rit:~ ~-~ .. - ~! - · .
--- ~ ~t~
ct°\ (l'(\ iQ ~~ ~ ® ~ ~\ \~ <;~"~~'2.)
t.. en~ c::,£ ~\ ) 3 ~<.\~1
~, '. ,,,. . . .
t1tr.<:'':/ '. .
1. A 4':- ~ ___________ _ _en, . -
'2· R)ts E'Ctt\ \Je~"' '1-€\J ltn1.
---
~ MA~'t-~tTl~ ) . _ _ __
~·~'at~ ~~~$., Ctt tn~~t~\(\ ~~
b~~~~-w ·
..0.
L\. Ri~ ffict\ ~elC\\\lJt~ n-·\..n
------
- - ---...-¥txi~c~~~ e,~,
~ \~ fl)_\t)-&'tX~t )i- ~ :.L~ ~£-1 \)))
\. 'l. ~ \\ \N he - ~ \n~\e- 9,C)~ce l l.n ~
--·
(), <;~<\) 6 ' (\ ~ \l ~ C n )
Dat a Str uct ure 4-9 E (CSIIT-Sem-3)
Qu e 4.8. )
ey in Bin ary Sea rch
i. Wr ite an iter ative fun ctio n to sea rch a k
Tree <BST>. _:i otir: ,/. , ,~iro,
e sui tab le exa mp le.
ii. Di8CU88 dis adv ant ages of rec urs ion wit h 90m
An swe r )
, PAR)
i. FIN D(I NFO, LEF .r, RIGHT, ROOT, ITE M, LOC i atio n is given.
rch tree Tis the mem ory and d.l11TEM of
A bin ary sea
in T . · the location
This pro ced ure find s the loca tion LOC of ITE M
ci
PA Rof the par ent of ITEM. The re are thre e spe e is emp ty.
1. LOC = NU LL and PAR = NU LL will ind
icat
is the roo t of T.
2. LOC * NU LL and PAR = NU LL will indi c Mi s not in T and
3. LOC = NU LL and PA R* NU LL will ind.
···
•· N cati on PA R:
can be add ed to T as a chil d of
a. [Tre e emp ty ?]
If RO OT = NULL, then : S
Lan d PAR := NU LL, and
Ret urn .
b. [ITE M at roo t ?]
If ITE M = INF O[R : Set LOC := RO OT a,f&,,.p AR := ,.{:;::~ .
/y:
Tree: Refer Q. 4.1, Page 4-2E, Unit-4.
Binary tree :
1. A binary is a non-lin ear data structu re with a maximu m of two children
for each parent.
2. Every node in a binary tree has a left and right referenc e along with the
data element .
3. The node of the top of the hierarc hy of a tree is called the root node.
Comple te binary tree: Refer Q. 4.7, Page 4-8E, Unit"".
Full binary tree :
1. A full binary tree is formed when each missing p,j,r . t',i
'tEMif
[Pop from STACK] .
P'ffl = STACK[TOPJ and TOP= TOP- 1
4-18 E (C8/rr .,
Data Structure --------- - - -- ~
--
End of step 2
6. Exit ereal s
Algorithm tor tnorder ' ;;;' ROOT) : ..
Inot-det (JNF'O, LEf"T, IUO OK' and initialize PTRJ
1. [Push NULL o-nto STA NULL and PTR :: ROOT
&t TOP .., 1, STACK l l] "'
. hi1 PTR· .,, NULL TACK]
2. ~P:'At w: e
[Push left.most path onto S d
a. Set TOP = TOP+ 1 an
STACK (TOP] = PTR
b. Set PTR = LEFT [PTRJ
End loop . d TOP= TOP -
3. Set PTR = STACK[TO ~] ~:#:NU LL
4_ Repeat steps 5 to 7 whi~]
Apply process to INFOtc .L n
!: [Right Child?] If RIGHT [Pl'R]
·
Then
a. Set PTR = RIGHT £Pffl
b. goto step 2
Endif
7. Set PI'R = STACK[
End ofStep 4 Loop ·
8. Exit
Algorith m o r traversa l:
Pos · , RIGHT, ROOT)
1. :ACK and initialize PT ·•·
TACK[l] = NULL and Pr
2. th onto STAC!{l
5 while .+f~ U
3. OP+ land ST
[Pushes Ron STACK]
4. HRIGHT [Pl'RJ ~NULL
Then
Set TOP =TOP + 1 and STACK [TOP] =RIGHT [Pl'R]
Endif
5. Set PTR = LEFT [PrRJ
End ,o f step 2 loop
6. Set PrR = STACK {TOP) and TOP :::. TOP - 1
[Pops node from STACK]
7. Repeat while PTR > O
a. Apply process to INFO [Pl'R]
b. Set PTR = STACK [TOP] and TOP = TOP- l
End loop ··
8. If P1'R < 0 Then
a. Set PTR ;:: - PTR
b. 9°-\() \\Q~'l
EN~
<3 -· E~it
Stacks and Queues
2-10 E (CSIIT-Sem-S>
th
6. Function call : Stack is used to keep information about e active
functions or subroutines.
Write an algorithm tot- t,0nverting infts: expression into
postfix expression. Trace your algorithm for infix expression•Q into
its equivalent postfix expression P, Q: A + (B C -
· t · ·. '~ ~f()
(DI IJ"F) • G) • H. ~~ -,,,.,·., .. · ,· ..,,.· ,J';,
+ +
( +( A
B +( AB
* +(* AB
C +(* ABC
+(- ABC*
( +(-( ABC*
D +(-(
ABc•n
I + (- ( /
ABC*D
E + (- ( /
ABC*DE
Data Structure
2-11 E (CS/IT-Sem-3)
~~~~~~ttiit;;:il
Algorithm for push
Page 2-3E, Unit-2.
/ A
A
AB
AB
ABC
ABC*
ABC*
(
D
! (-:,,~
~~f-J,,, ABC*D
+ ~;~ ( / ABC*D
I
E + (- (/ ABC*DE
t +< - ut ABC*DE
F + (- (It ABC*DEF
) + (- (It ABC*DEF
* + (- * ABC*DEF t/
G + (- * ABC*DEF t/ G
) + ABC*DEFt/G* -
* +* ABC*DEFt/G* -
H +* ABC*DEFt/G* - H
ABC*DEFt /G* - H*+
-'·Jlesultant postfix expression : ABC*DEFt/G* - H* +
Dat a Stru ctu re
2-I o E (CS/IT-Sem-3)
t =(+t
112 =(+t 112
)
I
= t+
=I
( =I(
2 =I( 2
X =l (x
a =l (x a
) =I X
1. n of a process in
of outcomes.
,,$al
¥v ue.
2. ppr oac h so~.~ en tfi'
epr oc , and the resu lt of each
3.
est · iter atio n.
iter a
Iter atio n allows us to sim ph , ithm by stat ing tha t ~e will
4.
rep eat cert ain step s unti l told.
sim pler because they do
5. Thi s mak es des igni ng algo rith ms icke r and
s.
not hav e to include lots of unn ece ssar y step
eat a set of inst ruct ions .
6. Iter atio n is use d in com pute r pro gram s to rep
stru ctio ns upto a specific
7. Cou nt controlled iter atio n will repe at a set ofin
iter atio n will rep eat the
num ber of times; while condition controlled
inst ruct ions unti l a specific condition is met.
What is rec urs ion ? Exp lain .
Indirect recursion occurs when one function calls another function that
2.
then calls the first function.
Suppose pis a procedure containing either a call statement to itself or
3. a call statement to a second procedure that may eventually result in a
call statement back to the original procedure P.
Then Pis called recursive procedure. So the program will not continue
4.
to run indefinitely.
5. A recursive procedure must have the following two properties :
a There must be certain criteria, called base criteria, for which the
procedure does not call itself. •. ·
b. Each time the procedure does call itself; · · oser to the
criteria.
A recursive procedure with these two '.
defined. ,
"~·
7. Similarly, a function is said t tfi>e lei~ si . defined if the function
definition refers to itself.
Program:
t~Y
else
return(n % 10 + sum (n / lO~J' .
main()
{
int s,n;
printf(" \nEnter any number-")·
8Canit"%d"& )· . '
' n'
s = sum(n);
printf("\nSum of d'1gits
· - %d"
getchO; - , s );
Data Structure 2-11 E (CS/IT-Sem-3)
return 0;
}
Time complexity :
1. Assume t~at n is a 10 digit number. The function is called 10 times as the
problem 1s reduced by a factor of 10 each time the program recurse.
ll. So, we can conclude that time taken by program is linear in terms of th
length of the digit of the input number n. e
ill. So, time ,complexity is,
~n_) ~ O(le~h of digit of (n)) where n is the numl5er whose sum of
mdividual digit is to be found. '
'"'r7
Explain all types of recursion
' '
'
~~\~
Types of recursion :
a. Direct recursion : A functio
explicit call to itself.
For example :
int foo (int x)
{ if(x <= 0)
returnx;
return foo (x -
}
b. I unction is indirec
C n.
returnx;
return bar (x) ;
}
int bar (int y)
{ return foo (y - 1) ;
}
e. Tail recursion :
1. Tail recursion (or tail-end recursion) is a special case ofrecursion in
which the last operatiop of the function, the tail call is a recursive
call. Such recursions can be easily transformed to iterations.
2. Replacing recursion with iteration, manually or automatically, can
drastically decrease the amount of stack space used and improve
efficiency.
3. Converting a call to a branch or jump in such a case is called a tail
call optimization. ·
2-18 E (CSIIT-S em-3) Stacks and Queues
For es.ample :
Consider this recursive definition of the factorial function in C :
factorial (n)
{
if( n == 0)
return 1;
return n • factorial (n - 1);
}
4. This definition is tail recursive since the recursive call to factorial is
not the last thing in the function (its result has to be multiplie d by
n).
factorial (n, accumula tor)
{
if(n == 0)
return accumula tor;
return factorial (n - 1, n * a'cc>fllulator );
}
factorial (n) ··
(
return factorial. (' - 1) ;
Th~ pending ope~ation for the recursive call is another call to fib. Therefor e,
fib 18 tree recursive .
i.
ii. m to calc
n-re cur ·
Def ine the recu rsio n. Wri te a re d non -rec ursi ve pro gram
to calc ulat e the fact oria l of the gil ·
}
int nonrecfactorial(int x
{
inti, f = 1;
for(i = 1; i <= x; · +
{
,11 l i LA.lnLc1
(3) C ➔ B
A B C
(4) A ➔ C
A
·· 5Y,1J ➔ A
,,
I
l Ik ~
Writether
}Wloi (n - 1, A, C, B);
r'
3. TOP-+-TOP + 1
4. STACK [TOP] +- DATA
5. S'IOP
POP operation: In pop remove an element from
every pop operation top o ecremented by -1. ·
Algorit hm:
POP (STACK, T .
1. IfT CKUNDERFL
2. S
3.
4.
#include<stdio .h>
#include<conio.h>
I #define MAX 50
int stack [MAX + 1], top = 0 ;
voidmain()
{
clrscr( );