Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

download Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

of 82

Transcript of Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    1/82

    Scilab Textbook Companion for

    Fundamentals Of Data Structure In C

    by S. Sahni , S. Anderson-freed And E.

    Horowitz1

    Created byAakash Yadav

    B-TECHComputer Engineering

    Swami Keshvanand Institute of Technology

    College TeacherRicha Rawal

    Cross-Checked byLavitha Pereira

    August 17, 2013

    1Funded by a grant from the National Mission on Education through ICT,http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilabcodes written in it can be downloaded from the Textbook Companion Projectsection at the website http://scilab.in

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    2/82

    Book Description

    Title: Fundamentals Of Data Structure In C

    Author: S. Sahni , S. Anderson-freed And E. Horowitz

    Publisher: University Press (India) Pvt. Ltd., New Delhi

    Edition: 2

    Year: 2008

    ISBN: 9788173716058

    1

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    3/82

    Scilab numbering policy used in this document and the relation to theabove book.

    Exa Example (Solved example)

    Eqn Equation (Particular equation of the above book)

    AP Appendix to Example(Scilab Code that is an Appednix to a particularExample of the above book)

    For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 meansa scilab code whose theory is explained in Section 2.3 of the book.

    2

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    4/82

    Contents

    List of Scilab Codes 4

    1 Basic concepts 6

    2 Arrays and Structures 16

    3 Stacks and Queues 19

    4 Linked lists 31

    5 Trees 44

    6 Graphs 58

    7 Sorting 68

    8 Hashing 76

    9 Priority Queues 78

    3

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    5/82

    List of Scilab Codes

    Exa 1.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 6Exa 1.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 7Exa 1.3 example . . . . . . . . . . . . . . . . . . . . . . . . . . 7Exa 1.4 example . . . . . . . . . . . . . . . . . . . . . . . . . . 8Exa 1.5 example . . . . . . . . . . . . . . . . . . . . . . . . . . 8Exa 1.6 example . . . . . . . . . . . . . . . . . . . . . . . . . . 9Exa 1.7 example . . . . . . . . . . . . . . . . . . . . . . . . . . 9Exa 1.8 example . . . . . . . . . . . . . . . . . . . . . . . . . . 10Exa 1.9 example . . . . . . . . . . . . . . . . . . . . . . . . . . 10Exa 1.10 example . . . . . . . . . . . . . . . . . . . . . . . . . . 11Exa 1.11 example . . . . . . . . . . . . . . . . . . . . . . . . . . 12Exa 1.12 example . . . . . . . . . . . . . . . . . . . . . . . . . . 13Exa 1.13 example . . . . . . . . . . . . . . . . . . . . . . . . . . 13

    Exa 1.14 example . . . . . . . . . . . . . . . . . . . . . . . . . . 14Exa 1.15 example . . . . . . . . . . . . . . . . . . . . . . . . . . 14Exa 2.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 16Exa 2.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 16Exa 2.3 example . . . . . . . . . . . . . . . . . . . . . . . . . . 17Exa 1.1.b example . . . . . . . . . . . . . . . . . . . . . . . . . . 19Exa 3.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 21Exa 3.1.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 21Exa 1.2.b example . . . . . . . . . . . . . . . . . . . . . . . . . . 22Exa 3.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 24Exa 1.3.a example . . . . . . . . . . . . . . . . . . . . . . . . . . 25

    Exa 3.3 example . . . . . . . . . . . . . . . . . . . . . . . . . . 27Exa 4.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 31Exa 4.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 31Exa 4.3 example . . . . . . . . . . . . . . . . . . . . . . . . . . 36

    4

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    6/82

    Exa 4.4 example . . . . . . . . . . . . . . . . . . . . . . . . . . 39

    Exa 5.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 44Exa 5.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 47Exa 5.3 example . . . . . . . . . . . . . . . . . . . . . . . . . . 51Exa 5.4 example . . . . . . . . . . . . . . . . . . . . . . . . . . 55Exa 6.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 58Exa 6.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 59Exa 6.3 example . . . . . . . . . . . . . . . . . . . . . . . . . . 61Exa 6.4 example . . . . . . . . . . . . . . . . . . . . . . . . . . 62Exa 6.5 example . . . . . . . . . . . . . . . . . . . . . . . . . . 63Exa 6.6 example . . . . . . . . . . . . . . . . . . . . . . . . . . 64Exa 6.7 example . . . . . . . . . . . . . . . . . . . . . . . . . . 66

    Exa 7.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 68Exa 7.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 69Exa 7.3 example . . . . . . . . . . . . . . . . . . . . . . . . . . 69Exa 7.4 example . . . . . . . . . . . . . . . . . . . . . . . . . . 70Exa 7.5 example . . . . . . . . . . . . . . . . . . . . . . . . . . 71Exa 7.6 example . . . . . . . . . . . . . . . . . . . . . . . . . . 72Exa 7.7 example . . . . . . . . . . . . . . . . . . . . . . . . . . 73Exa 7.8 example . . . . . . . . . . . . . . . . . . . . . . . . . . 74Exa 8.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 76Exa 8.2 example . . . . . . . . . . . . . . . . . . . . . . . . . . 77

    Exa 9.1 example . . . . . . . . . . . . . . . . . . . . . . . . . . 78

    5

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    7/82

    Chapter 1

    Basic concepts

    Scilab code Exa 1.1 example

    1 // t o do s o r t i n g o f no s . c on t ai n ed i n a l i s t2 function [ ] = s o r t i n g ( a )

    3 i = 1 ;

    4 [ j , k ] = size ( a ) ;

    5 j = i ;

    6 for i = 1 : k - 1

    7 for j = i : k8 if a ( i ) > a ( j ) then

    9 z = a ( i ) ;

    10 a ( i ) = a ( j ) ;

    11 a ( j ) = z ;

    12 end

    13 end

    14 end

    15 for i = 1 : k

    16 disp ( a ( i ) ) ;

    17 end18

    19 funcprot ( 0 ) ;

    20 e n d f u n c t i o n

    21 // c a l l i n r o u t i n e

    6

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    8/82

    22 a =[5 7 45 23 78]

    23 sort = s o r t i n g ( a )

    Scilab code Exa 1.2 example

    1 // t o do b i na r y s e a r ch . .2 function [ ] = s e a r c h ( a )

    3 i = 1 ;

    4 [ j , k ] = size ( a ) ;

    5 for i = 1 : k6 if z = = a ( i ) then

    7 printf ( \nFOUND and i nd e x no . i s=%d\ t , i ) ;

    8 end

    9 end

    10 funcprot ( 0 ) ;

    11 e n d f u n c t i o n

    12 // c a l l i n r o u t i ne13 a = [5 7 45 28 99]

    14 z = 4 5

    15 b i n a r y = s e a r c h ( a )

    Scilab code Exa 1.3 example

    1 // t o do b i na r y s e a r ch . .2 function [ ] = s e a r c h ( a )

    3 i = 1 ;

    4 [ j , k ] = size ( a ) ;

    5 for i = 1 : k6 if x = = a ( i ) then

    7 printf ( \nFOUND and i nd e x no . i s=%d\ t , i ) ;

    8 end

    7

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    9/82

    9 end

    10 funcprot ( 0 ) ;11 e n d f u n c t i o n

    12 // c a l l i n r o u t i ne13 a = [5 7 45 28 99]

    14 x = 4 5

    15 b i n a r y = s e a r c h ( a )

    Scilab code Exa 1.4 example

    1 / / e xa mp le 1 . 42 // p er mu ta ti on o f a s t r i n g o r c h a r ac t e r a r r ay . . .3 c l e ar a ll ;

    4 clc ;

    5 x =[ a b c d ]6 printf ( \n p o s s i b l e p er m ut at io n o f g i v en s t r i n g a re \n

    ) ;7 y = p e r m s ( x ) ;

    8 disp ( y ) ;

    Scilab code Exa 1.5 example

    1 // ex ampl e 1 . 52 / / ADT( A b st r ac t Data t yp e ) d e f i n a t i o n o f n a t u r a l

    number .3 function [ ] = A D T ( x )

    4 printf ( ADT n a t u r a l no . i s ) ;5 printf ( \nOBJECTS : a n o r d e r e d s u b r a n g e o f t h e

    i n t e g e r s s t a r t i n g a t z er o a nd ) ;6 printf ( e n d i n g a t t h e maximun i n t e g e r ( INT MAX )

    on t h e c om pu te r ) ;7 I N T _ M A X = 3 2 7 6 7 ;

    8 if x = = 0 // N at ur al N umbe r Z e r o ( )

    8

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    10/82

    9 printf ( \n ,0);

    10 end11 if x = = I N T _ M A X then //N a t u r a l N u m b e r S u c c e s s o r ( x )

    12 printf ( \nans . i s=%d , x ) ;13 else

    14 printf ( \nans . i s=%d , x + 1 ) ;15 end

    16 e n d f u n c t i o n

    17 // c a l l i n r o u t i n e18 x = 5 6

    19 y = A D T ( x ) ;

    Scilab code Exa 1.6 example

    1 / / f u n c ti o n abc a c c e p t in g o nl y t h r e e s i mp l e v a r i a b l e sg i v e n t he f u n c t i o n ha s

    2 / / o n l y f i x e d s a c e r e q u ir e m e nt . .3 function [ ] = a b c ( a , b , c )

    4 x = a + b + c * c +( a + b - c ) / ( a + b) + 4 . 0 0;

    5 disp ( x ) ;6 funcprot ( 0 ) ;

    7 e n d f u n c t i o n

    8 .... // c a l l i n g r o u t i n e9 a = [ 1 ] , b = [ 2 ] , c = [ 3 ]

    10 a b c ( a , b , c )

    Scilab code Exa 1.7 example

    1 // To add a l i s t o f no . u s i ng a r ra y .2 function [ ] = a d d ( a )

    3 r e s u l t = sum ( a ) ;

    4

    9

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    11/82

    5 printf ( a d d i t i o n o f no . on t h e l i s t i s =%d ,

    r e s u l t ) ;6 funcprot ( 0 ) ;

    7 e n d f u n c t i o n

    8 // c a l l i n g r o u t i n e9 a =[5 2 7 8 9 4 6]

    10 r = a d d ( a )

    Scilab code Exa 1.8 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n E xa mp le 1 . 8 ) ;4 a = [ 2 ; 5 ; 4 ; 6 4 ; 7 8 ]

    5 i = 1 ;

    6 x = 1 ; . . . . . . . . . . . . // i n i t i a l i s i n g sum e q ua l s t o one .7 c = 1 ; . . . . . . . . . . . . . . / / i n i t i a l i s i n g c o u n t e q u a l s t o

    one .8 while i

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    12/82

    4 a = [1 2 3;4 5 6];

    5 b = [7 8 9 ;10 11 12 ];6 x =matri x ( a , 3 , 2 ) ; . . . . . . . . // no . o f r ow s =3 , no . o f c o l .=2.

    7 y =matri x ( b , 3 , 2 ) ; . . . . . . . . // no , o f r ow s =3 , no . o f c o l. = 2 .

    8 printf ( m a t r i x x= ) ;9 disp ( x ) ;

    10 printf ( m a t r i x y= ) ;11 disp ( y ) ;

    12 [ p , q ] = size ( x ) ;

    13 i = 1 ;

    14 j = 1 ;15 c = 1 ;

    16 for i = 1 : p

    17 for j = 1 : q

    18 z ( i , j ) = x ( i , j ) + y ( i , j ) ; . . . . . . . . . . //summing twom a t r i c e s

    19 c = c + 1 ; . . . . . . . . . . . / / s t e p c o un t20 end

    21 end

    22 printf ( \n R e s ul t an t m at ri x a f t e r a d d i ti o n = ) ;23 disp ( z ) ; . . . . . . . . . . . . .

    // d i s p l a y i n sum o f two m a t r i ce s.24 printf ( \n s t e p c o un t i s =%d , ( c - 1 ) ) ;

    Scilab code Exa 1.10 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n E xa mp le 1 . 1 0 ) ;4 / / f u n c t i o n t o sum a l i s t o f n umb ers .5 function [ ] = a d d ( )

    6 printf ( \n no . i n t he l i s t a r e ) ;7 disp ( a ) ;

    11

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    13/82

    8 x = sum ( a ) ;

    9 printf ( \n Re su lt=%d , x ) ;10 funcprot ( 0 ) ;11 e n d f u n c t i o n

    12 // c a l l i n g r o u t i n e .13 a =[2 5 6 7 9 1 6 3 7 45]

    14 a d d ( )

    Scilab code Exa 1.11 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n E xa mp le 1 . 1 1 ) ;4 / / M at ri x a d d i t i o n .5 a = [1 2 3;4 5 6];

    6 b = [7 8 9 ;10 11 12 ];

    7 x =matri x ( a , 3 , 2 ) ; . . . . . . . . // no . o f r ow s =3 , no . o f c o l .=2.

    8 y =matri x ( b , 3 , 2 ) ; . . . . . . . . // no , o f r ow s =3 , no . o f c o l. = 2 .

    9 printf ( m a t r i x x= ) ;10 disp ( x ) ;

    11 printf ( m a t r i x y= ) ;12 disp ( y ) ;

    13 [ p , q ] = size ( x ) ;

    14 i = 1 ;

    15 j = 1 ;

    16 for i = 1 : p

    17 for j = 1 : q

    18 z ( i , j ) = x ( i , j ) + y ( i , j ) ; . . . . . . . . . . //summing two

    m a t r i c e s19 end20 end

    21 printf ( \n R e s ul t an t m at ri x a f t e r a d d i ti o n = ) ;22 disp ( z ) ; . . . . . . . . . . . . . // d i s p l a y i n sum o f two m a t r i ce s

    12

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    14/82

    .

    Scilab code Exa 1.12 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( E xa mp le 1 . 1 2 ) ;4 / / [ BIG o h ] f ( n )=O( g ( n ) ) . ( b i g o h n o t a t i o n ) .5 printf ( \n \n 3n+2=O(n ) as 3n+2=2. ) ;

    6 printf ( \n \n 3n+3=O(n ) as 3n+3

    =3. ); . . . . . . . . . . . . // O( n) i s c a l l e d l i n e a r .7 printf ( \n \n 3n+2=O(n ) as 100n+6=10. ) ;8 printf ( \n \n 10n2+4n+2=O(n 2 ) as 10n2+4n+2=5. ) ; . . . . . . . . . . //O( n 2 ) i s c a l l e dq u a d r a t i c .

    9 printf ( \n \n 1 0 0 0 n 2+ 10 0 n6=O( n 2) as 1000n2+ 100n6=100.) ;

    10 printf ( \n \n 62n+n2=4) ;11 printf ( \n \n 3n+3=O(n 2 ) as 3n+3=2) ;

    12 printf ( \n \n 10n2+4n+2=O(n 4 ) as 10n2+4n+2=2. ) ;

    13 printf ( \n \n 3 n+2 i s no t O( 1 ) a s 3n+2 i s l e s s t hano r e q ua l t o c f o r any c on st an t c and a l l n , n>=n0 . ) ; . . . . . . . . . . . . // O( 1 ) means c om pu ti ng t im e i sc o n s t a n t .

    14 printf ( \n \n 1 0n 2+4 n+2 i s n o t O( n ) ) ;

    Scilab code Exa 1.13 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n E xa mp le 1 . 1 3 ) ;

    13

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    15/82

    4 printf ( \n \n [ Omega ] f ( n )=ome ga ( g ( n ) ) ) ;

    5 printf ( \n \n 3n+2=omega( n) as 3n+2>

    =3n f o r n>

    =1) ;6 printf ( \n \n 3n+3=omega( n) as 3n+3>=3n f o r n>=1) ;7 printf ( \n \n 100n+6=ome ga ( n ) as 100n+6>=100n f o r n

    >=1) ;8 printf ( \n \n 10n2+4n+2=omega (n 2 ) as 10n2+4n+2>=n

    2 f o r n>=1) ;9 printf ( \n \n 62n+n2= omega( n) as 62n+n2>=2n

    f o r n>=1) ;10 printf ( \n \n 3n+3=omega ( 1 ) ) ;11 printf ( \n \n \ t [ Omega] f ( n )=omega ( 1 ) ) ;

    Scilab code Exa 1.14 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n E xa mp le 1 . 1 4 ) ;4 printf ( \n \n [ T h et a ] f ( n )= t h e t a ( g ( n ) ) ) ;5 printf ( \n \n 3 n+2= t h e t a ( n ) a s 3 n+2>=3n f o r a l n>=2

    ) ;

    6 printf ( \n \n 3n+3= t h e t a ( n ) ) ;7 printf ( \n \n 10n2+4n+2=th et a (n 2 ) ) ;8 printf ( \n \n 62n+n2= t he t a ( 2 n) ) ;9 printf ( \n \n 3n+2 i s no t t h et a ( 1 ) ) ;

    10 printf ( \n \n 3 n+3 i s n ot t h e ta ( n 2) \n ) ;11 printf ( \n \n The Theta n o t at i on i s more p r e c i s e

    t ha n b ot h b i g oh and omega n o t a i o n ) ;

    Scilab code Exa 1.15 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n \ t E xa mp le 1 . 1 5 ) ;

    14

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    16/82

    4 / / how v a r i o u s f u n c t i o n s grow w it h n , p l o t t i n g o f

    v a r i o u s f u n c t i o n s i s b ei ng shown .5 / / l i k e f u n c t i o n 2 n g ro ws v er y r a p i d l y w i th n . andu t i l i t y o f p ro g ra ms w i th e x p o n e n t i a l c o m p l e x i t yi s l i m i t e d t o s m a l l n ( t y p i c a l l y n , f > ) ;12 printf ( \n \n X a x i s i s r e p r e s e n t e d by v al u e s o f

    n a n d Ya x i s i f r e p re s e n te d by f ) ;

    15

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    17/82

    Chapter 2

    Arrays and Structures

    Scilab code Exa 2.1 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n e xa mp le 2 . 1 ) ;4 / / p r i n t i n g o ut v a l ue s o f t h e a rr ay .5 a = [3 1 40 57 46 97 8 4] ;

    6 printf ( \ n va l u es a re : \ n ) ;

    7 disp ( a ) ;

    Scilab code Exa 2.2 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n E xa mp le 2 . 2 \ n ) ;4 // S t r i n g i n s e r t i o n .5 s = a u t o ; . . . . . . . . . . . . . . . // 1 s t s t r i n g o r c h ar a c t e r

    a r r a y .6 x = m o b i l e ; . . . . . . . . . . . . . . . // 2nd s t r i n g o r c h a r ac t e r

    a r r a y .

    16

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    18/82

    7 z = s + x ; . . . . . . . . . . // c o nc a t e n at i on o f 2 s t r i n g s .

    8 printf ( \t s t r i n g s= ) ;9 disp ( s ) ;10 printf ( \t s t r i n g x= ) ;11 disp ( x ) ;

    12 printf ( \ t c o nc a t en a t e d s t r i n g z= ) ;13 disp ( z ) ; . . . . . . . . // d i s p a l y i n g c o nc a te n at e d s t r i n g .

    Scilab code Exa 2.3 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n Ex am pl e 2 . 3\ n ) ;4 // c om pa r i si on o f 2 s t r i n g s .5 a = hak unah ; . . . . . . . . . . // s t r i n g 1 .6 b = m a t a t a ; . . . . . . . . . . . . . // s t r i n g 2 .7 disp ( a & b r e s p e c t i v e l y a re = ) ;8 disp ( a ) ;

    9 disp ( b ) ;

    10 disp ( c o mp ar in g s t r i n g s ) ;

    11 z = s t r c m p ( a , b ) ; . . . . . . . . . // c om pa r i s io n o f 2 s t r i n g s .12 if ( z = = 0 )

    13 printf ( \nMATCHED\n ) ; . . . . . . . // i f s t r i n g sm atch ed s tr cm p r e t u r n s 0 .

    14 else

    15 printf ( \nNOT MATCHED\n ) ; . . . . . . . . . // i f s t r i n gd oe sn t m at ch ed s t rc mp r e t u r n s 1.

    16 end

    17 q = a k a s h ;18 w = a k a s h ;

    19 disp ( q & w r e s p e c t i v e l y a re= ) ;20 disp ( q ) ;21 disp ( w ) ;

    22 disp ( c o mp ar in g s t r i n g s ) ;23 x = s t r c m p ( q , w ) ;

    17

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    19/82

    24 if ( x = = 0 )

    25 printf ( \nMATCHED\n ) ; . . . . . . . // i f s t r i n g sm atch ed s tr cm p r e t u r n s 0 .26 else

    27 printf ( \nNOT MATCHED\n ) ; . . . . . . . . . // i f s t r i n gd oe sn t m at ch ed s t rc mp r e t u r n s 1.

    28 end

    18

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    20/82

    Chapter 3

    Stacks and Queues

    Scilab code Exa 1.1.b example

    1 // E x e r c i s e q u e s t i o n 2 :2 / / I m p l e m e n ti n g P ush And Pop F u n c t i o n s :3 function [ y , s t a 1 ] = e m p t y ( s t a )

    4 y = 0 ;

    5 s t a 1 = 0 ;

    6 if ( s t a . t o p = = 0 )

    7 y = 0 ;8 else

    9 y = 1 ;

    10 end

    11 s t a 1 = s t a

    12 e n d f u n c t i o n

    13

    14 function [ s t a ] = p u s h ( s t a c , e l e )

    15 sta=0;

    16 if ( e m p t y ( s t a c ) = = 0 )

    17 s t a c . a = e l e ;18 s t a c . t o p = s t a c . t o p + 1 ;

    19 else

    20 s t ac . a = [ s t a c . a ( : , :) e le ]

    21 s t a c . t o p = s t a c . t o p + 1 ;

    19

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    21/82

    22 end

    23 disp ( s t a c ) ;24 s t a = s t a c ;

    25 funcprot (0)

    26 e n d f u n c t i o n

    27

    28 function [ e l e , s t a ] = p o p ( s t a c k )

    29 e l e =1 ;30 if ( e m p t y ( s t a c k ) = = 0 )

    31 disp ( S t a c k U n de r fl o w ) ;32 break ;

    33 else

    34 e l e = s t a c k . a ( s t a c k . t o p ) ;35 s t a c k . t o p = s t a c k . t o p - 1 ;

    36 if ( s t a c k . t o p ~ = 0 )

    37 b = s t a c k . a ( 1 ) ;

    38 for i 2 = 2 : s t a c k . t o p

    39 b = [ b ( : , :) s t ac k . a ( i 2 ) ] ;

    40 end

    41 s t a c k . a = b ;

    42 else

    43 s t a c k . a = 0 ;44 end

    45 end

    46 disp ( s t a c k ) ;

    47 s t a = s t a c k ;

    48 e n d f u n c t i o n

    49 global stack

    50 / / C a l l i n g R o ut i n e :51 s t a c k = s t r u c t ( a ,0 , top ,0);52 stack= push( stack ,4);

    53 stack= push( stack ,55);

    54 stack= push( stack ,199);

    55 stack= push( stack ,363);56 [ e l e , s t a c k ] = p o p ( s t a c k ) ;

    57 disp ( s t a c k , A f t er t he abov e o p e r a ti o n s s t ac k i s : ) ;

    20

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    22/82

    Scilab code Exa 3.1 example

    1 c l e ar a ll ;

    2 clc ;

    3 printf ( \n ex a mp l e 3 . 1 \ n ) ;4 // s t a c k s f o l l o w LIFO i . e l a s t i n f i r s t o ut . s o

    p r i n t i n g ou t a rr ay from l a s t t o f i r s t w i l l besame a s s t a c k .

    5 a = [ 1 2 ; 3 5 ; 1 6 ; 4 8 ; 2 9 ; 1 7 ; 1 3 ]

    6 i = 7 ;

    7 printf ( \t s t a c k = ) ;8 while i >0

    9 printf ( \n\t%d , a ( i ) ) ;10 i = i - 1 ;

    11 end

    Scilab code Exa 3.1.2 example

    1 / / U n ol ve d E xa mp le 2 :2 c l e ar a ll ;

    3 clc ;

    4 disp ( U n s o lv e d e xa mp le 2 ) ;5 / / I m p le m en t i ng S t a ck u s i n g u n io n :6 function [ s t a c k ] = s t a _ u n i o n ( e t y p e , a )

    7 s t a c k e l e m e n t = s t r u c t ( e t y pe , e t y p e ) ;8 [ k , l ] = size ( a ) ;

    9 select s t a c k e l e m e n t . e t y p e ,

    10 case i n t then11 a = int32 ( a ) ;

    12 s t a c k = s t r u c t ( top ,l , i t e ms , a ) ; ,13 case f l o a t then14 a = double ( a ) ;

    21

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    23/82

    15 s t a c k = s t r u c t ( top ,l , i t e ms , a ) ; ,

    16 case cha r then17 a = string ( a ) ;18 s t a c k = s t r u c t ( top ,l , i t e ms , a ) ; ,19 end

    20 disp ( s t a c k , S t a ck i s : ) ;21 e n d f u n c t i o n

    22 a = [ 32 1 2. 34 2 32 3 2. 3 22 ]

    23 s t a c k = s t a _ u n i o n ( f l o a t ,a )24 s t a c k = s t a _ u n i o n ( i n t ,a )25 s t a c k = s t a _ u n i o n ( cha r ,a )

    Scilab code Exa 1.2.b example

    1 / / U n s o l v e d E xa mp le 12 c l e ar a ll ;

    3 clc ;

    4 disp ( e x a m pl e 3 . 7 ) ;5 //To d et er mi ne t h e s y n t a c t i c a l y v a l i d s t r i n g6 function [ l ] = s t r l e n ( x )

    7 i = 1 ;8 l = 0 ;

    9 [ j , k ] = size ( x )

    10 for i = 1 : k

    11 l = l + length ( x ( i ) ) ;

    12 end

    13 e n d f u n c t i o n

    14 function [ ] = s t r i n g v a l i d ( s t r )

    15 s t r = string ( s t r ) ;

    16 s t a c k = s t r u c t ( a , 0 , top ,0);

    17 l 1 = s t r l e n ( s t r ) ;18 v a l i d = 1 ;

    19 l = 1 ;

    20 while ( l < = l 1 )

    21 if ( s t r ( l ) = = ( | s t r ( l ) = = [ | s t r ( l ) = = { )

    22

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    24/82

    22 if ( s t a c k . t o p = = 0 )

    23 s t a c k . a = s t r ( l ) ;24 s t a c k . t o p = s t a c k . t o p + 1 ;

    25 else

    26 s t ac k . a = [ s t ac k . a ( : , : ) s tr ( l ) ] ;

    27 s t a c k . t o p = s t a c k . t o p + 1 ;

    28 end

    29 disp ( s t a c k ) ;

    30 end

    31 if ( s t r ( l ) = = ) | s t r ( l ) = = ] | s t r ( l ) = = } )32 if ( s t a c k . t o p = = 0 )

    33 v a l i d = 0 ;

    34 break ;35 else

    36 i = s t a c k . a ( s t a c k . t o p ) ;

    37 b = s t a c k . a ( 1 ) ;

    38 for i 1 = 2 : s t a c k . t o p - 1

    39 b = [ b ( : , :) s t ac k . a ( i 1 ) ]

    40 end

    41 s t a c k . a = b ;

    42 s t a c k . t o p = s t a c k . t o p - 1 ;

    43 s y m b = s t r ( l ) ;

    44 disp ( s t a c k ) ;

    45 if ( ( ( s y m b = = ) ) & ( i = = ( ) ) | ( ( s y m b = = ] ) & ( i = = [ ) ) | ( ( s y m b = = } ) & ( i = = { ) ) )

    46 else

    47 v a l i d = 0 ;

    48 break ;

    49 end

    50 end

    51 end

    52 l = l + 1 ;

    53 end

    54 if ( s t a c k . t o p ~ = 0 )55 v a l i d = 0 ;

    56 end

    57 if ( v a l i d = = 0 )

    58 disp ( I n v a l i d S t r i n g ) ;

    23

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    25/82

    59 else

    60 disp ( V a l id S t r i n g ) ;61 end62 e n d f u n c t i o n

    63 / / C a l l i n g R o ut i ne :64 s t r i n g v a l i d ( [ ( A + B } ) ])65 s t r i n g v a l i d ( [ { [ A + B ] [ (

    C D ) ] ])66 s t r i n g v a l i d ( [ ( A + B ) { C +

    D } [ F + G ] ])67 s t r i n g v a l i d ( [ ( ( H ) { ( [ J

    + K ] ) } ) ])

    68 s t r i n g v a l i d ( [ ( ( ( A ) ) ) ])

    Scilab code Exa 3.2 example

    1 / / e xa mp le 3 . 22 / / Queue O p e r a t i o n s3 c l e ar a ll ;

    4 clc ;

    5 function [ q 2 ] = p u s h ( e l e , q 1 )6 if ( q 1 . r e a r = = q 1 . f r o n t )

    7 q 1 . a = e l e ;

    8 q 1 . r e a r = q 1 . r e a r + 1 ;

    9 else

    10 q 1 . a =[ q 1 . a ( : , :) e l e ];

    11 q 1 . r e a r = q 1 . r e a r + 1 ;

    12 end

    13 q 2 = q 1 ;

    14 e n d f u n c t i o n

    15 funcprot ( 0 ) ;16 function [ e l e , q 2 ] = p o p ( q 1 )

    17 e l e = - 1 ;

    18 q 2 = 0 ;

    19 if ( q 1 . r e a r = = q 1 . f r o n t )

    24

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    26/82

    20 disp ( Q ueu e U n d e r f l o w ) ;

    21 return ;22 else

    23 e l e = q 1 . a ( q 1 . r e a r - q 1 . f r o n t ) ;

    24 q 1 . f r o n t = q 1 . f r o n t + 1 ;

    25 i = 1 ;

    26 a = q 1 . a ( 1 ) ;

    27 for i = 2 : ( q 1 . r e a r - q 1 . f r o n t )

    28 a = [ a ( : , :) q 1 . a ( i ) ];

    29 end

    30 q 1 . a = a ;

    31 end

    32 q 2 = q 1 ;33 e n d f u n c t i o n

    34 funcprot ( 0 ) ;

    35 / / C a l l i n g R o ut i n e :36 q 1 = s t r u c t ( a ,0 , r e a r ,0 , f r o n t ,0)37 q 1 = p u s h ( 3 , q 1 )

    38 q 1 = p u s h ( 2 2 , q 1 ) ;

    39 q 1 = p u s h ( 2 1 , q 1 ) ;

    40 disp ( q 1 , Queue a f t e r i n s e r t i o n ) ;41 [ e l e , q 1 ] = p o p ( q 1 )

    42 disp ( e l e , p op ed e l e m en t

    ) ;

    43 disp ( q 1 , Queue a f t e r p o pi n g ) ;44 [ e l e , q 1 ] = p o p ( q 1 ) ;

    45 [ e l e , q 1 ] = p o p ( q 1 ) ;

    46 [ e l e , q 1 ] = p o p ( q 1 ) ; / / U n d er f lo w C o n d i t i o n

    Scilab code Exa 1.3.a example

    1 c l e ar a ll ;2 clc ;

    3 disp ( U n s o lv e d e xa mp le 3 ) ;4 function [ l ] = s t r l e n ( x )

    5 i = 1 ;

    25

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    27/82

    6 l = 0 ;

    7 [ j , k ] = size ( x )8 for i = 1 : k

    9 l = l + length ( x ( i ) ) ;

    10 end

    11 e n d f u n c t i o n

    12 function [ ] = s t r ( s t )

    13 s t a c k = s t r u c t ( a ,0 , top ,0);14 st = string ( s t ) ;

    15 l = 1 ;

    16 l 1 = s t r l e n ( s t ) ;

    17 s y m b = s t ( l ) ;

    18 v a l i d = 1 ;19 while ( l < l 1 )

    20 while ( s y m b ~ = C )21 if ( s t a c k . t o p = = 0 )

    22 s t a c k . a = s t ( l ) ;

    23 s t a c k . t o p = s t a c k . t o p + 1 ;

    24 else

    25 s t ac k . a = [ s t ac k . a ( : , : ) s t ( l ) ];

    26 s t a c k . t o p = s t a c k . t o p + 1 ;

    27 end

    28 l = l + 1 ;

    29 s y m b = s t ( l ) ;

    30 end

    31 i = s t ( l + 1 ) ;

    32 if ( s t a c k . t o p = = 0 )

    33 v a l i d = 0 ;

    34 break ;

    35 else

    36 s y m b 1 = s t a c k . a ( s t a c k . t o p ) ;

    37 s t a c k . t o p = s t a c k . t o p - 1 ;

    38 if ( i ~ = s y m b 1 )

    39 v a l i d = 0 ;40 break ;

    41 end

    42 end

    43 l = l + 1 ;

    26

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    28/82

    44 end

    45 if ( s t a c k . t o p ~ = 0 )46 v a l i d = 0 ;

    47 end

    48 if ( v a l i d = = 0 )

    49 disp ( Not o f t he g i ve n f or ma t ) ;50 else

    51 disp ( S t r i n g Of t h e G iv en Fo rma t ) ;52 end

    53 e n d f u n c t i o n

    54 / / C a l l i n g R o ut i n e :55 s t = [A A B A C A B A A ]

    56 s t r ( s t )57 s t = [A A B A C A B A ]58 s t r ( s t )

    Scilab code Exa 3.3 example

    1 / / S o l v e d E xamp le 3 . 3 :2 / / Co nv e r i n g an i n f i x e x p r e s s i o n t o a P o s t f i x

    E x p r e s s i o n :3 function [ s t a ] = p u s h ( s t a c , e l e )

    4 sta=0;

    5 if ( s t a c . t o p = = 0 )

    6 s t a c . a = e l e ;

    7 s t a c . t o p = s t a c . t o p + 1 ;

    8 else

    9 s t ac . a = [ s t a c . a ( : , :) e le ]

    10 s t a c . t o p = s t a c . t o p + 1 ;

    11 end

    12 disp ( s t a c ) ;13 s t a = s t a c ;

    14 e n d f u n c t i o n

    15

    16 function [ e l e , s t a ] = p o p ( s t a c k )

    27

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    29/82

    17 e l e =1 ;

    18 if ( s t a c k . t o p = = 0 )19 disp ( S t a c k U n de r fl o w ) ;20 break ;

    21 else

    22 e l e = s t a c k . a ( s t a c k . t o p ) ;

    23 s t a c k . t o p = s t a c k . t o p - 1 ;

    24 if ( s t a c k . t o p ~ = 0 )

    25 b = s t a c k . a ( 1 ) ;

    26 for i 2 = 2 : s t a c k . t o p

    27 b = [ b ( : , :) s t ac k . a ( i 2 ) ] ;

    28 end

    29 s t a c k . a = b ;30 else

    31 s t a c k . a = 0 ;32 end

    33 end

    34 s t a = s t a c k ;

    35 e n d f u n c t i o n

    36 function [ l ] = s t r l e n ( x )

    37 i = 1 ;

    38 l = 0 ;

    39 [ j , k ] = size ( x )

    40 for i = 1 : k

    41 l = l + length ( x ( i ) ) ;

    42 end

    43 e n d f u n c t i o n

    44 function [ p ] = p r e ( s 1 , s 2 )

    45 i 1 = 0 ;

    46 select s1 ,

    47 case + then i 1 = 5 ;48 case then i 1 = 5 ;49 case then i 1 = 9 ;

    50 case / then i 1 = 9 ;51 end

    52 i 2 = 0 ;

    53 select s2 ,

    54 case + then i 2 = 5 ;

    28

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    30/82

    55 case then i 2 = 5 ;

    56 case then i 2 = 9 ;57 case / then i 2 = 9 ;58 end

    59 p = 0 ;

    60 p = i 1 - i 2 ;

    61 if ( s 1 = = ( )62 p = - 1 ;

    63 end

    64 if ( s 2 = = ( & s 1 ~ = ) )65 p = - 1 ;

    66 end

    67 if ( s 1 ~ = ( & s 2 = = ) )68 p = 1 ;

    69 end

    70

    71 e n d f u n c t i o n

    72 function [ a 2 ] = i n t o p o ( a 1 , n )

    73 s t a c k = s t r u c t ( a ,0 , top ,0);74 l 1 = 1 ;

    75 l 2 = s t r l e n ( a 1 ( 1 ) )

    76 for i = 2 : n

    77 l 2 = l 2 + s t r l e n ( a 1 ( i ) )

    78 end

    79 a2 = list () ;

    80 while ( l 1 < = l 2 )

    81 s y m b = a 1 ( l 1 ) ;

    82 if ( i s a l p h a n u m ( string ( a 1 ( l 1 ) ) ) )

    83 a2 = list ( a 2 , s y m b ) ;

    84 else

    85 while ( s t a c k . t o p ~ = 0 & ( p r e ( s t a c k . a ( s t a c k . t o p ) ,

    s y m b ) > = 0 ) )

    86 [ t o p s y mb , s t a c k ] = p o p ( s t a c k ) ;

    87 if ( t o p s y m b = = ) | t o p s y m b = = ( )88 a 2 = a 2 ;

    89 else

    90 a2 = list ( a 2 , t o p s y m b ) ;

    91 end

    29

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    31/82

    92 end

    93 if ( s t a c k . t o p = = 0 | s y m b ~ = ) )94 s t a c k = p u s h ( s t a c k , s y m b ) ;95 else

    96 [ e l e , s t a c k ] = p o p ( s t a c k ) ;

    97 end

    98 end

    99 l 1 = l 1 + 1 ;

    100 end

    101 while ( s t a c k . t o p ~ = 0 )

    102 [ t o p s ym b , s t a c k ] = p o p ( s t a c k ) ;

    103 if ( t o p s y m b = = ) | t o p s y m b = = ( )

    104 a 2 = a 2 ;105 else

    106 a2 = list ( a 2 , t o p s y m b ) ;

    107 end

    108 end

    109 disp ( a 2 ) ;

    110 e n d f u n c t i o n

    111 / / C a l l i n g R o ut i n e :112 a 1 = [ ( 2 + 3 ) ( 5 4 ) ]113 a 2 = i n t o p o ( a 1 , 1 1 )

    30

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    32/82

    Chapter 4

    Linked lists

    Scilab code Exa 4.1 example

    1 // L i s t o f word s i n a l i n k e d l i s t .2 c l e ar a ll ;

    3 clc ;

    4 printf ( \n E xa pm le 4 . 1 \ n ) ;5 x = list ( s c i , l a b , t e x t , c o m p a n i o n s h i p , p r o j e c t )

    ;

    6 disp ( x= ) ;7 disp ( x ) ;

    Scilab code Exa 4.2 example

    1 //CIRCULAR LINKED LIST2 c l e ar a ll ;

    3 clc ;

    4 funcprot ( 0 ) ;

    5 disp ( E xa mp le 4 . 2 ) ;6 function [ l i n k 2 ] = a p p e n d ( e l e , l i n k 1 )

    7 l i n k 2 = list

    31

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    33/82

    ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , , 0

    ;8 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 )

    9 l i n k 1 ( 1 ) ( 1 ) . d a t a = e l e ;

    10 l i n k 1 ( 1 ) ( 1 ) . a d d = 1 ;

    11 l i n k 1 ( 1 ) ( 1 ) . n e x a d d = 1 ;

    12 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;

    13 else

    14 if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d )

    15 l i n 2 = l i n k 1 ( 1 ) ( 1 ) ;

    16 l i n 2 . d a t a = e l e ;

    17 l i n 2 . a d d = l i n k 1 ( 1 ) ( 1 ) . a d d + 1 ;

    18 l i n k 1 ( 1 ) ( 1 ) . n e x a d d = l i n 2 . a d d ;19 l i n 2 . n e x a d d = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    20 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;

    21 l i n k 2 ( 2 ) = l i n 2 ;

    22 else

    23 l i n 2 = l i n k 1 ( 1 ) ( 1 ) ;

    24 i = 1 ;

    25 while ( l i n k 1 ( i ) ( 1 ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    26 i = i + 1 ;

    27 end

    28 j = i ;

    29 l i n 2 . d a t a = e l e ;

    30 l i n 2 . a d d = l i n k 1 ( i ) . a d d + 1 ;

    31 l i n 2 . n e x a d d = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    32 l i n k 1 ( i ) . n e x a d d = l i n 2 . a d d ;

    33 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;

    34 i = 2 ;

    35 while ( l i n k 1 ( i ) . n e x a d d ~ = l i n 2 . a d d )

    36 l i n k 2 ( i ) = ( l i n k 1 ( i ) ) ;

    37 i = i + 1 ;

    38 end

    39 l i n k 2 ( i ) = l i n k 1 ( i ) ;40 l i n k 2 ( i + 1 ) = l i n 2 ;

    41 end

    42 end

    43 e n d f u n c t i o n

    32

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    34/82

    44 function [ l i n k 2 ] = a d d ( e l e , p o s , l i n k 1 ) ;

    45 l i n k 2 = list( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,

    ;

    46 i = 1 ;

    47 while ( i < = p o s )

    48 if ( l i n k 1 ( i ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d )

    49 break ;

    50 else

    51 i = i + 1 ;

    52 end

    53 end

    54 if ( l i n k 1 ( i ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )55 i = i - 1 ;

    56 l i n 2 . d a t a = e l e ;

    57 l i n 2 . a d d = i ;

    58 j = i ;

    59 while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    60 l i n k 1 ( j ) . a d d = l i n k 1 ( j ) . a d d + 1 ;

    61 l i n k 1 ( j ) . n e x a d d = l i n k 1 ( j ) . n e x a d d + 1 ;

    62 j = j + 1 ;

    63 end

    64 l i n k 1 ( j ) . a d d = l i n k 1 ( j ) . a d d + 1 ;

    65 l i n 2 . n e x a d d = l i n k 1 ( i ) . a d d ;

    66 l i n k 1 ( i - 1 ) . n e x a d d = l i n 2 . a d d ;

    67 k = 1 ;

    68 while ( k < i )

    69 l i n k 2 ( k ) = l i n k 1 ( k ) ;

    70 k = k + 1 ;

    71 end

    72 l i n k 2 ( k ) = l i n 2 ;

    73 k = k + 1 ;

    74 l i n k 2 ( k ) = l i n k 1 ( k - 1 ) ;

    75 k = k + 176 l = k - 1 ;

    77 while ( k ~ = j )

    78 l i n k 2 ( k ) = l i n k 1 ( l ) ;

    79 k = k + 1 ;

    33

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    35/82

    80 l = l + 1 ;

    81 end82 l i n k 2 ( j ) = l i n k 1 ( j - 1 ) ; ;

    83 l i n k 2 ( j + 1 ) = l i n k 1 ( j ) ;

    84 else

    85 if ( i = = p o s )

    86 k = 1 ;

    87 l i n 2 . d a t a = e l e ;

    88 l i n 2 . a d d = l i n k 1 ( i - 1 ) . a d d + 1 ;

    89 l i n k 1 ( i ) . a d d = l i n k 1 ( i ) . a d d + 1 ;

    90 l i n 2 . n e x a d d = l i n k 1 ( i ) . a d d ;

    91 l i n k 1 ( i ) . n e x a d d = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    92 k = 1 ;93 while ( k < p o s )

    94 l i n k 2 ( k ) = l i n k 1 ( k ) ;

    95 k = k + 1 ;

    96 end

    97 l i n k 2 ( k ) = l i n 2 ;

    98 l i n k 2 ( k + 1 ) = l i n k 1 ( k )

    99 end

    100 end

    101

    102 e n d f u n c t i o n

    103 function [ l i n k 2 ] = d e l e t e 1 ( p o s , l i n k 1 )

    104 l i n k 2 = list

    ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , , 0

    ;

    105 i = 1 ;

    106 j = 1 ;

    107 while ( i < p o s )

    108 if ( ( l i n k 1 ( j ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d ) )

    109 j = 1 ;

    110 i = i + 1 ;

    111 else112 i = i + 1 ;

    113 j = j + 1 ;

    114 end

    115 end

    34

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    36/82

    116 if ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    117 k = 1 ;118 if ( j = = 1 )

    119 k = 2 ;

    120 while ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    121 l i n k 2 ( k - 1 ) = l i n k 1 ( k ) ;

    122 k = k + 1 ;

    123 end

    124 l i n k 2 ( k - 1 ) = l i n k 1 ( k ) ;

    125 l i n k 2 ( k - 1 ) . n e x a d d = l i n k 2 ( 1 ) . a d d ;

    126 else

    127 l i n 2 = l i n k 1 ( j ) ;

    128 l i n k 1 ( j - 1 ) . n e x a d d = l i n k 1 ( j + 1 ) . a d d ;129 k = 1 ;

    130 while ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( j + 1 ) . a d d )

    131 l i n k 2 ( k ) = l i n k 1 ( k ) ;

    132 k = k + 1 ;

    133 end

    134 l i n k 2 ( k ) = l i n k 1 ( k ) ;

    135 k = k + 2 ;

    136 while ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    137 l i n k 2 ( k - 1 ) = l i n k 1 ( k ) ;

    138 k = k + 1 ;

    139 end

    140 l i n k 2 ( k - 1 ) = l i n k 1 ( k ) ;

    141 end

    142 else

    143 l i n k 1 ( j - 1 ) . n e x a d d = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    144 l = 1 ;

    145 while ( l i n k 1 ( l ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    146 l i n k 2 ( l ) = l i n k 1 ( l ) ;

    147 l = l + 1 ;

    148 end

    149 l i n k 2 ( l ) = l i n k 1 ( l ) ;150 end

    151 e n d f u n c t i o n

    152 / / C a l l i n g R o ut i n e :153 l i n k 1 = s t r u c t ( da ta ,0 , add ,0 , ne x add ,0);

    35

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    37/82

    154 l i n k 1 = a p p e n d ( 4 , l i n k 1 ) ; // T hi s w i l l a c t u a l y c r e a t e a

    l i s t and 4 a s s t a r t155 l i n k 1 = a p p e n d ( 6 , l i n k 1 ) ;156 l i n k 1 = a d d ( 1 0 , 2 , l i n k 1 ) ;

    157 l i n k 1 = d e l e t e 1 ( 4 , l i n k 1 ) ; // As t h e l i s t i s c i r c u l a r t h e4 th e le me nt r e f e r s t o a c t u a l y t he 1 s t one

    158 disp ( l i n k 1 , A f t e r t he a bo ve m an up la ti on s t he l i s t i s ) ;

    Scilab code Exa 4.3 example

    1 // L i s t I n s e r t i o n .2 clc ;

    3 c l e ar a ll ;

    4 disp ( E xa mp le 4 . 3 ) ;5 funcprot (0)

    6 function [ l i n k 2 ] = i n s e r t _ p r i ( e l e , l i n k 1 )

    7 l i n k 2 = list

    ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , , 0

    ;

    8 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 )9 l i n k 1 ( 1 ) ( 1 ) . d a t a = e l e ;

    10 l i n k 1 ( 1 ) ( 1 ) . a d d = 1 ;

    11 l i n k 1 ( 1 ) ( 1 ) . n e x a d d = 1 ;

    12 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;

    13 else

    14 if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d )

    15 if ( e l e > = l i n k 1 ( 1 ) ( 1 ) . d a t a )

    16 t = e l e ;

    17 p = l i n k 1 ( 1 ) ( 1 ) . d a t a ;

    18 else19 t = l i n k 1 ( 1 ) ( 1 ) . d a t a ;

    20 p = e l e ;

    21 end

    22 l i n k 1 ( 1 ) ( 1 ) . d a t a = t ;

    36

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    38/82

    23 l i n 2 = l i n k 1 ( 1 ) ( 1 ) ;

    24 l i n 2 . d a t a = p ;25 l i n 2 . a d d = 2 ;

    26 l i n 2 . n e x a d d = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    27 l i n k 1 ( 1 ) ( 1 ) . n e x a d d = l i n 2 . a d d ;

    28 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;

    29 l i n k 2 ( 2 ) = l i n 2 ;

    30 else

    31 i = 1 ;

    32 a = [ ] ;

    33 while ( l i n k 1 ( i ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    34 a = [ a ( : , :) l i nk 1 ( i ) . d a ta ] ;

    35 i = i + 1 ;36 end

    37 a = [ a ( : , :) l i nk 1 ( i ) . d a ta ] ;

    38 a = gsort ( a ) ;

    39 j = 1 ;

    40 while ( j < = i )

    41 l i n k 1 ( j ) . d a t a = a ( j ) ;

    42 j = j + 1 ;

    43 end

    44 k = 1 ;

    45 while ( l i n k 1 ( k ) . d a t a > = e l e )

    46 if ( l i n k 1 ( k ) . n e x a d d = = l i n k 1 ( 1 ) ( 1 ) . a d d )

    47 break ;

    48 else

    49 l i n k 2 ( k ) = l i n k 1 ( k ) ;

    50 k = k + 1 ;

    51 end

    52 end

    53 if ( l i n k 1 ( k ) . n e x a d d ~ = l i n k 1 ( 1 ) ( 1 ) . a d d )

    54 l i n 2 = l i n k 1 ( k ) ;

    55 l i n 2 . d a t a = e l e ;

    56 l i n 2 . a d d = l i n k 1 ( k ) . a d d ;57 j = k ;

    58 y = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    59 while ( l i n k 1 ( k ) . n e x a d d ~ = y )

    60 l i n k 1 ( k ) . a d d = l i n k 1 ( k ) . a d d + 1 ;

    37

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    39/82

    61 l i n k 1 ( k ) . n e x a d d = l i n k 1 ( k ) . n e x a d d + 1 ;

    62 k = k + 1 ;63 end

    64 l i n k 1 ( k ) . a d d = l i n k 1 ( k ) . a d d + 1 ;

    65 l i n 2 . n e x a d d = l i n k 1 ( j ) . a d d ;

    66 l i n k 2 ( j ) = l i n 2 ;

    67 j = j + 1 ;

    68 while ( j < = k + 1 )

    69 l i n k 2 ( j ) = l i n k 1 ( j - 1 ) ;

    70 j = j + 1 ;

    71 end

    72 else

    73 l i n 2 = l i n k 1 ( k ) ;74 l i n 2 . d a t a = e l e ;

    75 l i n 2 . n e x a d d = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    76 l i n 2 . a d d = l i n k 1 ( k ) . a d d + 1 ;

    77 l i n k 1 ( k ) . n e x a d d = l i n 2 . a d d ;

    78 j = 1 ;

    79 while ( j < = k )

    80 l i n k 2 ( j ) = l i n k 1 ( j ) ;

    81 j = j + 1 ;

    82 end

    83 l i n k 2 ( j ) = l i n 2 ;

    84 end

    85 end

    86 end

    87 e n d f u n c t i o n

    88 / / C a l l i n g R o u ti n e :89 l i n k 1 = s t r u c t ( dat a ,0 , add ,0 , ne x add ,0);90 l i n k 1 = i n s e r t _ p r i ( 3 , l i n k 1 ) ;

    91 l i n k 1 = i n s e r t _ p r i ( 4 , l i n k 1 ) ;

    92 l i n k 1 = i n s e r t _ p r i ( 2 2 , l i n k 1 ) ;

    93 l i n k 1 = i n s e r t _ p r i ( 2 1 , l i n k 1 ) ;

    94 l i n k 1 = i n s e r t _ p r i ( 1 1 , l i n k 1 ) ;95 disp ( l i n k 1 , L i s t A f t e r I n s e r t i o n s ) ;

    38

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    40/82

    Scilab code Exa 4.4 example

    1 / / D e l e t i o n f ro m t h e l i s t :2 function [ l i n k 2 ] = a p p e n d ( e l e , l i n k 1 )

    3 l i n k 2 = list

    ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , , 0

    ;

    4 if ( l i n k 1 ( 1 ) ( 1 ) . a d d = = 0 )

    5 l i n k 1 ( 1 ) ( 1 ) . d a t a = e l e ;

    6 l i n k 1 ( 1 ) ( 1 ) . a d d = 1 ;

    7 l i n k 1 ( 1 ) ( 1 ) . n e x a d d = 0 ;

    8 l i n k 1 ( 1 ) ( 1 ) . p r e v a d d = 0 ;

    9 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;

    10 else

    11 if ( l i n k 1 ( 1 ) ( 1 ) . n e x a d d = = 0 )

    12 l i n 2 = l i n k 1 ( 1 ) ( 1 ) ;

    13 l i n 2 . d a t a = e l e ;

    14 l i n 2 . a d d = l i n k 1 ( 1 ) ( 1 ) . a d d + 1 ;

    15 l i n k 1 ( 1 ) ( 1 ) . n e x a d d = l i n 2 . a d d ;

    16 l i n 2 . n e x a d d = 0 ;17 l i n 2 . p r e v a d d = l i n k 1 ( 1 ) ( 1 ) . a d d ;

    18 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;

    19 l i n k 2 ( 2 ) = l i n 2 ;

    20 else

    21 l i n 2 = l i n k 1 ( 1 ) ( 1 ) ;

    22 i = 1 ;

    23 while ( l i n k 1 ( i ) ( 1 ) . n e x a d d ~ = 0 )

    24 i = i + 1 ;

    25 end

    26 j = i ;

    27 l i n 2 . d a t a = e l e ;

    28 l i n 2 . a d d = l i n k 1 ( i ) . a d d + 1 ;

    29 l i n 2 . n e x a d d = 0 ;

    30 l i n k 1 ( i ) . n e x a d d = l i n 2 . a d d ;

    39

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    41/82

    31 l i n 2 . p r e v a d d = l i n k 1 ( i ) . a d d ;

    32 l i n k 2 ( 1 ) = l i n k 1 ( 1 ) ( 1 ) ;33 i = 2 ;

    34 while ( l i n k 1 ( i ) . n e x a d d ~ = l i n 2 . a d d )

    35 l i n k 2 ( i ) = ( l i n k 1 ( i ) ) ;

    36 i = i + 1 ;

    37 end

    38 l i n k 2 ( i ) = l i n k 1 ( i ) ;

    39 l i n k 2 ( i + 1 ) = l i n 2 ;

    40 end

    41 end

    42 e n d f u n c t i o n

    43 function [ l i n k 2 ] = a d d ( e l e , p o s , l i n k 1 ) ;44 l i n k 2 = list

    ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 ,

    ;

    45 i = 1 ;

    46 while ( i < = p o s )

    47 if ( l i n k 1 ( i ) . n e x a d d = = 0 )

    48 break ;

    49 else

    50 i = i + 1 ;

    51 end

    52 end

    53 if ( l i n k 1 ( i ) . n e x a d d ~ = 0 )

    54 i = i - 1 ;

    55 l i n 2 . d a t a = e l e ;

    56 l i n 2 . a d d = i ;

    57 j = i ;

    58 while ( l i n k 1 ( j ) . n e x a d d ~ = 0 )

    59 l i n k 1 ( j ) . p r e v a d d = l i n k 1 ( j ) . p r e v a d d + 1 ;

    60 l i n k 1 ( j ) . a d d = l i n k 1 ( j ) . a d d + 1 ;

    61 l i n k 1 ( j ) . n e x a d d = l i n k 1 ( j ) . n e x a d d + 1 ;

    62 j = j + 1 ;63 end

    64 l i n k 1 ( j ) . p r e v a d d = l i n k 1 ( j ) . p r e v a d d + 1 ;

    65 l i n k 1 ( j ) . a d d = l i n k 1 ( j ) . a d d + 1 ;

    66 l i n 2 . n e x a d d = l i n k 1 ( i ) . a d d ;

    40

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    42/82

    67 l i n k 1 ( i ) . p r e v a d d = l i n 2 . a d d ;

    68 l i n 2 . p r e v a d d = l i n k 1 ( i - 1 ) . a d d ;69 l i n k 1 ( i - 1 ) . n e x a d d = l i n 2 . a d d ;

    70 k = 1 ;

    71 while ( k < i )

    72 l i n k 2 ( k ) = l i n k 1 ( k ) ;

    73 k = k + 1 ;

    74 end

    75 l i n k 2 ( k ) = l i n 2 ;

    76 k = k + 1 ;

    77 l i n k 2 ( k ) = l i n k 1 ( k - 1 ) ;

    78 k = k + 1

    79 l = k - 1 ;80 while ( k ~ = j )

    81 l i n k 2 ( k ) = l i n k 1 ( l ) ;

    82 k = k + 1 ;

    83 l = l + 1 ;

    84 end

    85 l i n k 2 ( j ) = l i n k 1 ( j - 1 ) ; ;

    86 l i n k 2 ( j + 1 ) = l i n k 1 ( j ) ;

    87 else

    88 if ( i = = p o s )

    89 k = 1 ;

    90 l i n 2 . d a t a = e l e ;

    91 l i n 2 . a d d = l i n k 1 ( i - 1 ) . a d d + 1 ;

    92 l i n k 1 ( i ) . a d d = l i n k 1 ( i ) . a d d + 1 ;

    93 l i n 2 . n e x a d d = l i n k 1 ( i ) . a d d ;

    94 l i n k 1 ( i ) . p r e v a d d = l i n 2 . a d d ;

    95 l i n 2 . p r e v a d d = l i n k 1 ( i - 1 ) . a d d ;

    96 k = 1 ;

    97 while ( k < p o s )

    98 l i n k 2 ( k ) = l i n k 1 ( k ) ;

    99 k = k + 1 ;

    100 end101 l i n k 2 ( k ) = l i n 2 ;

    102 l i n k 2 ( k + 1 ) = l i n k 1 ( k )

    103 end

    104 end

    41

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    43/82

    105

    106 e n d f u n c t i o n107 function [ l i n k 2 ] = d e l e t e 1 ( p o s , l i n k 1 )

    108 l i n k 2 = list

    ( 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , , 0

    ;

    109 i = 1 ;

    110 while ( i < = p o s )

    111 if ( ( l i n k 1 ( i ) . n e x a d d = = 0 ) )

    112 break ;

    113 else

    114 i = i + 1 ;

    115 end116 end

    117 if ( l i n k 1 ( i ) . n e x a d d ~ = 0 )

    118 i = i - 1 ;

    119 j = 1 ;

    120 if ( i = = 1 )

    121 j = 1 ;

    122 while ( l i n k 1 ( j ) . n e x a d d ~ = 0 )

    123 l i n k 2 ( j ) = l i n k 1 ( j ) ;

    124 j = j + 1 ;

    125 end

    126 l i n k 2 ( j ) = l i n k 1 ( j ) ;

    127 else

    128 l i n k 1 ( i - 1 ) . n e x a d d = l i n k 1 ( i + 1 ) . a d d ;

    129 l i n k 1 ( i + 1 ) . p r e v a d d = l i n k 1 ( i - 1 ) . a d d ;

    130 while ( l i n k 1 ( j ) . n e x a d d ~ = l i n k 1 ( i + 1 ) . a d d )

    131 l i n k 2 ( j ) = l i n k 1 ( j ) ;

    132 j = j + 1 ;

    133 end

    134 if ( j ~ = i - 1 )

    135 l i n k 2 ( j ) = l i n k 1 ( j ) ;

    136 l i n k 2 ( j + 1 ) = l i n k 1 ( j + 1 ) ;137 k = i + 1 ;

    138 l = 2 ;

    139 else

    140 l i n k 2 ( j ) = l i n k 1 ( j ) ;

    42

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    44/82

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    45/82

    Chapter 5

    Trees

    Scilab code Exa 5.1 example

    1

    2 funcprot ( 0 ) ;

    3 function [ t r e e ] = m a k e t r e e ( x )

    4 t r e e = zeros ( 3 0 , 1 ) ;

    5 for i = 1 : 3 0

    6 t r e e ( i ) = - 1 ;

    7 end8 t r e e ( 1 ) = x ;

    9 t r e e ( 2 ) = - 2 ;

    10 e n d f u n c t i o n

    11 function [ t r e e 1 ] = s e t l e f t ( t r e e , t r e , x )

    12 t r e e 1 = [ ] ;

    13 i = 1 ;

    14 while ( t r e e ( i ) ~ = - 2 )

    15 if ( t r e e ( i ) = = t r e )

    16 j = i ;

    17 end18 i = i + 1 ;

    19 end

    20 if ( i > 2 * j )

    21 t r e e ( 2 * j ) = x ;

    44

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    46/82

    22 else

    23 t r e e ( 2 * j ) = x ;24 t r e e ( 2 * j + 1 ) = - 2 ;

    25 for l = i : 2 * j - 1

    26 t r e e ( i ) = - 1 ;

    27 end

    28 end

    29 t r e e 1 = t r e e ;

    30 e n d f u n c t i o n

    31 function [ t r e e 1 ] = s e t r i g h t ( t r e e , t r e , x )

    32 t r e e 1 = [ ] ;

    33 i = 1 ;

    34 while ( t r e e ( i ) ~ = - 2 )35 if ( t r e e ( i ) = = t r e )

    36 j = i ;

    37 end

    38 i = i + 1 ;

    39 end

    40 if ( i > 2 * j + 1 )

    41 t r e e ( 2 * j + 1 ) = x ;

    42 else

    43 t r e e ( 2 * j + 1 ) = x ;

    44 t r e e ( 2 * j + 2 ) = - 2 ;

    45 for l = i : 2 * j

    46 t r e e ( i ) = - 1 ;

    47 end

    48 end

    49 t r e e 1 = t r e e ;

    50 e n d f u n c t i o n

    51 function [ x ] = i s l e f t ( t r e e , t r e )

    52 i = 1 ;

    53 x = 0 ;

    54 while ( t r e e ( i ) ~ = - 2 )

    55 if ( t r e e ( i ) = = t r e )56 j = i ;

    57 end

    58 i = i + 1 ;

    59 end

    45

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    47/82

    60 if ( i > = 2 * j )

    61 if ( ( t r e e ( 2 * j ) ~ = - 1 ) | ( t r e e ( 2 * j ) ~ = - 2 ) )62 x = 1 ;

    63 return 1;

    64 else

    65 return 0;

    66 end

    67 else

    68 x = 0 ;

    69 return x ;

    70 end

    71 e n d f u n c t i o n

    72 function [ x ] = i s r i g h t ( t r e e , t r e )73 i = 1 ;

    74 x = 0 ;

    75 while ( t r e e ( i ) ~ = - 2 )

    76 if ( t r e e ( i ) = = t r e )

    77 j = i ;

    78 end

    79 i = i + 1 ;

    80 end

    81 if ( i > = 2 * j + 1 )

    82 if ( ( t r e e ( 2 * j + 1 ) ~ = - 1 ) | ( t r e e ( 2 * j + 1 ) ~ = - 2 ) )

    83 x = 1 ;

    84 return 1;

    85 else

    86 return 0;

    87 end

    88 else

    89 x = 0 ;

    90 return x ;

    91 end

    92 e n d f u n c t i o n

    93 / / C a l l i n g R o ut i n e :94 t r e e = m a k e t r e e ( 3 ) ;

    95 disp ( t r e e , T r e e made ) ;96 tree= setleft (tree ,3,1);

    97 disp ( t r e e , A f te r s e t t i n g 1 t o l e f t o f 3 ) ;

    46

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    48/82

    98 t r e e = s e t r i g h t ( t r e e , 3 , 2 ) ;

    99 disp ( t r e e , A ft e r s e t t i n g 2 t o r i g h t o f 3 ) ;100 t r e e = s e t r i g h t ( t r e e , 2 , 4 ) ;101 tree= setleft (tree ,2,5);

    102 t r e e = s e t r i g h t ( t r e e , 1 , 6 ) ;

    103 t r e e = s e t r i g h t ( t r e e , 5 , 8 ) ;

    104 disp ( t r e e , A f t e r a bo ve o p e r a t i o n s : ) ;105 x= isright (tree ,3);

    106 disp (x , Ch e c k i n g f o r t h e r i g h t s o n o f 3 y e s i f 1e l s e no ) ;

    107 x= isleft( tree ,2);

    108 disp (x , Check f o r l e f t s on o f 2 ) ;

    Scilab code Exa 5.2 example

    1 funcprot ( 0 ) ;

    2 function [ t r e e ] = m a k e t r e e ( x )

    3 t r e e = zeros ( 3 0 , 1 ) ;

    4 for i = 1 : 3 0

    5 t r e e ( i ) = - 1 ;

    6 end7 t r e e ( 1 ) = x ;

    8 t r e e ( 2 ) = - 2 ;

    9 e n d f u n c t i o n

    10 function [ t r e e 1 ] = s e t l e f t ( t r e e , t r e , x )

    11 t r e e 1 = [ ] ;

    12 i = 1 ;

    13 while ( t r e e ( i ) ~ = - 2 )

    14 if ( t r e e ( i ) = = t r e )

    15 j = i ;

    16 end17 i = i + 1 ;

    18 end

    19 if ( i > 2 * j )

    20 t r e e ( 2 * j ) = x ;

    47

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    49/82

    21 else

    22 t r e e ( 2 * j ) = x ;23 t r e e ( 2 * j + 1 ) = - 2 ;

    24 for l = i : 2 * j - 1

    25 t r e e ( i ) = - 1 ;

    26 end

    27 end

    28 t r e e 1 = t r e e ;

    29 e n d f u n c t i o n

    30 function [ t r e e 1 ] = s e t r i g h t ( t r e e , t r e , x )

    31 t r e e 1 = [ ] ;

    32 i = 1 ;

    33 while ( t r e e ( i ) ~ = - 2 )34 if ( t r e e ( i ) = = t r e )

    35 j = i ;

    36 end

    37 i = i + 1 ;

    38 end

    39 if ( i > 2 * j + 1 )

    40 t r e e ( 2 * j + 1 ) = x ;

    41 else

    42 t r e e ( 2 * j + 1 ) = x ;

    43 t r e e ( 2 * j + 2 ) = - 2 ;

    44 for l = i : 2 * j

    45 t r e e ( i ) = - 1 ;

    46 end

    47 end

    48 t r e e 1 = t r e e ;

    49 e n d f u n c t i o n

    50 function [ x ] = i s l e f t ( t r e e , t r e )

    51 i = 1 ;

    52 x = 0 ;

    53 while ( t r e e ( i ) ~ = - 2 )

    54 if ( t r e e ( i ) = = t r e )55 j = i ;

    56 end

    57 i = i + 1 ;

    58 end

    48

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    50/82

    59 if ( i > = 2 * j )

    60 if ( ( t r e e ( 2 * j ) ~ = - 1 ) | ( t r e e ( 2 * j ) ~ = - 2 ) )61 x = 1 ;

    62 return 1;

    63 else

    64 return 0;

    65 end

    66 else

    67 x = 0 ;

    68 return x ;

    69 end

    70 e n d f u n c t i o n

    71 function [ x ] = i s r i g h t ( t r e e , t r e )72 i = 1 ;

    73 x = 0 ;

    74 while ( t r e e ( i ) ~ = - 2 )

    75 if ( t r e e ( i ) = = t r e )

    76 j = i ;

    77 end

    78 i = i + 1 ;

    79 end

    80 if ( i > = 2 * j + 1 )

    81 if ( ( t r e e ( 2 * j + 1 ) ~ = - 1 ) | ( t r e e ( 2 * j + 1 ) ~ = - 2 ) )

    82 x = 1 ;

    83 return 1;

    84 else

    85 return 0;

    86 end

    87 else

    88 x = 0 ;

    89 return x ;

    90 end

    91 e n d f u n c t i o n

    92 funcprot ( 0 ) ;93 function [ ] = i n o r d e r ( t r e e , p )

    94 if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 )

    95 return ;

    96 else

    49

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    51/82

    97 inorder (tree ,2*p);

    98 printf ( %d\ t , t r e e ( p ) ) ;99 inorder (tree ,2*p+1) ;100 end

    101 e n d f u n c t i o n

    102 function [ ] = p r e o r d e r ( t r e e , p )

    103 if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 )

    104 return ;

    105 else

    106 printf ( %d\ t , t r e e ( p ) ) ;107 preorder (tree ,2*p);

    108 preorder (tree ,2*p +1);

    109 end110 e n d f u n c t i o n

    111 function [ ] = p o s t o r d e r ( t r e e , p )

    112 if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 )

    113 return ;

    114 else

    115 postorder (tree ,2*p);

    116 postorder (tree ,2*p +1);

    117 printf ( %d\ t , t r e e ( p ) ) ;118 end

    119 e n d f u n c t i o n

    120 / / C a l l i n g R o ut i n e :121 t r e e = m a k e t r e e ( 3 ) ;

    122 tree= setleft (tree ,3,1);

    123 t r e e = s e t r i g h t ( t r e e , 3 , 2 ) ;

    124 tree= setleft (tree ,2,4);

    125 t r e e = s e t r i g h t ( t r e e , 2 , 5 ) ;

    126 disp ( I n o r d er t r a v e r s a l ) ;127 inorder (tree ,1);

    128 disp ( P r e o rd e r t r a v e r s a l ) ;129 preorder (tree ,1);

    130 disp ( P o s to r de r t r a v e r s a l ) ;131 postorder (tree ,1);

    50

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    52/82

    Scilab code Exa 5.3 example

    1 funcprot ( 0 ) ;

    2 function [ t r e e ] = m a k e t r e e ( x )

    3 t r e e = zeros ( 1 , 3 0 ) ;

    4 for i = 1 : 3 0

    5 t r e e ( i ) = - 1 ;

    6 end

    7 t r e e ( 1 ) = x ;

    8 t r e e ( 2 ) = - 2 ;

    9 e n d f u n c t i o n

    10 function [ t r e e 1 ] = s e t l e f t ( t r e e , t r e , x )

    11 t r e e 1 = [ ] ;

    12 i = 1 ;

    13 while ( t r e e ( i ) ~ = - 2 )

    14 if ( t r e e ( i ) = = t r e )

    15 j = i ;

    16 end

    17 i = i + 1 ;

    18 end19 if ( i > 2 * j )

    20 t r e e ( 2 * j ) = x ;

    21 else

    22 t r e e ( 2 * j ) = x ;

    23 t r e e ( 2 * j + 1 ) = - 2 ;

    24 for l = i : 2 * j - 1

    25 t r e e ( i ) = - 1 ;

    26 end

    27 end

    28 t r e e 1 = t r e e ;

    29 e n d f u n c t i o n

    30 function [ t r e e 1 ] = s e t r i g h t ( t r e e , t r e , x )

    31 t r e e 1 = [ ] ;

    32 i = 1 ;

    51

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    53/82

    33 while ( t r e e ( i ) ~ = - 2 )

    34 if ( t r e e ( i ) = = t r e )35 j = i ;

    36 end

    37 i = i + 1 ;

    38 end

    39 if ( i > 2 * j + 1 )

    40 t r e e ( 2 * j + 1 ) = x ;

    41 else

    42 t r e e ( 2 * j + 1 ) = x ;

    43 t r e e ( 2 * j + 2 ) = - 2 ;

    44 for l = i : 2 * j

    45 t r e e ( i ) = - 1 ;46 end

    47 end

    48 t r e e 1 = t r e e ;

    49 e n d f u n c t i o n

    50 function [ x ] = i s l e f t ( t r e e , t r e )

    51 i = 1 ;

    52 x = 0 ;

    53 while ( t r e e ( i ) ~ = - 2 )

    54 if ( t r e e ( i ) = = t r e )

    55 j = i ;

    56 end

    57 i = i + 1 ;

    58 end

    59 if ( i > = 2 * j )

    60 if ( ( t r e e ( 2 * j ) ~ = - 1 ) | ( t r e e ( 2 * j ) ~ = - 2 ) )

    61 x = 1 ;

    62 return 1;

    63 else

    64 return 0;

    65 end

    66 else67 x = 0 ;

    68 return x ;

    69 end

    70 e n d f u n c t i o n

    52

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    54/82

    71 function [ x ] = i s r i g h t ( t r e e , t r e )

    72 i = 1 ;73 x = 0 ;

    74 while ( t r e e ( i ) ~ = - 2 )

    75 if ( t r e e ( i ) = = t r e )

    76 j = i ;

    77 end

    78 i = i + 1 ;

    79 end

    80 if ( i > = 2 * j + 1 )

    81 if ( ( t r e e ( 2 * j + 1 ) ~ = - 1 ) | ( t r e e ( 2 * j + 1 ) ~ = - 2 ) )

    82 x = 1 ;

    83 return 1;84 else

    85 return 0;

    86 end

    87 else

    88 x = 0 ;

    89 return x ;

    90 end

    91 e n d f u n c t i o n

    92 funcprot ( 0 ) ;

    93 function [ ] = i n o r d e r ( t r e e , p )

    94 if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 )

    95 return ;

    96 else

    97 inorder (tree ,2*p);

    98 disp ( t r e e ( p ) , ) ;99 inorder (tree ,2*p+1) ;

    100 end

    101 e n d f u n c t i o n

    102 function [ ] = p r e o r d e r ( t r e e , p )

    103 if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 )

    104 return ;105 else

    106 disp ( t r e e ( p ) , ) ;107 preorder (tree ,2*p);

    108 preorder (tree ,2*p +1);

    53

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    55/82

    109 end

    110 e n d f u n c t i o n111 function [ ] = p o s t o r d e r ( t r e e , p )

    112 if ( t r e e ( p ) = = - 1 | t r e e ( p ) = = - 2 )

    113 return ;

    114 else

    115 postorder (tree ,2*p);

    116 postorder (tree ,2*p +1);

    117 disp ( t r e e ( p ) , ) ;118 end

    119 e n d f u n c t i o n

    120 function [ t r e e 1 ] = b i n a r y ( t r e e , x )

    121 p = 1 ;122 while ( t r e e ( p ) ~ = - 1 & t r e e ( p ) ~ = - 2 )

    123 q = p ;

    124 if ( t r e e ( p ) > x )

    125 p = 2 * p ;

    126 else

    127 p = 2 * p + 1 ;

    128 end

    129 end

    130 i = 1 ;

    131 while ( t r e e ( i ) ~ = - 2 )

    132 i = i + 1 ;

    133 end

    134 if ( t r e e ( q ) > x )

    135 if ( i = = 2 * q )

    136 t r e e ( 2 * q ) = x ;

    137 t r e e ( 2 * q + 1 ) = - 2

    138 else

    139 if ( i < 2 * q )

    140 t r e e ( i ) = - 1 ;

    141 t r e e ( 2 * q + 1 ) = - 2 ;

    142 t r e e ( 2 * q ) = x ;143 end

    144 end

    145

    146 else

    54

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    56/82

    147 if ( i = = 2 * q + 1 )

    148 t r e e ( 2 * q + 1 ) = x ;149 t r e e ( 2 * q + 2 ) = - 2 ;

    150 else

    151 if ( i < 2 * q + 1 )

    152 t r e e ( i ) = - 1 ;

    153 t r e e ( 2 * q + 1 ) = x ;

    154 t r e e ( 2 * q + 2 ) = - 2 ;

    155 end

    156 end

    157

    158 end

    159 t r e e 1 = t r e e ;160 e n d f u n c t i o n

    161 / / C a l l i n g R o ut i n e :162 t r e e = m a k e t r e e ( 3 ) ;

    163 tree= binary( tree ,1);

    164 tree= binary( tree ,2);

    165 tree= binary( tree ,4);

    166 tree= binary( tree ,5);

    167 disp ( t r e e , B i n ar y t r e e t hu s o bt a i ne by i n s e r t i n g1 , 2 , 4 and5 i n t r e e r o o te d 3 i s : ) ;

    Scilab code Exa 5.4 example

    1 function [ t r e e 1 ] = b i n a r y ( t r e e , x )

    2 p = 1 ;

    3 while ( t r e e ( p ) ~ = - 1 & t r e e ( p ) ~ = - 2 )

    4 q = p ;

    5 if ( t r e e ( p ) > x )

    6 p = 2 * p ;7 else

    8 p = 2 * p + 1 ;

    9 end

    10 end

    55

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    57/82

    11 if ( t r e e ( q ) > x )

    12 if ( t r e e ( 2 * q ) = = - 2 )13 t r e e ( 2 * q ) = x ;

    14 t r e e ( 2 * q + 1 ) = - 2 ;

    15 else

    16 t r e e ( 2 * q ) = x ;

    17 end

    18 else

    19 if ( t r e e ( 2 * q + 1 ) = = - 2 )

    20 t r e e ( 2 * q + 1 ) = x ;

    21 t r e e ( 2 * q + 2 ) = - 2 ;

    22 else

    23 t r e e ( 2 * q + 1 ) = x ;24 end

    25 end

    26 t r e e 1 = t r e e ;

    27 e n d f u n c t i o n

    28 funcprot ( 0 ) ;

    29 function [ t r e e ] = m a k e t r e e ( x )

    30 t r e e = zeros ( 4 0 , 1 ) ;

    31 for i = 1 : 4 0

    32 t r e e ( i ) = - 1 ;

    33 end

    34 t r e e ( 1 ) = x ;

    35 t r e e ( 2 ) = - 2 ;

    36 e n d f u n c t i o n

    37 function [ ] = d u p l i c a t e 1 ( a , n )

    38 t r e e = m a k e t r e e ( a ( 1 ) ) ;

    39 q = 1 ;

    40 p = 1 ;

    41 i = 2 ;

    42 x = a ( i )

    43 while ( i < n )

    44 while ( t r e e ( p ) ~ = x & t r e e ( q ) ~ = - 1 & t r e e ( q ) ~ = - 2 )45 p = q ;

    46 if ( t r e e ( p ) < x )

    47 q = 2 * p ;

    48 else

    56

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    58/82

    49 q = 2 * p + 1 ;

    50 end51 end

    52 if ( t r e e ( p ) = = x )

    53 disp (x , Du p li ca te ) ;54 else

    55 t r e e = b i n a r y ( t r e e , x ) ;

    56 end

    57 i = i + 1 ;

    58 x = a ( i ) ;

    59 end

    60 while ( t r e e ( p ) ~ = x & t r e e ( q ) ~ = - 1 & t r e e ( q ) ~ = - 2 )

    61 p = q ;62 if ( t r e e ( p ) < x )

    63 q = 2 * p ;

    64 else

    65 q = 2 * p + 1 ;

    66 end

    67 end

    68 if ( t r e e ( p ) = = x )

    69 disp (x , Du p li ca te ) ;70 else

    71 t r e e = b i n a r y ( t r e e , x ) ;

    72 end

    73 e n d f u n c t i o n

    74 / / C a l l i n g A d r es s :75 a = [2 2 11 33 22 21 1 3 34]

    76 d u p l i c a t e 1 ( a , 6 )

    77 a = [2 1 11 33 22 22 33 4]

    78 d u p l i c a t e 1 ( a , 6 )

    57

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    59/82

    Chapter 6

    Graphs

    Scilab code Exa 6.1 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 6 . 1 ) ;4 / / Depth F i r s t S ea rc h T r a v er s a l5 funcprot (0)

    6 function [ ] = D f s ( a d j , n ) ;

    7 i = 1 , j = 1 ;8 c o l o u r = [ ] ;

    9 for i = 1 : n

    10 for j = 1 : n

    11 c o lo u r = [ c o l ou r ( : , : ) 0 ];

    12 end

    13 end

    14 disp ( The DFS t r a v e r s a l i s ) ;15 dfs(adj ,colour ,1,n) ;

    16 e n d f u n c t i o n

    17 function [ ] = d f s ( a d j , c o l o u r , r , n )18 c o l o u r ( r ) = 1 ;

    19 disp (r , ) ;20 for i = 1 : n

    21 if ( a d j ( ( r - 1 ) * n + i ) & ( c o l o u r ( i ) = = 0 ) )

    58

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    60/82

    22 d f s ( a d j , c o l o u r , i , n ) ;

    23 end24 end

    25 c o l o u r ( r ) = 2 ;

    26 e n d f u n c t i o n

    27 / / C a l l i n g R o ut i n e :28 n = 4 ;

    29 adj =[0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0]

    30 D f s ( a d j , n )

    Scilab code Exa 6.2 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 6 . 2 ) ;4 / / / / BFS T r a v e r s a l5 funcprot (0)

    6 function [ q 2 ] = p u s h ( e l e , q 1 )

    7 if ( q 1 . r e a r = = q 1 . f r o n t )

    8 q 1 . a = e l e ;

    9 q 1 . r e a r = q 1 . r e a r + 1 ;10 else

    11 q 1 . a =[ q 1 . a ( : , :) e l e ];

    12 q 1 . r e a r = q 1 . r e a r + 1 ;

    13 end

    14 q 2 = q 1 ;

    15 e n d f u n c t i o n

    16 function [ e l e , q 2 ] = p o p ( q 1 )

    17 e l e = - 1 ;

    18 q 2 = 0 ;

    19 if ( q 1 . r e a r = = q 1 . f r o n t )20 return ;

    21 else

    22 e l e = q 1 . a ( q 1 . r e a r - q 1 . f r o n t ) ;

    23 q 1 . f r o n t = q 1 . f r o n t + 1 ;

    59

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    61/82

    24 i = 1 ;

    25 a = q 1 . a ( 1 ) ;26 for i = 2 : ( q 1 . r e a r - q 1 . f r o n t )

    27 a = [ a ( : , :) q 1 . a ( i ) ];

    28 end

    29 q 1 . a = a ;

    30 end

    31 q 2 = q 1 ;

    32 e n d f u n c t i o n

    33

    34 function [ ] = B f s ( a d j , n ) ;

    35 i = 1 , j = 1 ;

    36 c o l o u r = [ ] ;37 for i = 1 : n

    38 for j = 1 : n

    39 c o lo u r = [ c o l ou r ( : , : ) 0 ];

    40 end

    41 end

    42 disp ( The BFS T r a v e r s a l i s ) ;43 bfs(adj ,colour ,1,n) ;

    44 e n d f u n c t i o n

    45 function [ ] = b f s ( a d j , c o l o u r , s , n )

    46 c o l o u r ( s ) = 1 ;

    47 q = s t r u c t ( r e a r ,0 , f r o n t ,0 , a ,0);48 q = p u s h ( s , q ) ;

    49 while ( ( q . r e a r ) - ( q . f r o n t ) > 0 )

    50 [ u , q ] = p o p ( q ) ;

    51 disp (u , ) ;52 for i = 1 : n

    53 if ( a d j ( ( u - 1 ) * n + i ) & ( c o l o u r ( i ) = = 0 ) )

    54 c o l o u r ( i ) = 1 ;

    55 q = p u s h ( i , q ) ;

    56 end

    57 end58 c o l o u r ( u ) = 2 ;

    59 end

    60 e n d f u n c t i o n

    61 / / C a l l i n g R o ut i n e :

    60

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    62/82

    62 n = 4 ;

    63 adj =[0 1 1 0 0 0 0 1 0 0 0 1 0 0 0 0]64 B f s ( a d j , n )

    Scilab code Exa 6.3 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 6 . 3 ) ;

    4 / / W a r s h al l s A l g o r i t hm5 clc ;

    6 c l e ar a ll ;

    7 funcprot (0)

    8 function [ p a t h ] = t r a n s c l o s e ( a d j , n )

    9 for i = 1 : n

    10 for j = 1 : n

    11 p a t h ( ( i - 1 ) * n + j ) = a d j ( ( i - 1 ) * n + j ) ;

    12 end

    13 end

    14 for k = 1 : n

    15 for i = 1 : n16 if ( p a t h ( ( i - 1 ) * n + k ) = = 1 )

    17 for j = 1 : n

    18 p a t h ( ( i - 1 ) * n + j ) = p a t h ( ( i - 1 ) * n + j ) | p a t h ( ( k - 1 )

    * n + j ) ;

    19 end

    20 end

    21 end

    22 end

    23 printf ( T r a n s i t i v e c l o s u r e f o r t h e g iv en g r aph i s

    :\ n ) ;24 for i = 1 : n25 printf ( F or v e r t e x %d \n , i ) ;26 for j = 1 : n

    27 printf ( %d %d i s %d\n , i , j , p a t h ( ( i - 1 ) * n + j ) ) ;

    61

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    63/82

    28 end

    29 end30 e n d f u n c t i o n

    31 / / C a l l i n g R o ut i n e :32 n = 3 ;

    33 adj =[0 1 0 0 0 1 0 0 0]

    34 p a t h = t r a n s c l o s e ( a d j , n )

    Scilab code Exa 6.4 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 6 . 4 ) ;4 / / F in nd in g T r a n s i t i v e C l os u r e5 funcprot ( 0 ) ;

    6 function [ p a t h ] = T r a n c l o s e ( a d j , n ) ;

    7 i = 1 , j = 1 ;

    8 p a t h = zeros ( n * n , 1 ) ;

    9 p a t h = t r a n c l o s e ( a d j , n ) ;

    10 printf ( T r a n s i t i v e C l o s u re Of G iv en Graph i s :\ n ) ;

    11 for i = 1 : n12 printf ( F o r V e r t ex %d\n , i ) ;13 for j = 1 : n

    14 printf ( %d %d i s %d\n , i , j , p a t h ( ( i - 1 ) * n + j ) ) ;15 end

    16 end

    17

    18 e n d f u n c t i o n

    19 function [ p a t h ] = t r a n c l o s e ( a d j , n )

    20 a d j p r o d = zeros ( n * n , 1 ) ;

    21 k = 1 ;22 n e w p r o d = zeros ( n * n , 1 ) ;

    23 for i = 1 : n

    24 for j = 1 : n

    25 p a t h ( ( i - 1 ) * n + j ) = a d j ( ( i - 1 ) * n + j ) ;

    62

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    64/82

    26 a d j pr o d ( ( i - 1) * n + j ) = p a th ( ( i - 1 ) * n + j ) ;

    27 end28 end

    29 for i = 1 : n

    30 n e w p r o d = p r o d 1 ( a d j p r o d , a d j , n ) ;

    31 for j = 1 : n

    32 for k = 1 : n

    33 p a t h ( ( j - 1 ) * n + k ) = p a t h ( ( j - 1 ) * n + k ) | n e w p r o d ( ( j

    - 1 ) * n + k ) ;

    34 end

    35 end

    36 for j = 1 : n

    37 for k = 1 : n38 a d j p r o d ( ( j - 1 ) * n + k ) = n e w p r o d ( ( j - 1 ) * n + k ) ;

    39 end

    40 end

    41 end

    42 e n d f u n c t i o n

    43 function [ c ] = p r o d 1 ( a , b , n )

    44 for i = 1 : n

    45 for j = 1 : n

    46 val=0

    47 for k = 1 : n

    48 v a l = v a l | ( a ( ( i - 1 ) * n + k ) & b ( ( k - 1 ) * n + j ) ) ;

    49 end

    50 c ( ( i - 1 ) * n + j ) = v a l ;

    51 end

    52 end

    53 e n d f u n c t i o n

    54 / / C a l l i n g R o ut i n e :55 n = 3 ;

    56 adj =[0 1 0 0 0 1 0 0 0]

    57 p a t h = T r a n c l o s e ( a d j , n )

    Scilab code Exa 6.5 example

    63

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    65/82

    1 c l e ar a ll ;

    2 clc ;3 disp ( E xa mp le 6 . 5 ) ;4 / / F i n d i n g The Number Of S i m p l e P a th s From One P o i n t

    To A n ot h er I n A G iv en Graph5 funcprot (0)

    6 function [ ] = s i m _ p a t h ( n , a d j , i , j ) ;

    7 l = 0 ;

    8 m = 1 ;

    9 for m = 1 : n

    10 l = l + p a t h ( m , n , a d j , i , j ) ;

    11 end

    12 printf ( Ther e a r e %d s i m pl e p at hs from %d t o %di n t he g i ve n gr a ph \n , l , i , j ) ;

    13 e n d f u n c t i o n

    14 function [ b ] = p a t h ( k , n , a d j , i , j )

    15 b = 0 ;

    16 if ( k = = 1 )

    17 b = a d j ( ( i - 1 ) * n + j ) ;

    18 else

    19 for c = 1 : n

    20 if ( a d j ( ( i - 1 ) * n + c ) = = 1 )

    21 b = b + p a t h ( k - 1 , n , a d j , c , j ) ;

    22 end

    23 end

    24 end

    25 return b ;

    26 e n d f u n c t i o n

    27 n = 3 ;

    28 adj =[0 1 1 0 0 1 0 0 0];

    29 b = s i m _ p a t h ( n , a d j , 1 , 3 )

    Scilab code Exa 6.6 example

    1 c l e ar a ll ;

    64

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    66/82

    2 clc ;

    3 disp ( E xa mp le 6 . 6 ) ;4 / / D i j k s t r a s A lg o ri th m5 funcprot (0)

    6 function [ l ] = s h o r t ( a d j , w , i 1 , j 1 , n )

    7 for i = 1 : n

    8 for j = 1 : n

    9 if ( w ( ( i - 1 ) * n + j ) = = 0 )

    10 w ( ( i - 1 ) * n + j ) = 9 9 9 9 ;

    11 end

    12 end

    13 end

    1415 d i s t a n c e = [ ] ;

    16 p e r m = [ ] ;

    17 for i = 1 : n

    18 d i s ta n c e = [ d i s ta n c e ( : , :) 9 9 99 9 ] ;

    19 p e rm = [ p e r m ( : , :) 0 ] ;

    20 end

    21 p e r m ( i 1 ) = 1 ;

    22 d i s t a n c e ( i 1 ) = 0 ;

    23 c u r r e n t = i 1 ;

    24 while ( c u r r e n t ~ = j 1 )

    25 s m a l l d i s t = 9 9 9 9 ;

    26 d c = d i s t a n c e ( c u r r e n t ) ;

    27 for i = 1 : n

    28 if ( p e r m ( i ) = = 0 )

    29 n e w d i s t = d c + w ( ( c u r r e n t - 1 ) * n + i ) ;

    30 if ( n e w d i st < d i s t a n c e ( i ) )

    31 d i s t a n c e ( i ) = n e w d i s t ;

    32 end

    33 if ( d i s t a n c e ( i ) < s m a l l d i s t )

    34 s m a l l d i s t = d i s t a n c e ( i ) ;

    35 k = i ;36 end

    37 end

    38 end

    39 c u r r e n t = k ;

    65

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    67/82

    40 p e r m ( c u r r e n t ) = 1 ;

    41 end42 l = d i s t a n c e ( j 1 ) ;

    43 printf ( The s h o r t e s t p at h b et we en %d and %d i s %d , i 1 , j 1 , l ) ;

    44 e n d f u n c t i o n

    45 / / C a l l i n g R o ut i n e :46 n = 3 ;

    47 adj =[0 1 1 0 0 1 0 0 0] // A dj a ce nc y L i s t48 w =[0 12 22 0 0 9 0 0 0] / / w ei g ht l i s t f i l l 0 f o r no

    e d g e49 s h o r t ( a d j , w , 1 , 3 , n ) ;

    Scilab code Exa 6.7 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 6 . 7 ) ;4 / / F i n d i n g The Number Of P a t h s From One V e r t e x To

    A n ot h er Of A G iv en L e ng t h

    56 function [ b ] = p a t h ( k , n , a d j , i , j )

    7 b = 0 ;

    8 if ( k = = 1 )

    9 b = a d j ( ( i - 1 ) * n + j ) ;

    10 else

    11 for c = 1 : n

    12 if ( a d j ( ( i - 1 ) * n + c ) = = 1 )

    13 b = b + p a t h ( k - 1 , n , a d j , c , j ) ;

    14 end

    15 end16 end

    17 printf ( Number o f p a t h s f ro m v e r t e x %d t o %d o f l e n g t h %d a r e %d , i , j , k , b ) ;

    18 return b ;

    66

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    68/82

    19 e n d f u n c t i o n

    20 / / C a l l i n g R o ut i n e :21 n = 3 ;22 adj =[0 1 1 0 0 1 0 0 0]

    23 b = p a t h ( 1 , n , a d j , 1 , 3 )

    67

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    69/82

    Chapter 7

    Sorting

    Scilab code Exa 7.1 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 7 . 1 ) ;4 funcprot ( 0 ) ;

    5 function [ a 1 ] = i n s e r t i o n ( a , n )

    6 for k = 1 : n

    7 y = a ( k ) ;8 i = k ;

    9 while ( i > = 1 )

    10 if ( y < a ( i ) )

    11 a ( i + 1 ) = a ( i ) ;

    12 a ( i ) = y ;

    13 end

    14 i = i - 1 ;

    15 end

    16 end

    17 a 1 = a ;18 disp ( a 1 , S o r te d a r ra y i s : ) ;19 e n d f u n c t i o n

    20 / / C a l l i n g R o ut i n e :21 a =[5 4 3 2 1] / / w or stc a se b eh av io ur o f

    68

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    70/82

    i n s e r t i o n s o r t .

    22 disp (a , G i ve n A rr a y ) ;23 a 1 = i n s e r t i o n ( a , 5 )

    Scilab code Exa 7.2 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 7 . 2 ) ;

    4 funcprot ( 0 ) ;5 function [ a 1 ] = i n s e r t i o n ( a , n )

    6 for k = 1 : n

    7 y = a ( k ) ;

    8 i = k ;

    9 while ( i > = 1 )

    10 if ( y < a ( i ) )

    11 a ( i + 1 ) = a ( i ) ;

    12 a ( i ) = y ;

    13 end

    14 i = i - 1 ;

    15 end16 end

    17 a 1 = a ;

    18 disp ( a 1 , S o r te d a r ra y i s : ) ;19 e n d f u n c t i o n

    20 / / C a l l i n g R o ut i n e :21 a =[2 3 4 5 1]

    22 disp (a , G i ve n A rr a y ) ;23 a 1 = i n s e r t i o n ( a , 5 )

    Scilab code Exa 7.3 example

    1 c l e ar a ll ;

    69

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    71/82

    2 clc ;

    3 disp ( E xa mp le 7 . 3 ) ;4 funcprot ( 0 ) ;5 function [ a 1 ] = q u i c k ( a ) ;

    6 a = gsort ( a ) ; // IN BUILT QUICK SORT FUNCTION7 n = length ( a ) ;

    8 a 1 = [ ] ;

    9 for i = 1 : n

    10 a 1 = [ a1 ( : , : ) a ( n + 1 - i ) ];

    11 end

    12 disp ( a 1 , S o r te d a r ra y i s : ) ;13 e n d f u n c t i o n

    14 / / C a l l i n g R o ut i n e :15 a =[26 5 37 1 61 11 59 15 48 19]

    16 disp (a , G i ve n A rr a y ) ;17 a 1 = q u i c k ( a )

    Scilab code Exa 7.4 example

    1 c l e ar a ll ;

    2 clc ;3 disp ( E xa mp le 7 . 4 ) ;4 function [ a 1 ] = i n s e r t i o n ( a , n )

    5 for k = 1 : n

    6 y = a ( k ) ;

    7 i = k ;

    8 while ( i > = 1 )

    9 if ( y < a ( i ) )

    10 a ( i + 1 ) = a ( i ) ;

    11 a ( i ) = y ;

    12 end13 i = i - 1 ;

    14 end

    15 end

    16 a 1 = a ;

    70

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    72/82

    17 disp ( a 1 , S o r te d a r ra y i s : ) ;

    18 e n d f u n c t i o n19 / / C a l l i n g R o ut i n e :20 a = [3 1 2]

    21 disp (a , G i ve n A rr a y ) ;22 a 1 = i n s e r t i o n ( a , 3 )

    Scilab code Exa 7.5 example

    1 c l e ar a ll ;2 clc ;

    3 disp ( E xa mp le 7 . 5 ) ;4 funcprot ( 0 ) ;

    5 function [ a 1 ] = m e r g e s o r t ( a , p , r )

    6 if ( p < r )

    7 q = int ( ( p + r ) / 2 ) ;

    8 a = m e r g e s o r t ( a , p , q ) ;

    9 a = m e r g e s o r t ( a , q + 1 , r ) ;

    10 a = m e r g e ( a , p , q , r ) ;

    11 else

    12 a 1 = a ;13 return ;

    14 end

    15 a 1 = a ;

    16 e n d f u n c t i o n

    17 function [ a 1 ] = m e r g e ( a , p , q , r )

    18 n 1 = q - p + 1 ;

    19 n 2 = r - q ;

    20 l e f t = zeros ( n 1 + 1 ) ;

    21 r i g h t = zeros ( n 2 + 1 ) ;

    22 for i = 1 : n 123 l e f t ( i ) = a ( p + i - 1 ) ;

    24 end

    25 for i 1 = 1 : n 2

    26 r i g h t ( i 1 ) = a ( q + i 1 ) ;

    71

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    73/82

    27 end

    28 l e f t ( n 1 + 1 ) = 9 9 9 9 9 9 9 9 9 ;29 r i g h t ( n 2 + 1 ) = 9 9 9 9 9 9 9 9 9 ;

    30 i = 1 ;

    31 j = 1 ;

    32 k = p ;

    33 for k = p : r

    34 if ( l e f t ( i ) < = r i g h t ( j ) )

    35 a ( k ) = l e f t ( i ) ;

    36 i = i + 1 ;

    37 else

    38 a ( k ) = r i g h t ( j ) ;

    39 j = j + 1 ;40 end

    41 end

    42 a 1 = a ;

    43 e n d f u n c t i o n

    44 / / C a l l i n g R o ut i n e :45 a =[26 5 77 1 61 11 59 15 48 19]

    46 disp (a , G i ve n A rr a y ) ;47 a 1 = m e r g e s o r t ( a , 1 , 1 0 )

    48 disp ( a 1 , S o r te d a r ra y i s : ) ;

    Scilab code Exa 7.6 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 7 . 7 ) ;4 function [ a 1 ] = s h e l l ( a , n , i n c r , n i c )

    5 for i = 1 : n i c

    6 s p a n = i n c r ( i ) ;7 for j = s p a n + 1 : n

    8 y = a ( j ) ;

    9 k = j - s p a n ;

    10 while ( k > = 1 & y < a ( k ) )

    72

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    74/82

    11 a ( k + s p a n ) = a ( k ) ;

    12 k = k - s p a n ;13 end

    14 a ( k + s p a n ) = y ;

    15 end

    16 end

    17 a 1 = a ;

    18 disp ( a 1 , S o r te d a r ra y i s : ) ;19 e n d f u n c t i o n

    20 / / C a l l i n g R o ut i n e :21 a = [ 23 21 2 32 1 21 2 32 4 1 22 24 33 1 21 2]

    22 disp (a , G i ve n A rr a y ) ;

    23 i nc r =[ 5 3 1] // must a l wa y s end w it h 124 a1= shell(a ,7,incr ,3)

    Scilab code Exa 7.7 example

    1 c l e ar a ll ;

    2 clc ;

    3 disp ( E xa mp le 7 . 7 ) ;

    4 function [ a 1 ] = s h e l l ( a , n , i n c r , n i c )5 for i = 1 : n i c

    6 s p a n = i n c r ( i ) ;

    7 for j = s p a n + 1 : n

    8 y = a ( j ) ;

    9 k = j - s p a n ;

    10 while ( k > = 1 & y < a ( k ) )

    11 a ( k + s p a n ) = a ( k ) ;

    12 k = k - s p a n ;

    13 end

    14 a ( k + s p a n ) = y ;15 end

    16 end

    17 a 1 = a ;

    18 disp ( a 1 , S o r te d a r ra y i s : ) ;

    73

  • 8/12/2019 Fundamentals of Data Structure in C_S. Sahni , S. Anderson-Freed and E. Horowitz (1)

    75/82

    19 e n d f u n c t i o n

    20 / / C a l l i n g R o ut i n e :21 a = [ 23 21 2 32 1 21 2 32 4 1 22