Professional Documents
Culture Documents
Horowitz1
Created by Aakash Yadav B-TECH Computer Engineering Swami Keshvanand Institute of Technology College Teacher Richa Rawal Cross-Checked by Lavitha Pereira August 17, 2013
by a grant from the National Mission on Education through ICT, http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab codes written in it can be downloaded from the Textbook Companion Project section at the website http://scilab.in
1 Funded
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
Scilab numbering policy used in this document and the relation to the above 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 particular Example of the above book) For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means a scilab code whose theory is explained in Section 2.3 of the book.
Contents
List of Scilab Codes 1 Basic concepts 2 Arrays and Structures 3 Stacks and Queues 4 Linked lists 5 Trees 6 Graphs 7 Sorting 8 Hashing 9 Priority Queues 4 6 16 19 31 44 58 68 76 78
Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa Exa
4.4 5.1 5.2 5.3 5.4 6.1 6.2 6.3 6.4 6.5 6.6 6.7 7.1 7.2 7.3 7.4 7.5 7.6 7.7 7.8 8.1 8.2 9.1
example example example example example example example example example example example example example example example example example example example example example example example
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . . . . . . . . .
39 44 47 51 55 58 59 61 62 63 64 66 68 69 69 70 71 72 73 74 76 77 78
list
22 23
// t o do b i n a r y s e a r c h . . function []= search ( a ) i =1; [j , k ]= size ( a ) ; for i =1: k if z == a ( i ) then printf ( \nFOUND and i n d e x no . i s =%d\ t ,i ) ; end end funcprot (0) ; endfunction // c a l l i n r o u t i n e a =[5 7 45 28 99] z =45 binary = search ( a )
// t o do b i n a r y s e a r c h . . function []= search ( a ) i =1; [j , k ]= size ( a ) ; for i =1: k if x == a ( i ) then printf ( \nFOUND and i n d e x no . i s =%d\ t ,i ) ; 8 end 7
);
7 y = perms ( x ) ; 8 disp ( y ) ;
// //
example 1 . 5 ADT( A b s t r a c t Data t y p e ) d e f i n a t i o n o f n a t u r a l number . function []= ADT ( x ) printf ( ADT n a t u r a l no . i s ) ; printf ( \ nOBJECTS : an 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 e r o and ) ; 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 o m p u t e r ) ; INT_MAX =32767; if x ==0 // N a t u r a l N u m b e r Z e r o ( ) 8
9 10 11 12 13 14 15 16 17 18 19
printf ( \ n ,0) ; end if x == INT_MAX then // NaturalNumberSuccessor ( x ) printf ( \ n a n s . i s =%d ,x ) ; else printf ( \ n a n s . i s =%d ,x +1) ; end endfunction // c a l l i n r o u t i n e x =56 y = ADT ( x ) ;
// f u n c t i o n abc a c c e p t i n g o n l y t h r e e s i m p l e v a r i a b l e s given the f u n c t i o n has // o n l y f i x e d s a c e r e q u i r e m e n t . . function []= abc (a ,b , c ) x = a + b + c * c +( a +b - c ) /( a + b ) +4.00; disp ( x ) ; funcprot (0) ; endfunction .... // c a l l i n g r o u t i n e a =[1] , b =[2] , c =[3] abc (a ,b , c )
5 6 7 8 9 10
i s =%d ,
clear all ; clc ; printf ( \ n Example 1 . 8 ) ; a =[2;5;4;64;78] i =1; x =1;............ // i n i t i a l i s i n g sum e q u a l s t o one . 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 . while i <6 c = c + a ( i ) ;........ // sum x = x +1;............... // // s t e p c o u n t i = i +1; end printf ( \ n no . i n t h e l i s t a r e a= ) printf ( \ n %d ,a ) ; printf ( \ n sum i s =%d ,(c -1) ) ; printf ( \ n c o u n t i s =%d ,(x -1) ) ;
10
o f r o w s =3 , no . o f c o l . o f r o w s =3 , no . o f c o l
=2.
7 y = matrix (b ,3 ,2) ;........ // no , 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
.=2. printf ( m a t r i x x= ) ; disp ( x ) ; printf ( m a t r i x y= ) ; disp ( y ) ; [p , q ]= size ( x ) ; i =1; j =1; c =1; for i =1: p for j =1: q z (i , j ) = x (i , j ) + y (i , j ) ;.......... // summing two matrices c = c +1;........... // s t e p c o u n t end end printf ( \ n R e s u l t a n t m a t r i x a f t e r a d d i t i o n = ) ; disp ( z ) ;............. // d i s p l a y i n sum o f two m a t r i c e s . printf ( \ n s t e p c o u n t i s =%d ,(c -1) ) ;
11
8 9 10 11 12 13 14
clear all ; clc ; printf ( \ n Example 1 . 1 1 ) ; // M a t r i x a d d i t i o n . a =[1 2 3;4 5 6]; b =[7 8 9;10 11 12]; x = matrix (a ,3 ,2) ;........ // no . o f r o w s =3 , no . o f c o l . =2. y = matrix (b ,3 ,2) ;........ // no , o f r o w s =3 , no . o f c o l .=2. printf ( m a t r i x x= ) ; disp ( x ) ; printf ( m a t r i x y= ) ; disp ( y ) ; [p , q ]= size ( x ) ; i =1; j =1; for i =1: p for j =1: q z (i , j ) = x (i , j ) + y (i , j ) ;.......... // summing two matrices end end printf ( \ n R e s u l t a n t m a t r i x a f t e r a d d i t i o n = ) ; disp ( z ) ;............. // d i s p l a y i n sum o f two m a t r i c e s 12
9 10 11 12 13
14
;............ // O( n ) i s c a l l e d l i n e a r . printf ( \ n \ n 3 n+2=O( n ) a s 100 n+6<=101n f o r a l l n > =10. ) ; printf ( \ n \ n 10 n2+4n+2=O( n 2 ) a s 10 n2+4n+2<=11n 2 f o r n > =5. ) ;.......... //O( n 2 ) i s c a l l e d quadratic . printf ( \ n \ n 1 0 0 0 n 2+100n6=O( n 2 ) a s 1 0 0 0 n 2+100n 6 <=1001n 2 f o r n > =100. ) ; printf ( \ n \ n 6 2 n+n2 < =7 2n f o r n>=4 ) ; printf ( \ n \ n 3 n+3=O( n 2 ) a s 3 n+3<=3n 2 f o r n>=2 ) ; printf ( \ n \ n 10 n2+4n+2=O( n 4 ) a s 10 n2+4n+2<=10n 4 f o r n > =2. ) ; printf ( \ n \ n 3 n+2 i s n o t O( 1 ) a s 3 n+2 i s l e s s t h a n o r e q u a l t o c f o r any c o n s t a n t c and a l l n , n>=n0 . ) ;............ // O( 1 ) means c o m p u t i n g t i m e i s constant . printf ( \ n \ n 10 n2+4n+2 i s n o t O( n ) ) ;
13
4 5 6 7 8 9 10 11
printf ( \ n \ n [ Omega ] f ( n )=omega ( g ( n ) ) ) ; printf ( \ n \ n 3 n+2=omega ( n ) a s 3 n+2>=3n f o r n>=1 ) ; printf ( \ n \ n 3 n+3=omega ( n ) a s 3 n+3>=3n f o r n>=1 ) ; printf ( \ n \ n 100 n+6=omega ( n ) a s 100 n+6>=100n f o r n >=1 ) ; printf ( \ n \ n 10 n2+4n+2=omega ( n 2 ) a s 10 n2+4n+2>=n 2 f o r n>=1 ) ; printf ( \ n \ n 6 2 n+n2=omega ( n ) a s 6 2 n+n2 >=2n f o r n>=1 ) ; printf ( \ n \ n 3 n+3=omega ( 1 ) ) ; printf ( \ n \ n \ t [ Omega ] f ( n )=omega ( 1 ) ) ;
al
n>=2
); printf ( \ n \ n 3 n+3= t h e t a ( n ) ) ; printf ( \ n \ n 10 n2+4n+2= t h e t a ( n 2 ) ) ; printf ( \ n \ n 6 2 n+n2= t h e t a ( 2 n ) ) ; printf ( \ n \ n 3 n+2 i s n o t t h e t a ( 1 ) ) ; printf ( \ n \ n 3 n+3 i s n o t t h e t a ( n 2 ) \ n ) ; printf ( \ n \ n The Theta n o t a t i o n i s more p r e c i s e t h a n b o t h b i g oh and omega n o t a i o n ) ;
14
4 5
7 8 9 10 11 12
// how v a r i o u s f u n c t i o n s grow w i t 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 e i n g shown . // l i k e f u n c t i o n 2 n g r o w s v e r y r a p i d l y w i t h n . and u t i l i t y o f programs with e x p o n e n t i a l complexity i 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 < =40) . n =[ 1 2 3 4 5 6];....... // t a k i n v a l u e o f n from 1 t o 10 t o o b s e r v e t h e v a r i a t i o n i n v a r i o u s functions . plot ( log ( n ) ) ; plot (2^ n ) ; plot ( n ) ; plot ( n ^2) ; xtitle ( P l o t o f f u n c t i o n v a l u e s , n > , f > ) ; printf ( \ n \ n X a x i s i s r e p r e s e n t e d by v a l u e s o f n and Y a x i s i f r e p r e s e n t e d by f ) ;
15
s t r i n g or c h a r a c t e r
clear all ; clc ; printf ( \ nExample 2 . 3 \ n ) ; // c o m p a r i s i o n o f 2 s t r i n g s . a = hakunah ;.......... // s t r i n g 1 . b = matata ;............. // s t r i n g 2 . disp ( a & b r e s p e c t i v e l y a r e = ) ; disp ( a ) ; disp ( b ) ; disp ( c o m p a r i n g s t r i n g s ) ; z = strcmp (a , b ) ;......... // c o m p a r i s i o n o f 2 s t r i n g s . if ( z ==0) printf ( \nMATCHED\ n ) ;....... // i f s t r i n g s matched s t r c m p r e t u r n s 0 . else printf ( \nNOT MATCHED\ n ) ;......... // i f s t r i n g doesn t matched s t r c m p r e t u r n s 1. end q= akash ; w= akash ; disp ( q & w r e s p e c t i v e l y a r e= ) ; disp ( q ) ; disp ( w ) ; disp ( c o m p a r i n g s t r i n g s ) ; x = strcmp (q , w ) ; 17
24 25 26 27 28
strings
string
18
// E x e r c i s e q u e s t i o n 2 : // I m p l e m e n t i n g Push And Pop F u n c t i o n s : function [y , sta1 ]= empty ( sta ) y =0; sta1 =0; if ( sta . top ==0) y =0; else y =1; end sta1 = sta endfunction function [ sta ]= push ( stac , ele ) sta =0; if ( empty ( stac ) ==0) stac . a = ele ; stac . top = stac . top +1; else stac . a =[ stac . a (: ,:) ele ] stac . top = stac . top +1; 19
22 end 23 disp ( stac ) ; 24 sta = stac ; 25 funcprot (0) 26 endfunction 27 28 function [ ele , sta ]= pop ( stack ) 29 ele = 1 ; 30 if ( empty ( stack ) ==0) 31 disp ( S t a c k U n d e r f l o w ) ; 32 break ; 33 else 34 ele = stack . a ( stack . top ) ; 35 stack . top = stack . top -1; 36 if ( stack . top ~=0) 37 b = stack . a (1) ; 38 for i2 =2: stack . top 39 b =[ b (: ,:) stack . a ( i2 ) ]; 40 end 41 stack . a = b ; 42 else 43 stack . a = 0 ; 44 end 45 end 46 disp ( stack ) ; 47 sta = stack ; 48 endfunction 49 global stack 50 // C a l l i n g R o u t i n e : 51 stack = struct ( a ,0 , t o p ,0) ; 52 stack = push ( stack ,4) ; 53 stack = push ( stack ,55) ; 54 stack = push ( stack ,199) ; 55 stack = push ( stack ,363) ; 56 [ ele , stack ]= pop ( stack ) ; 57 disp ( stack , A f t e r t h e a b o v e o p e r a t i o n s
s t a c k i s : );
20
5 6 7 8 9 10 11
in f i r s t out . so p r i n t i n g o u t a r r a y from l a s t t o f i r s t w i l l be same a s s t a c k . a =[12;35;16;48;29;17;13] i =7; printf ( \ t s t a c k = ) ; while i >0 printf ( \ n \ t%d ,a ( i ) ) ; i =i -1; end
21
15 stack = struct ( t o p ,l , i t e m s ,a ) ; , 16 case c h a r then 17 a = string ( a ) ; 18 stack = struct ( t o p ,l , i t e m s ,a ) ; , 19 end 20 disp ( stack , S t a c k i s : ) ; 21 endfunction 22 a =[32 12.34 232 32.322] 23 stack = sta_union ( f l o a t ,a ) 24 stack = sta_union ( i n t ,a ) 25 stack = sta_union ( c h a r ,a )
22
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
if ( stack . top ==0) stack . a = str ( l ) ; stack . top = stack . top +1; else stack . a =[ stack . a (: ,:) str ( l ) ]; stack . top = stack . top +1; end disp ( stack ) ; end if ( str ( l ) == ) | str ( l ) == ] | str ( l ) == } ) if ( stack . top ==0) valid =0; break ; else i = stack . a ( stack . top ) ; b = stack . a (1) ; for i1 =2: stack . top -1 b =[ b (: ,:) stack . a ( i1 ) ] end stack . a = b ; stack . top = stack . top -1; symb = str ( l ) ; disp ( stack ) ; if ((( symb == ) ) &( i == ( ) ) |(( symb == ] ) &( i == [ ) ) |(( symb == } ) &( i == { ) ) ) else valid =0; break ; end end end l = l +1; end if ( stack . top ~=0) valid =0; end if ( valid ==0) disp ( I n v a l i d S t r i n g ) ; 23
59 60 61 62 63 64 65 66 67 68
} ) ]) B ] [ ( ) { C + ] ]) { ( [ J ) ) ) ])
24
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
disp ( Queue U n d e r f l o w ) ; return ; else ele = q1 . a ( q1 . rear - q1 . front ) ; q1 . front = q1 . front +1; i =1; a = q1 . a (1) ; for i =2:( q1 . rear - q1 . front ) a =[ a (: ,:) q1 . a ( i ) ]; end q1 . a = a ; end q2 = q1 ; endfunction funcprot (0) ; // C a l l i n g R o u t i n e : q1 = struct ( a ,0 , r e a r ,0 , f r o n t ,0) q1 = push (3 , q1 ) q1 = push (22 , q1 ) ; q1 = push (21 , q1 ) ; disp ( q1 , Queue a f t e r i n s e r t i o n ) ; [ ele , q1 ]= pop ( q1 ) disp ( ele , poped e l e m e n t ) ; disp ( q1 , Queue a f t e r p o p i n g ) ; [ ele , q1 ]= pop ( q1 ) ; [ ele , q1 ]= pop ( q1 ) ; [ ele , q1 ]= pop ( q1 ) ; // U n d e r f l o w C o n d i t i o n
25
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
l =0; [j , k ]= size ( x ) for i =1: k l = l + length ( x ( i ) ) ; end endfunction function []= str ( st ) stack = struct ( a ,0 , t o p ,0) ; st = string ( st ) ; l =1; l1 = strlen ( st ) ; symb = st ( l ) ; valid =1; while (l < l1 ) while ( symb ~= C ) if ( stack . top ==0) stack . a = st ( l ) ; stack . top = stack . top +1; else stack . a =[ stack . a (: ,:) st ( l ) ]; stack . top = stack . top +1; end l = l +1; symb = st ( l ) ; end i = st ( l +1) ; if ( stack . top ==0) valid =0; break ; else symb1 = stack . a ( stack . top ) ; stack . top = stack . top -1; if ( i ~= symb1 ) valid =0; break ; end end l = l +1; 26
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
end if ( stack . top ~=0) valid =0; end if ( valid ==0) disp ( Not o f t h e g i v e n f o r m a t ) ; else disp ( S t r i n g Of t h e Given Format ) ; end endfunction // C a l l i n g R o u t i n e : st =[ A A B A C A B A A ] str ( st ) st =[ A A B A C A B A ] str ( st )
// S o l v e d Example 3 . 3 : // C o n v 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 Expression : function [ sta ]= push ( stac , ele ) sta =0; if ( stac . top ==0) stac . a = ele ; stac . top = stac . top +1; else stac . a =[ stac . a (: ,:) ele ] stac . top = stac . top +1; end disp ( stac ) ; sta = stac ; endfunction function [ ele , sta ]= pop ( stack ) 27
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
ele = 1 ; if ( stack . top ==0) disp ( S t a c k U n d e r f l o w ) ; break ; else ele = stack . a ( stack . top ) ; stack . top = stack . top -1; if ( stack . top ~=0) b = stack . a (1) ; for i2 =2: stack . top b =[ b (: ,:) stack . a ( i2 ) ]; end stack . a = b ; else stack . a = 0 ; end end sta = stack ; endfunction function [ l ]= strlen ( x ) i =1; l =0; [j , k ]= size ( x ) for i =1: k l = l + length ( x ( i ) ) ; end endfunction function [ p ]= pre ( s1 , s2 ) i1 =0; select s1 , case + then i1 =5; case then i1 =5; case then i1 =9; case / then i1 =9; end i2 =0; select s2 , case + then i2 =5; 28
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
case then i2 =5; case then i2 =9; case / then i2 =9; end p =0; p = i1 - i2 ; if ( s1 == ( ) p = -1; end if ( s2 == ( & s1 ~= ) ) p = -1; end if ( s1 ~= ( & s2 == ) ) p =1; end endfunction function [ a2 ]= intopo ( a1 , n ) stack = struct ( a ,0 , t o p ,0) ; l1 =1; l2 = strlen ( a1 (1) ) for i =2: n l2 = l2 + strlen ( a1 ( i ) ) end a2 = list () ; while ( l1 <= l2 ) symb = a1 ( l1 ) ; if ( isalphanum ( string ( a1 ( l1 ) ) ) ) a2 = list ( a2 , symb ) ; else while ( stack . top ~=0&( pre ( stack . a ( stack . top ) , symb ) >=0) ) [ topsymb , stack ]= pop ( stack ) ; if ( topsymb == ) | topsymb == ( ) a2 = a2 ; else a2 = list ( a2 , topsymb ) ; end 29
92 end 93 if ( stack . top ==0| symb ~= ) ) 94 stack = push ( stack , symb ) ; 95 else 96 [ ele , stack ]= pop ( stack ) ; 97 end 98 end 99 l1 = l1 +1; 100 end 101 while ( stack . top ~=0) 102 [ topsymb , stack ]= pop ( stack ) ; 103 if ( topsymb == ) | topsymb == ( ) 104 a2 = a2 ; 105 else 106 a2 = list ( a2 , topsymb ) ; 107 end 108 end 109 disp ( a2 ) ; 110 endfunction 111 // C a l l i n g R o u t i n e : 112 a1 =[ ( 2 + 3 ) ( 5 113 a2 = intopo ( a1 ,11)
4 ) ]
30
; 6 disp ( x= ) ; 7 disp ( x ) ;
31
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
(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 ,0) ; if ( link1 (1) (1) . add ==0) link1 (1) (1) . data = ele ; link1 (1) (1) . add =1; link1 (1) (1) . nexadd =1; link2 (1) = link1 (1) (1) ; else if ( link1 (1) (1) . nexadd == link1 (1) (1) . add ) lin2 = link1 (1) (1) ; lin2 . data = ele ; lin2 . add = link1 (1) (1) . add +1; link1 (1) (1) . nexadd = lin2 . add ; lin2 . nexadd = link1 (1) (1) . add ; link2 (1) = link1 (1) (1) ; link2 (2) = lin2 ; else lin2 = link1 (1) (1) ; i =1; while ( link1 ( i ) (1) . nexadd ~= link1 (1) (1) . add ) i = i +1; end j=i; lin2 . data = ele ; lin2 . add = link1 ( i ) . add +1; lin2 . nexadd = link1 (1) (1) . add ; link1 ( i ) . nexadd = lin2 . add ; link2 (1) = link1 (1) (1) ; i =2; while ( link1 ( i ) . nexadd ~= lin2 . add ) link2 ( i ) =( link1 ( i ) ) ; i = i +1; end link2 ( i ) = link1 ( i ) ; link2 ( i +1) = lin2 ; end end endfunction 32
44 45
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
function [ link2 ]= add ( ele , pos , link1 ) ; link2 = 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 ,0) ; i =1; while (i <= pos ) if ( link1 ( i ) . nexadd == link1 (1) (1) . add ) break ; else i = i +1; end end if ( link1 ( i ) . nexadd ~= link1 (1) (1) . add ) i =i -1; lin2 . data = ele ; lin2 . add = i ; j=i; while ( link1 ( j ) . nexadd ~= link1 (1) (1) . add ) link1 ( j ) . add = link1 ( j ) . add +1; link1 ( j ) . nexadd = link1 ( j ) . nexadd +1; j = j +1; end link1 ( j ) . add = link1 ( j ) . add +1; lin2 . nexadd = link1 ( i ) . add ; link1 (i -1) . nexadd = lin2 . add ; k =1; while (k < i ) link2 ( k ) = link1 ( k ) ; k = k +1; end link2 ( k ) = lin2 ; k = k +1; link2 ( k ) = link1 (k -1) ; k = k +1 l =k -1; while ( k ~= j ) link2 ( k ) = link1 ( l ) ; k = k +1; 33
l = l +1; end link2 ( j ) = link1 (j -1) ;; link2 ( j +1) = link1 ( j ) ; else if ( i == pos ) k =1; lin2 . data = ele ; lin2 . add = link1 (i -1) . add +1; link1 ( i ) . add = link1 ( i ) . add +1; lin2 . nexadd = link1 ( i ) . add ; link1 ( i ) . nexadd = link1 (1) (1) . add ; k =1; while (k < pos ) link2 ( k ) = link1 ( k ) ; k = k +1; end link2 ( k ) = lin2 ; link2 ( k +1) = link1 ( k ) end end endfunction function [ link2 ]= delete1 ( pos , link1 ) link2 = 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 ,0) ; i =1; j =1; while (i < pos ) if (( link1 ( j ) . nexadd == link1 (1) (1) . add ) ) j =1; i = i +1; else i = i +1; j = j +1; end end 34
105 106 107 108 109 110 111 112 113 114 115
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
if ( link1 ( j ) . nexadd ~= link1 (1) (1) . add ) k =1; if ( j ==1) k =2; while ( link1 ( k ) . nexadd ~= link1 (1) (1) . add ) link2 (k -1) = link1 ( k ) ; k = k +1; end link2 (k -1) = link1 ( k ) ; link2 (k -1) . nexadd = link2 (1) . add ; else lin2 = link1 ( j ) ; link1 (j -1) . nexadd = link1 ( j +1) . add ; k =1; while ( link1 ( k ) . nexadd ~= link1 ( j +1) . add ) link2 ( k ) = link1 ( k ) ; k = k +1; end link2 ( k ) = link1 ( k ) ; k = k +2; while ( link1 ( k ) . nexadd ~= link1 (1) (1) . add ) link2 (k -1) = link1 ( k ) ; k = k +1; end link2 (k -1) = link1 ( k ) ; end else link1 (j -1) . nexadd = link1 (1) (1) . add ; l =1; while ( link1 ( l ) . nexadd ~= link1 (1) (1) . add ) link2 ( l ) = link1 ( l ) ; l = l +1; end link2 ( l ) = link1 ( l ) ; end endfunction // C a l l i n g R o u t i n e : link1 = struct ( d a t a ,0 , add ,0 , nexadd ,0) ; 35
link1 = append (4 , link1 ) ; // T h i 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 t link1 = append (6 , link1 ) ; link1 = add (10 ,2 , link1 ) ; link1 = delete1 (4 , link1 ) ; // As t h e l i s t i s c i r c u l a r t h e 4 t h e l e m e n t r e f e r s t o a c t u a l y t h e 1 s t one disp ( link1 , A f t e r t h e a b o v e m a n u p l a t i o n s t h e l i s t i s );
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
(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 ,0) ; if ( link1 (1) (1) . add ==0) link1 (1) (1) . data = ele ; link1 (1) (1) . add =1; link1 (1) (1) . nexadd =1; link2 (1) = link1 (1) (1) ; else if ( link1 (1) (1) . nexadd == link1 (1) (1) . add ) if ( ele >= link1 (1) (1) . data ) t = ele ; p = link1 (1) (1) . data ; else t = link1 (1) (1) . data ; p = ele ; end link1 (1) (1) . data = t ; 36
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
lin2 = link1 (1) (1) ; lin2 . data = p ; lin2 . add =2; lin2 . nexadd = link1 (1) (1) . add ; link1 (1) (1) . nexadd = lin2 . add ; link2 (1) = link1 (1) (1) ; link2 (2) = lin2 ; else i =1; a =[]; while ( link1 ( i ) . nexadd ~= link1 (1) (1) . add ) a =[ a (: ,:) link1 ( i ) . data ]; i = i +1; end a =[ a (: ,:) link1 ( i ) . data ]; a = gsort ( a ) ; j =1; while (j <= i ) link1 ( j ) . data = a ( j ) ; j = j +1; end k =1; while ( link1 ( k ) . data >= ele ) if ( link1 ( k ) . nexadd == link1 (1) (1) . add ) break ; else link2 ( k ) = link1 ( k ) ; k = k +1; end end if ( link1 ( k ) . nexadd ~= link1 (1) (1) . add ) lin2 = link1 ( k ) ; lin2 . data = ele ; lin2 . add = link1 ( k ) . add ; j=k; y = link1 (1) (1) . add ; while ( link1 ( k ) . nexadd ~= y ) link1 ( k ) . add = link1 ( k ) . add +1; 37
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
link1 ( k ) . nexadd = link1 ( k ) . nexadd +1; k = k +1; end link1 ( k ) . add = link1 ( k ) . add +1; lin2 . nexadd = link1 ( j ) . add ; link2 ( j ) = lin2 ; j = j +1; while (j <= k +1) link2 ( j ) = link1 (j -1) ; j = j +1; end else lin2 = link1 ( k ) ; lin2 . data = ele ; lin2 . nexadd = link1 (1) (1) . add ; lin2 . add = link1 ( k ) . add +1; link1 ( k ) . nexadd = lin2 . add ; j =1; while (j <= k ) link2 ( j ) = link1 ( j ) ; j = j +1; end link2 ( j ) = lin2 ; end end end endfunction // C a l l i n g R o u t i n e : link1 = struct ( d a t a ,0 , add ,0 , nexadd ,0) ; link1 = insert_pri (3 , link1 ) ; link1 = insert_pri (4 , link1 ) ; link1 = insert_pri (22 , link1 ) ; link1 = insert_pri (21 , link1 ) ; link1 = insert_pri (11 , link1 ) ; disp ( link1 , L i s t A f t e r I n s e r t i o n s ) ;
38
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
// D e l e t i o n from t h e l i s t : function [ link2 ]= append ( ele , link1 ) link2 = 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 ,0) ; if ( link1 (1) (1) . add ==0) link1 (1) (1) . data = ele ; link1 (1) (1) . add =1; link1 (1) (1) . nexadd =0; link1 (1) (1) . prevadd =0; link2 (1) = link1 (1) (1) ; else if ( link1 (1) (1) . nexadd ==0) lin2 = link1 (1) (1) ; lin2 . data = ele ; lin2 . add = link1 (1) (1) . add +1; link1 (1) (1) . nexadd = lin2 . add ; lin2 . nexadd =0; lin2 . prevadd = link1 (1) (1) . add ; link2 (1) = link1 (1) (1) ; link2 (2) = lin2 ; else lin2 = link1 (1) (1) ; i =1; while ( link1 ( i ) (1) . nexadd ~=0) i = i +1; end j=i; lin2 . data = ele ; lin2 . add = link1 ( i ) . add +1; lin2 . nexadd =0; link1 ( i ) . nexadd = lin2 . add ; 39
31 32 33 34 35 36 37 38 39 40 41 42 43 44
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
lin2 . prevadd = link1 ( i ) . add ; link2 (1) = link1 (1) (1) ; i =2; while ( link1 ( i ) . nexadd ~= lin2 . add ) link2 ( i ) =( link1 ( i ) ) ; i = i +1; end link2 ( i ) = link1 ( i ) ; link2 ( i +1) = lin2 ; end end endfunction function [ link2 ]= add ( ele , pos , link1 ) ; link2 = 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 ,0) ; i =1; while (i <= pos ) if ( link1 ( i ) . nexadd ==0) break ; else i = i +1; end end if ( link1 ( i ) . nexadd ~=0) i =i -1; lin2 . data = ele ; lin2 . add = i ; j=i; while ( link1 ( j ) . nexadd ~=0) link1 ( j ) . prevadd = link1 ( j ) . prevadd +1; link1 ( j ) . add = link1 ( j ) . add +1; link1 ( j ) . nexadd = link1 ( j ) . nexadd +1; j = j +1; end link1 ( j ) . prevadd = link1 ( j ) . prevadd +1; link1 ( j ) . add = link1 ( j ) . add +1; lin2 . nexadd = link1 ( i ) . add ; 40
link1 ( i ) . prevadd = lin2 . add ; lin2 . prevadd = link1 (i -1) . add ; link1 (i -1) . nexadd = lin2 . add ; k =1; while (k < i ) link2 ( k ) = link1 ( k ) ; k = k +1; end link2 ( k ) = lin2 ; k = k +1; link2 ( k ) = link1 (k -1) ; k = k +1 l =k -1; while ( k ~= j ) link2 ( k ) = link1 ( l ) ; k = k +1; l = l +1; end link2 ( j ) = link1 (j -1) ;; link2 ( j +1) = link1 ( j ) ; else if ( i == pos ) k =1; lin2 . data = ele ; lin2 . add = link1 (i -1) . add +1; link1 ( i ) . add = link1 ( i ) . add +1; lin2 . nexadd = link1 ( i ) . add ; link1 ( i ) . prevadd = lin2 . add ; lin2 . prevadd = link1 (i -1) . add ; k =1; while (k < pos ) link2 ( k ) = link1 ( k ) ; k = k +1; end link2 ( k ) = lin2 ; link2 ( k +1) = link1 ( k ) end end 41
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
endfunction function [ link2 ]= delete1 ( pos , link1 ) link2 = 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 ,0) ; i =1; while (i <= pos ) if (( link1 ( i ) . nexadd ==0) ) break ; else i = i +1; end end if ( link1 ( i ) . nexadd ~=0) i =i -1; j =1; if ( i ==1) j =1; while ( link1 ( j ) . nexadd ~=0) link2 ( j ) = link1 ( j ) ; j = j +1; end link2 ( j ) = link1 ( j ) ; else link1 (i -1) . nexadd = link1 ( i +1) . add ; link1 ( i +1) . prevadd = link1 (i -1) . add ; while ( link1 ( j ) . nexadd ~= link1 ( i +1) . add ) link2 ( j ) = link1 ( j ) ; j = j +1; end if ( j ~= i -1) link2 ( j ) = link1 ( j ) ; link2 ( j +1) = link1 ( j +1) ; k = i +1; l =2; else link2 ( j ) = link1 ( j ) ; 42
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
k = i +1; l =1; end while ( link1 ( k ) . nexadd ~=0) link2 ( j + l ) = link1 ( k ) ; k = k +1; l = l +1; end link2 ( j + l ) = link1 ( k ) ; end else if ( i == pos ) j =1; link1 (i -1) . nexadd =0; while (j <= i -1) link2 ( j ) = link1 ( j ) ; j = j +1; end end end endfunction // C a l l i n g R o u t i n e : link1 = struct ( d a t a ,0 , add ,0 , nexadd ,0) ; link1 = append (4 , link1 ) ; link1 = append (6 , link1 ) ; link1 = add (10 ,2 , link1 ) ; link1 = delete1 (3 , link1 ) ; disp ( link1 , A f t e r t h e a b o v e m a n i p u l a t i o n t h e l i s t );
is
43
Chapter 5 Trees
funcprot (0) ; function [ tree ]= maketree ( x ) tree = zeros (30 ,1) ; for i =1:30 tree ( i ) = -1; end tree (1) = x ; tree (2) = -2; endfunction function [ tree1 ]= setleft ( tree , tre , x ) tree1 =[]; i =1; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >2* j ) tree (2* j ) = x ; 44
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
else tree (2* j ) = x ; tree (2* j +1) = -2; for l = i :2* j -1 tree ( i ) = -1; end end tree1 = tree ; endfunction function [ tree1 ]= setright ( tree , tre , x ) tree1 =[]; i =1; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >2* j +1) tree (2* j +1) = x ; else tree (2* j +1) = x ; tree (2* j +2) = -2; for l = i :2* j tree ( i ) = -1; end end tree1 = tree ; endfunction function [ x ]= isleft ( tree , tre ) i =1; x =0; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end 45
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
if (i >=2* j ) if (( tree (2* j ) ~= -1) |( tree (2* j ) ~= -2) ) x =1; return 1; else return 0; end else x =0; return x ; end endfunction function [ x ]= isright ( tree , tre ) i =1; x =0; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >=2* j +1) if (( tree (2* j +1) ~= -1) |( tree (2* j +1) ~= -2) ) x =1; return 1; else return 0; end else x =0; return x ; end endfunction // C a l l i n g R o u t i n e : tree = maketree (3) ; disp ( tree , T r e e made ) ; tree = setleft ( tree ,3 ,1) ; disp ( tree , A f t e r s e t t i n g 1 t o l e f t o f 3 ) ; 46
98 tree = setright ( tree ,3 ,2) ; 99 disp ( tree , A f t e r s e t t i n g 2 t o r i g h t o f 3 ) ; 100 tree = setright ( tree ,2 ,4) ; 101 tree = setleft ( tree ,2 ,5) ; 102 tree = setright ( tree ,1 ,6) ; 103 tree = setright ( tree ,5 ,8) ; 104 disp ( tree , A f t e r a b o v e o p e r a t i o n s : ) ; 105 x = isright ( tree ,3) ; 106 disp (x , C h 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 1
funcprot (0) ; function [ tree ]= maketree ( x ) tree = zeros (30 ,1) ; for i =1:30 tree ( i ) = -1; end tree (1) = x ; tree (2) = -2; endfunction function [ tree1 ]= setleft ( tree , tre , x ) tree1 =[]; i =1; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >2* j ) tree (2* j ) = x ; 47
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
else tree (2* j ) = x ; tree (2* j +1) = -2; for l = i :2* j -1 tree ( i ) = -1; end end tree1 = tree ; endfunction function [ tree1 ]= setright ( tree , tre , x ) tree1 =[]; i =1; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >2* j +1) tree (2* j +1) = x ; else tree (2* j +1) = x ; tree (2* j +2) = -2; for l = i :2* j tree ( i ) = -1; end end tree1 = tree ; endfunction function [ x ]= isleft ( tree , tre ) i =1; x =0; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end 48
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
if (i >=2* j ) if (( tree (2* j ) ~= -1) |( tree (2* j ) ~= -2) ) x =1; return 1; else return 0; end else x =0; return x ; end endfunction function [ x ]= isright ( tree , tre ) i =1; x =0; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >=2* j +1) if (( tree (2* j +1) ~= -1) |( tree (2* j +1) ~= -2) ) x =1; return 1; else return 0; end else x =0; return x ; end endfunction funcprot (0) ; function []= inorder ( tree , p ) if ( tree ( p ) == -1| tree ( p ) == -2) return ; else 49
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
inorder ( tree ,2* p ) ; printf ( %d\ t , tree ( p ) ) ; inorder ( tree ,2* p +1) ; end endfunction function []= preorder ( tree , p ) if ( tree ( p ) == -1| tree ( p ) == -2) return ; else printf ( %d\ t , tree ( p ) ) ; preorder ( tree ,2* p ) ; preorder ( tree ,2* p +1) ; end endfunction function []= postorder ( tree , p ) if ( tree ( p ) == -1| tree ( p ) == -2) return ; else postorder ( tree ,2* p ) ; postorder ( tree ,2* p +1) ; printf ( %d\ t , tree ( p ) ) ; end endfunction // C a l l i n g R o u t i n e : tree = maketree (3) ; tree = setleft ( tree ,3 ,1) ; tree = setright ( tree ,3 ,2) ; tree = setleft ( tree ,2 ,4) ; tree = setright ( tree ,2 ,5) ; disp ( I n o r d e r t r a v e r s a l ) ; inorder ( tree ,1) ; disp ( P r e o r d e r t r a v e r s a l ) ; preorder ( tree ,1) ; disp ( P o s t o r d e r t r a v e r s a l ) ; postorder ( tree ,1) ;
50
funcprot (0) ; function [ tree ]= maketree ( x ) tree = zeros (1 ,30) ; for i =1:30 tree ( i ) = -1; end tree (1) = x ; tree (2) = -2; endfunction function [ tree1 ]= setleft ( tree , tre , x ) tree1 =[]; i =1; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >2* j ) tree (2* j ) = x ; else tree (2* j ) = x ; tree (2* j +1) = -2; for l = i :2* j -1 tree ( i ) = -1; end end tree1 = tree ; endfunction function [ tree1 ]= setright ( tree , tre , x ) tree1 =[]; i =1; 51
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >2* j +1) tree (2* j +1) = x ; else tree (2* j +1) = x ; tree (2* j +2) = -2; for l = i :2* j tree ( i ) = -1; end end tree1 = tree ; endfunction function [ x ]= isleft ( tree , tre ) i =1; x =0; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >=2* j ) if (( tree (2* j ) ~= -1) |( tree (2* j ) ~= -2) ) x =1; return 1; else return 0; end else x =0; return x ; end endfunction 52
function [ x ]= isright ( tree , tre ) i =1; x =0; while ( tree ( i ) ~= -2) if ( tree ( i ) == tre ) j=i; end i = i +1; end if (i >=2* j +1) if (( tree (2* j +1) ~= -1) |( tree (2* j +1) ~= -2) ) x =1; return 1; else return 0; end else x =0; return x ; end endfunction funcprot (0) ; function []= inorder ( tree , p ) if ( tree ( p ) == -1| tree ( p ) == -2) return ; else inorder ( tree ,2* p ) ; disp ( tree ( p ) , ) ; inorder ( tree ,2* p +1) ; end endfunction function []= preorder ( tree , p ) if ( tree ( p ) == -1| tree ( p ) == -2) return ; else disp ( tree ( p ) , ) ; preorder ( tree ,2* p ) ; preorder ( tree ,2* p +1) ; 53
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
end endfunction function []= postorder ( tree , p ) if ( tree ( p ) == -1| tree ( p ) == -2) return ; else postorder ( tree ,2* p ) ; postorder ( tree ,2* p +1) ; disp ( tree ( p ) , ) ; end endfunction function [ tree1 ]= binary ( tree , x ) p =1; while ( tree ( p ) ~= -1& tree ( p ) ~= -2) q=p; if ( tree ( p ) >x ) p =2* p ; else p =2* p +1; end end i =1; while ( tree ( i ) ~= -2) i = i +1; end if ( tree ( q ) >x ) if ( i ==2* q ) tree (2* q ) = x ; tree (2* q +1) = -2 else if (i <2* q ) tree ( i ) = -1; tree (2* q +1) = -2; tree (2* q ) = x ; end end else 54
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
if ( i ==2* q +1) tree (2* q +1) = x ; tree (2* q +2) = -2; else if (i <2* q +1) tree ( i ) = -1; tree (2* q +1) = x ; tree (2* q +2) = -2; end end end tree1 = tree ; endfunction // C a l l i n g R o u t i n e : tree = maketree (3) ; tree = binary ( tree ,1) ; tree = binary ( tree ,2) ; tree = binary ( tree ,4) ; tree = binary ( tree ,5) ; disp ( tree , B i n a r y t r e e t h u s o b t a i n e by i n s e r t i n g 1 , 2 , 4 and5 i n t r e e r o o t e d 3 i s : ) ;
function [ tree1 ]= binary ( tree , x ) p =1; while ( tree ( p ) ~= -1& tree ( p ) ~= -2) q=p; if ( tree ( p ) >x ) p =2* p ; else p =2* p +1; end end 55
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
if ( tree ( q ) >x ) if ( tree (2* q ) == -2) tree (2* q ) = x ; tree (2* q +1) = -2; else tree (2* q ) = x ; end else if ( tree (2* q +1) == -2) tree (2* q +1) = x ; tree (2* q +2) = -2; else tree (2* q +1) = x ; end end tree1 = tree ; endfunction funcprot (0) ; function [ tree ]= maketree ( x ) tree = zeros (40 ,1) ; for i =1:40 tree ( i ) = -1; end tree (1) = x ; tree (2) = -2; endfunction function []= duplicate1 (a , n ) tree = maketree ( a (1) ) ; q =1; p =1; i =2; x=a(i) while (i < n ) while ( tree ( p ) ~= x & tree ( q ) ~= -1& tree ( q ) ~= -2) p=q; if ( tree ( p ) <x ) q =2* p ; else 56
49 q =2* p +1; 50 end 51 end 52 if ( tree ( p ) == x ) 53 disp (x , D u p l i c a t e ) ; 54 else 55 tree = binary ( tree , x ) ; 56 end 57 i = i +1; 58 x=a(i); 59 end 60 while ( tree ( p ) ~= x & tree ( q ) ~= -1& tree ( q ) ~= -2) 61 p=q; 62 if ( tree ( p ) <x ) 63 q =2* p ; 64 else 65 q =2* p +1; 66 end 67 end 68 if ( tree ( p ) == x ) 69 disp (x , D u p l i c a t e ) ; 70 else 71 tree = binary ( tree , x ) ; 72 end 73 endfunction 74 // C a l l i n g A d r e s s : 75 a =[22 11 33 22 211 334] 76 duplicate1 (a ,6) 77 a =[21 11 33 22 22 334] 78 duplicate1 (a ,6)
57
Chapter 6 Graphs
58
22 dfs ( adj , colour ,i , n ) ; 23 end 24 end 25 colour ( r ) =2; 26 endfunction 27 // C a l l i n g R o u t 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 Dfs ( adj , n )
59
24 i =1; 25 a = q1 . a (1) ; 26 for i =2:( q1 . rear - q1 . front ) 27 a =[ a (: ,:) q1 . a ( i ) ]; 28 end 29 q1 . a = a ; 30 end 31 q2 = q1 ; 32 endfunction 33 34 function []= Bfs ( adj , n ) ; 35 i =1 , j =1; 36 colour =[]; 37 for i =1: n 38 for j =1: n 39 colour =[ colour (: ,:) 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 endfunction 45 function []= bfs ( adj , colour ,s , n ) 46 colour ( s ) =1; 47 q = struct ( r e a r ,0 , f r o n t ,0 , a ,0) ; 48 q = push (s , q ) ; 49 while (( q . rear ) -( q . front ) >0) 50 [u , q ]= pop ( q ) ; 51 disp (u , ) ; 52 for i =1: n 53 if ( adj (( u -1) * n + i ) &( colour ( i ) ==0) ) 54 colour ( i ) =1; 55 q = push (i , q ) ; 56 end 57 end 58 colour ( u ) =2; 59 end 60 endfunction 61 // C a l l i n g R o u t i n e :
60
*n+j);
19 20 21 22 23 24 25 26 27
end end end end 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 i v e n g r a p h i s : \ n); for i =1: n printf ( For v e r t e x %d \ n ,i ) ; for j =1: n printf ( %d %d i s %d\ n ,i ,j , path (( i -1) * n + j ) ) ; 61
62
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
adjprod (( i -1) * n + j ) = path (( i -1) * n + j ) ; end end for i =1: n newprod = prod1 ( adjprod , adj , n ) ; for j =1: n for k =1: n path (( j -1) * n + k ) = path (( j -1) * n + k ) | newprod (( j -1) * n + k ) ; end end for j =1: n for k =1: n adjprod (( j -1) * n + k ) = newprod (( j -1) * n + k ) ; end end end endfunction function [ c ]= prod1 (a ,b , n ) for i =1: n for j =1: n val =0 for k =1: n val = val |( a (( i -1) * n + k ) & b (( k -1) * n + j ) ) ; end c (( i -1) * n + j ) = val ; end end endfunction // C a l l i n g R o u t i n e : n =3; adj =[0 1 0 0 0 1 0 0 0] path = Tranclose ( adj , n )
To A n o t h e r I n A Given Graph funcprot (0) function []= sim_path (n , adj ,i , j ) ; l =0; m =1; for m =1: n l = l + path (m ,n , adj ,i , j ) ; end printf ( There a r e %d s i m p l e p a t h s from %d t o %d i n t h e g i v e n g r a p h \ n ,l ,i , j ) ; endfunction function [ b ]= path (k ,n , adj ,i , j ) b =0; if ( k ==1) b = adj (( i -1) * n + j ) ; else for c =1: n if ( adj (( i -1) * n + c ) ==1) b = b + path (k -1 ,n , adj ,c , j ) ; end end end return b ; endfunction n =3; adj =[0 1 1 0 0 1 0 0 0]; b = sim_path (n , adj ,1 ,3)
clear all ; 64
2 clc ; 3 disp ( Example 6 . 6 ) ; 4 // D i j k s t r a s A l g o r i t h m 5 funcprot (0) 6 function [ l ]= short ( adj ,w , i1 , j1 , 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 ) =9999; 11 end 12 end 13 end 14 15 distance =[]; 16 perm =[]; 17 for i =1: n 18 distance =[ distance (: ,:) 99999]; 19 perm =[ perm (: ,:) 0]; 20 end 21 perm ( i1 ) =1; 22 distance ( i1 ) =0; 23 current = i1 ; 24 while ( current ~= j1 ) 25 smalldist =9999; 26 dc = distance ( current ) ; 27 for i =1: n 28 if ( perm ( i ) ==0) 29 newdist = dc + w (( current -1) * n + i ) ; 30 if ( newdist < distance ( i ) ) 31 distance ( i ) = newdist ; 32 end 33 if ( distance ( i ) < smalldist ) 34 smalldist = distance ( i ) ; 35 k=i; 36 end 37 end 38 end 39 current = k ;
65
40 41 42 43 44 45 46 47 48 49
perm ( current ) =1; end l = distance ( j1 ) ; printf ( The s h o r t e s t p a t h b e t w e e n %d and %d i s %d ,i1 , j1 , l ) ; endfunction // C a l l i n g R o u t i n e : n =3; adj =[0 1 1 0 0 1 0 0 0] // A d j a c e n c y List w =[0 12 22 0 0 9 0 0 0] // w e i g h t l i s t f i l l 0 f o r no edge short ( adj ,w ,1 ,3 , n ) ;
A n o t h e r Of A Given Length function [ b ]= path (k ,n , adj ,i , j ) b =0; if ( k ==1) b = adj (( i -1) * n + j ) ; else for c =1: n if ( adj (( i -1) * n + c ) ==1) b = b + path (k -1 ,n , adj ,c , j ) ; end end end printf ( Number o f p a t h s from 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
5 6 7 8 9 10 11 12 13 14 15 16 17
67
Chapter 7 Sorting
68
clear all ; 69
2 clc ; 3 disp ( Example 7 . 3 ) ; 4 funcprot (0) ; 5 function [ a1 ]= quick ( a ) ; 6 a = gsort ( a ) ; // IN BUILT QUICK SORT FUNCTION 7 n = length ( a ) ; 8 a1 =[]; 9 for i =1: n 10 a1 =[ a1 (: ,:) a ( n +1 - i ) ]; 11 end 12 disp ( a1 , S o r t e d a r r a y i s : ) ; 13 endfunction 14 // C a l l i n g R o u t i n e : 15 a =[26 5 37 1 61 11 59 15 48 19] 16 disp (a , Given Array ) ; 17 a1 = quick ( a )
70
i s : );
71
27 end 28 left ( n1 +1) =999999999; 29 right ( n2 +1) =999999999; 30 i =1; 31 j =1; 32 k=p; 33 for k = p : r 34 if ( left ( i ) <= right ( j ) ) 35 a ( k ) = left ( i ) ; 36 i = i +1; 37 else 38 a ( k ) = right ( j ) ; 39 j = j +1; 40 end 41 end 42 a1 = a ; 43 endfunction 44 // C a l l i n g R o u t i n e : 45 a =[26 5 77 1 61 11 59 15 48 19] 46 disp (a , Given Array ) ; 47 a1 = mergesort (a ,1 ,10) 48 disp ( a1 , S o r t e d a r r a y i s : ) ;
72
11 a ( k + span ) = a ( k ) ; 12 k =k - span ; 13 end 14 a ( k + span ) = y ; 15 end 16 end 17 a1 = a ; 18 disp ( a1 , S o r t e d a r r a y i s : ) ; 19 endfunction 20 // C a l l i n g R o u t i n e : 21 a =[23 21 232 121 2324 1222433 1212] 22 disp (a , Given Array ) ; 23 incr =[5 3 1] // must a l w a y s end w i t h 1 24 a1 = shell (a ,7 , incr ,3)
73
19 endfunction 20 // C a l l i n g R o u t i n e : 21 a =[23 21 232 121 2324 1222433 1212] 22 disp (a , Given Array ) ; 23 incr =[5 3 1] // must a l w a y s end w i t h 1 24 a1 = shell (a ,7 , incr ,3)
sorted
74
27
sortedsearch (a ,7 ,23)
75
Chapter 8 Hashing
clear all ; clc ; disp ( Example 8 . 1 ) ; k =12320324111220; p1 =123; p2 =203; p3 =241;...... // key k p a r t i t i o n e d i n t o p a r t s t h a t a r e 3 decimal long . p4 =112; p5 =20; // / . . . . . u s i n g s h i f t f o l d i n g . . . // . . . . p a r t i t i o n s a r e added t o g e t t h e h a s h a d d r e s s . z = p1 + p2 + p3 + p4 + p5 ; disp ( z ) ; // when f o l d i n g a t t h e b o u n d a r i e s i s used , we r e v e r s e p2 and p4 . p2 =302; p4 =211; x = p1 + p2 + p3 + p4 + p5 ; disp ( x ) ;
76
77
7 8 9 10 11 12 13 14 15 16 17 18 19
(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 ,0) ; if ( link1 (1) (1) . add ==0) link1 (1) (1) . data = ele ; link1 (1) (1) . add =1; link1 (1) (1) . nexadd =1; link2 (1) = link1 (1) (1) ; else if ( link1 (1) (1) . nexadd == link1 (1) (1) . add ) if ( ele >= link1 (1) (1) . data ) t = ele ; p = link1 (1) (1) . data ; else t = link1 (1) (1) . data ; p = ele ; 78
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
end link1 (1) (1) . data = t ; lin2 = link1 (1) (1) ; lin2 . data = p ; lin2 . add =2; lin2 . nexadd = link1 (1) (1) . add ; link1 (1) (1) . nexadd = lin2 . add ; link2 (1) = link1 (1) (1) ; link2 (2) = lin2 ; else i =1; a =[]; while ( link1 ( i ) . nexadd ~= link1 (1) (1) . add ) a =[ a (: ,:) link1 ( i ) . data ]; i = i +1; end a =[ a (: ,:) link1 ( i ) . data ]; a = gsort ( a ) ; j =1; while (j <= i ) link1 ( j ) . data = a ( j ) ; j = j +1; end k =1; while ( link1 ( k ) . data >= ele ) if ( link1 ( k ) . nexadd == link1 (1) (1) . add ) break ; else link2 ( k ) = link1 ( k ) ; k = k +1; end end if ( link1 ( k ) . nexadd ~= link1 (1) (1) . add ) lin2 = link1 ( k ) ; lin2 . data = ele ; lin2 . add = link1 ( k ) . add ; j=k; y = link1 (1) (1) . add ; 79
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
89 90 91 92 93
while ( link1 ( k ) . nexadd ~= y ) link1 ( k ) . add = link1 ( k ) . add +1; link1 ( k ) . nexadd = link1 ( k ) . nexadd +1; k = k +1; end link1 ( k ) . add = link1 ( k ) . add +1; lin2 . nexadd = link1 ( j ) . add ; link2 ( j ) = lin2 ; j = j +1; while (j <= k +1) link2 ( j ) = link1 (j -1) ; j = j +1; end else lin2 = link1 ( k ) ; lin2 . data = ele ; lin2 . nexadd = link1 (1) (1) . add ; lin2 . add = link1 ( k ) . add +1; link1 ( k ) . nexadd = lin2 . add ; j =1; while (j <= k ) link2 ( j ) = link1 ( j ) ; j = j +1; end link2 ( j ) = lin2 ; end end end endfunction function [ ele , link2 ]= extract_min ( link1 ) ; link2 = 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 ,0) ; i =1; ele = -1; if ( link1 (1) (1) . add ==0) disp ( U n d e r f l o w ) ; return ; 80
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
else if ( link1 (1) (1) . nexadd == link1 (1) (1) . add ) link1 (1) (1) . add =0; link1 (1) (1) . nexadd =0; ele = link1 (1) (1) . data ; link1 (1) (1) . data =0; link2 (1) = link1 (1) (1) ; else i =1; while ( link1 ( i ) . nexadd ~= link1 (1) (1) . add ) link2 ( i ) = link1 ( i ) ; i = i +1; end ele = link1 ( i ) . data ; link2 (i -1) . nexadd = link2 (1) . add ; end end endfunction // C a l l i n g R o u t i n e : link1 = struct ( d a t a ,0 , add ,0 , nexadd ,0) ; link1 = insert_pri (3 , link1 ) ; link1 = insert_pri (4 , link1 ) ; link1 = insert_pri (22 , link1 ) ; link1 = insert_pri (21 , link1 ) ; link1 = insert_pri (11 , link1 ) ; disp ( link1 , L i s t A f t e r I n s e r t i o n s ) ; [ ele , link1 ]= extract_min ( link1 ) disp ( ele , Element a f t e r t h e min e x t r a c t i o n ) ;
81