Professional Documents
Culture Documents
display();
}
}
return 0 I·
}
P
p re ss 1 ·. 1n se rt an el
em en
P re ss 2: D el et e an el em t
re ss 3: D is p la y th e en t
E n te r el em en t
y o u r ch o ic e:
3
Elements i n a Queue
P re ss 1 ·. I n se ar e :123
rt an el em en t ,
P re ss 2: D el et e
an el em en t
:r e s s 3: D is p la
y th e el em en t
n te r y o u r ch o ic
e: 4
~
~
~
w--: '
(ri:a
-....-....
.,..
~
►
~
~
~
6r
~
~
.D,;.J. ,r._ --11'"°~...-o.M
- " - '- ( ) ~ o x f " " ' ~
.fYV'll' \~..>'ll< ~"'4 lb,
'
~
~""
o,J\J.. &J',,r,4
~~-
<To .u>9u"\e.. o- c, --V'"°fo.m. .:to . .f'Cll'(>lcm\~
~ ~cct,'e,- ~ ~ ~& .:to J'M~I~
A~t:1t ()Q.\1'i\ M ..
- ~Q..p \~ ~
,uq, ,_ : \ Y\).~cfu"'u_ .0.. ~~ ,..XO.~ "°""'"1 "\-
.; (
,oo,
~~ ~ ~
~?-9-
void pop( )
{
if (top == - 1)
prin tf( "Underflow") ;
else
top = top - 1;
}
void show ()
{
for (i = top; i >= 0 ; i --)
{
prin tf( "%d\n " , stac k[i] );
}
if (top == - 1)
{
prin tf ( "Stack is empty" );
}
}
void main ()
{
1.P ush
2.Pop
3 . Sho l'I
LL Ex it
Enter your choice
l
Enter the value?l
Chose one from the below options.• ·
l.Push
2.Pop
3.Show
q.fxit
Enter your choice
1
Enter the value?2
Chose one from the below options . . .
1.Push
2.Pop
3.Show
q,Exit
Enter your choice
1
Enter the value?3
Chose one from the below options . . -.
I.Push
2.Pop
3.Show
LL Exit
Enter your choice
2
Chose one from the below options ...
l . Push
2.Pop
3 . ShOl'/
q,fxit
Enter your choice
3
2
1
Chose one from th e below op t·
1.Push ion s
2.Pop
3 .Show
ti.E xit
. Enter yo ur cho ice
, q
Ex itin g ... /
y
~) -\) SI O N;
-ro ..u ,~Q .. a.. c.. r ro.y(I .:,;;u .fr n " \ ~ ~~
~ ~ I o.,,,d ~ J' M '( '\~
!h .4 '~ o. .X I~
~ ..,0 f>~ oe -,i, . JJ ..~ -1'\AM"- ' 9o p 0-¥\d... ~ ~
~ ~ ~~--
. ··-
. .. - .
Al"1 '.
\0 ..u'.>"-'~ ~ CL
C -I'~"'" -"' :U> J,m'l;J\ex-f\RM l e ~
0
~ OY\~ ~ ~:n:xNVvl o-1. .::t a.~tM ~ vJ. .
0
_tQ,C.W'\A, u.9-A .
WO.
-
#include <stdio.h >
#include <stdlib. h>
struct node
{
int item;
struct node *left;
struct node *right;
};
newNode->right = NULL ;
return newNode;
}
int main ()
{
struct node *root = createNode ( l );
insertLeft (root, 2);
insertRight (root, 3);
insertLeft (root->left, 4);
,
)
:, _________________ ___________
;)
OO"rPUT
PROBLEMS ourour TER MINA L DEBUG CONSOLE I}) Code + v 8 ti)) < X
PS C:\Users\Adith\Dropbox\Sem
rs\Adith\Dropbox\Sem 3\DS-C\C ! \DS-C\Code-playground> cd ''c:\Use
11
nary_tree c _ b" 0 e-playground\ if ( $?) { g•b"
Inorder t;aversa:nary_tree}
0 ; if ( $? ) { . \bin;ry_tre~ } cc l
q ->2 ->1 ->3 ->
Preorder traversal
1 ->2 ->1' ->3 ->
Postorder traversal
1' ->2 ->3 ->1 ->
.
/
r
(!I)(~ ~~·
~ '.I- : \ru'\-\'Cl.Jiu .A .-u-ru ck<-""'-'- \<)-r a. ..w,c9-,-,
..w~~ .do.;lo.._. ~ ~}~~
~ 3 ._ \~ifo.Qf ~ ..o.. ~u:lCJ\1\.- ~
~ ol ~"YO.tJ'e>JJ oi. ,A).JrtO'
~
.J,.~
-
~~o~
OI'\ .
'w
~ =
~().)J~~-
6,
°'
0-
~
;>------------------------_J
. -- _.. .. ,
#i nc lu de <s td io .h >
#i nc lu de < st dl ib .h >
st ru ct node
{
in t key;
st ru ct node *t ef t,
*r ig ht ;
};
st ru ct node *newNode
( in t ite m )
{ st ru ct node *temp = (s
tr uc t node *) malloc (s
iz eo f( st ru ct node )) ;
temp->key = m ;
te m p- > le ft =ite
temp->right =NULL ;
re tu rn temp;
}
void in or de r( st ru ct
node *r oo t )
{
if (r oo t != NULL )
{
in or de r( ro ot -> le ft );
p ri n tf ( 11 %d -> ", ro ot
->key);
in or de r( ro ot -> ri g h t)
;
}
}
st ru ct node *i n se rt (s
tr u ct node *node , in
t key)
{
if ( node NULL )
re tur n newNode (key );
re tu rn node ;
}
struct node *minValueNode (struc t node *node)
{
struct node *curre nt = node ;
return curren t;
}
if (root= = NULL) , I ,
return root ;
if ( key< root->key)
root-> left = deleteNode (root- >left, key); '
else if (key> root->key) ·
root-> right = deleteNode (root- >righ t, key);
else
{
root->key = temp->key;
int main ()
{
struct node *root = NULL ;
int val;
printf( "Enter the elements to be entered in SST : \ n");
while ( 1)
{
scanf( "%d " , &val);
root = insert (root, val);
if (val == 0)
{
deleteNode (root, 0);
printf( "Inorder Traversal:" );
inorder(root);
printf ( "\n Enter the value to be deleted? \t ");
scanf( 11 %d 11 1 &val);
root = deleteNode (root, val);
printf( "Inorder Traversal after elet on: \n ");
inorder(root);
break ;
}
}
~l°a.VtU ,o. ~-
'1'"°-N ~ ~~c>,w,f°\W- .
M~ b «i)ehlll- ~ ~ch .t~ ~ v.:>o..v$ w~
~~ A__Ai'~ ~ ~
#include <stdio.h>
#include <s tdl ib. h>
str uc t Node
{
int key;
str uc t Node *le ft;
str uc t Node *ri gh t;
int hei gh t;
} j
y- >h ei gh t = max ( he ig
ht ( y- >l ef t) , he ig ht (
x- >h ei gh t = max ( he ig y- >r ig ht )) + 1 ;
ht (x -> le ft ), he ig ht (x
-> rig ht )) + 1;
re tu rn x;
}
st ru ct Node *l ef tR ot
at e ( st ru ct Node *X)
{
st ru ct Node *Y = x- >r
ig ht ;
st ru ct Node *T2 = y-
>l ef t;
y- >l ef t = x;
x- >r ig ht = T2;
x- >h ei gh t = max ( he ig
ht ( x- >l ef t) , he ig ht (x->
y- >h ei gh t = max( he ig ri gh t) ) + l ;
ht (y -> le ft ), he ig ht (y ->
ri gh t) ) + l ;
re tu rn y;
getBalance (s tr uc t Node
*N)
{
if ( N == NULL )
re tu rn 0 ;
re tu rn he ig ht ( N-> le ft
) - he ig ht ( N-> ri gh t) ;
}
st ru ct Node *i ns er tN od
e ( st ru ct Node *node ,
in t key )
{
if ( node == NULL )
re tu rn ( newNo e( key ))
;
return node ;
}
return current;
}
~
if ( key < root->key)
root->left = deleteNode (root->left, key ); ~
f;
'-------- - - -- - - - - - - -- ---------__J ~
else if (key> root->key)
root->right = deleteNode (root->right, key);-
else
{
if ((root->left == NULL) I I (root->right == NULL))
{
struct Node *temp = root->left ? root->left : root->right j
if (temp == NULL)
{
temp = root j
root = NULL ;
}
else
*root = *temp;
free (temp)j
}
else
{
struct Node *temp = minValueNode(r oot->right);
root->key = temp->keyj
if (root == NULL)
return root j
{
>ri gh t);
roo t-> rig ht = rig htR ota te (ro ot-
}
ret urn lef tR ota te (ro ot ); ry,
~In
ret urn roo t ; ~
}
.J:
void pri ntP reO rde r( str uc t Nod
e *ro ot ) _ !l
{
~Ill
if (ro ot != NULL)
{
pri ntf ( %d , roo t->l<ey); .
11 11
9~
pri ntP reO rde r(r oo t-> lef t);
pri ntP reO rde r(r oo t-> rig ht) ;
~ '
} ~ Jt
•
}
~I
~ I
int main ()
{
Cc ~
str uc t Node *ro ot = NULL ;
int n =0; C ,
pri ntf ( "E nte r the elements in
AVL tre e: \ n") ; ; ;
whil e (n != - 1)
{
sca nf( 11 %d 11 , &n);
roo t = ins ert No de (ro ot, n);
if (n == 0)
{
pri ntP reO rde r(r oo t);
n = - 1;
an element? \n l.y es \n .no \n ");
pri ntf (11 Do11 you want to de let e
11
break ;
}
}
re tu rn 0;
}
0V T PU 1:
TER MINAL DEBUG CONSOLE ill Code + v 8 lfilJ < X
ro pb ox \S em 3\ 0S -C \Code-playground> cd
PS C:\Users\Adith\D ode-playground \
11
ox \S em 3\D S- C \C
"e : \Users\Adith\Dropb l2 } ; if ($? ) { .\ av
l2 }
e av l2 .e -o av
if ( $? ) { ge
in AVL tr ee :
E nt er th e elements
1
2
3
I
4
I 0
2 1 0 3 4
te an element?
Do you want to de le
1. ye s
2.no
1
1
want to de le te ?
What element do you
A ft er de le ti on :
2034 /
V
CONCJ..USION : J 'm p \~ AV).,
-r~ ::tt-u,
ru\cl . ."b
.
e « .e .~ -
flF AtM.:
-
~U:n.Ot\T\-\M,
.
~
4-t~ ~ ~ ,vttfo-.~~ 0- ~~ ~rttOY'\ ()A~
o...ncA ~ ~~ .
.c,
~
,0
a..
~
tX'f'lo..AY\ ~~~J ~ \C'I{)~
l.lX~f> : \n
C
~ -\"'I «..Jl •
vo.>.f~ ~ ..,l'(\ OX\
~~~
.Ul2..(J i ·. c.a.,l1
~ t,I,. ~ct,E)(I .
Me..p ~ ·. ~o .
, - - - - - - - - - - - -- - - - -- - - - -- -
c
G,,-
#define MAX 3
#define MIN 2
struc t BTreeNode
{
int val[MAX + 1], count;
struc t BTreeNode *link [MAX + 1];
};
void insert Node ( int val , int pos , struc t BTreeNode *node ,
struc t BTreeNode *Child )
{
int j = node-> count ;
while (j > pos )
{
node-> val[j + l ] = node-> val[j ];
node-> link[ j + l ] = node-> link[ j];
j -- ;
}
node-> val[j + l ] = val ;
node-> link[ j + l ] = child ;
node-> count ++ ;
}
void splitN ode ( int val , int *pval , int pos , struc t BTreeNode
*node ,
struc t BTreeNode *Child , struc t BTreeNode **n ewNode )
{
int median I 3• I•