diff options
author | priyanka | 2015-06-24 15:03:17 +0530 |
---|---|---|
committer | priyanka | 2015-06-24 15:03:17 +0530 |
commit | b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (patch) | |
tree | ab291cffc65280e58ac82470ba63fbcca7805165 /1034/CH5 | |
download | Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.gz Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.tar.bz2 Scilab-TBC-Uploads-b1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b.zip |
initial commit / add all books
Diffstat (limited to '1034/CH5')
-rwxr-xr-x | 1034/CH5/EX5.1/5s1.sce | 108 | ||||
-rwxr-xr-x | 1034/CH5/EX5.2/s2.sce | 131 | ||||
-rwxr-xr-x | 1034/CH5/EX5.3/5s3.sce | 167 | ||||
-rwxr-xr-x | 1034/CH5/EX5.4/5s4.sce | 78 |
4 files changed, 484 insertions, 0 deletions
diff --git a/1034/CH5/EX5.1/5s1.sce b/1034/CH5/EX5.1/5s1.sce new file mode 100755 index 000000000..0954c13e1 --- /dev/null +++ b/1034/CH5/EX5.1/5s1.sce @@ -0,0 +1,108 @@ +
+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;
+ 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
+ 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
+//Calling Routine:
+tree=maketree(3);
+disp(tree,"Tree made");
+tree=setleft(tree,3,1);
+disp(tree,"After setting 1 to left of 3");
+tree=setright(tree,3,2);
+disp(tree,"After setting 2 to right of 3");
+tree=setright(tree,2,4);
+tree=setleft(tree,2,5);
+tree=setright(tree,1,6);
+tree=setright(tree,5,8);
+disp(tree,"After above operations:");
+x=isright(tree,3);
+disp(x,"Checking for the right son of 3 yes if 1 else no");
+x=isleft(tree,2);
+disp(x,"Check for left son of 2");
diff --git a/1034/CH5/EX5.2/s2.sce b/1034/CH5/EX5.2/s2.sce new file mode 100755 index 000000000..07bbfc9f9 --- /dev/null +++ b/1034/CH5/EX5.2/s2.sce @@ -0,0 +1,131 @@ +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;
+ 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
+ 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
+ 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
+//Calling Routine:
+tree=maketree(3);
+tree=setleft(tree,3,1);
+tree=setright(tree,3,2);
+tree=setleft(tree,2,4);
+tree=setright(tree,2,5);
+disp("Inorder traversal");
+inorder(tree,1);
+disp("Preorder traversal");
+preorder(tree,1);
+disp("Postorder traversal");
+postorder(tree,1);
\ No newline at end of file diff --git a/1034/CH5/EX5.3/5s3.sce b/1034/CH5/EX5.3/5s3.sce new file mode 100755 index 000000000..a666d4349 --- /dev/null +++ b/1034/CH5/EX5.3/5s3.sce @@ -0,0 +1,167 @@ +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;
+ 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
+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);
+ 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
+ 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
+//Calling Routine:
+tree=maketree(3);
+tree=binary(tree,1);
+tree=binary(tree,2);
+tree=binary(tree,4);
+tree=binary(tree,5);
+disp(tree,"Binary tree thus obtaine by inserting 1,2,4and5 in tree rooted 3 is:");
\ No newline at end of file diff --git a/1034/CH5/EX5.4/5s4.sce b/1034/CH5/EX5.4/5s4.sce new file mode 100755 index 000000000..fa64e1aa4 --- /dev/null +++ b/1034/CH5/EX5.4/5s4.sce @@ -0,0 +1,78 @@ +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
+ 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
+ q=2*p+1;
+ end
+ end
+ if(tree(p)==x)
+ disp(x," Duplicate ");
+ else
+ tree=binary(tree,x);
+ end
+ i=i+1;
+ x=a(i);
+ end
+ while(tree(p)~=x&tree(q)~=-1&tree(q)~=-2)
+ p=q;
+ if(tree(p)<x)
+ q=2*p;
+ else
+ q=2*p+1;
+ end
+ end
+ if(tree(p)==x)
+ disp(x," Duplicate ");
+ else
+ tree=binary(tree,x);
+ end
+endfunction
+//Calling Adress:
+a=[22 11 33 22 211 334]
+duplicate1(a,6)
+a=[21 11 33 22 22 334]
+duplicate1(a,6)
\ No newline at end of file |