Bai tap tham khao CSPE

50
Trang 1 [email protected] package sinh_day_ky_tu; public class Main { int n; char[] s; int d=0; public void inkq() { d++; System.out.print("\n Ket qua thu " +d ); for(int i=1; i<=n; i++) System.out.print(s[i] + " "); } public void thu(int i) { if(i>n) inkq(); else { for(char c= 'a'; c<='z'; c++) { s[i]=c; thu(i+1); } } } void khoitao() { n=5; s= new char[n+1]; } public static void main(String[] args) { Main m= new Main(); m.khoitao(); m.thu(1); } }

description

 

Transcript of Bai tap tham khao CSPE

Page 1: Bai tap tham khao CSPE

                                                       Trang  1                                                                            [email protected] 

package sinh_day_ky_tu; public class Main { int n; char[] s; int d=0; public void inkq() { d++; System.out.print("\n Ket qua thu " +d ); for(int i=1; i<=n; i++) System.out.print(s[i] + " "); } public void thu(int i) { if(i>n) inkq(); else { for(char c= 'a'; c<='z'; c++) { s[i]=c; thu(i+1); } } } void khoitao() { n=5; s= new char[n+1]; } public static void main(String[] args) { Main m= new Main(); m.khoitao(); m.thu(1); } }

Page 2: Bai tap tham khao CSPE

                                                       Trang  2                                                                            [email protected] 

package Sinh_file_gom_n_so_nguyen; import java.io.*; import java.util.Scanner; public class Sinh_file_gom_n_so { int n,x; public void Sinh_File(String fi) { try { FileWriter fw = new FileWriter(fi); BufferedWriter bw = new BufferedWriter(fw); Scanner kb= new Scanner(System.in); System.out.print("Nhap so phan tu can sinh:"); n=kb.nextInt(); //bw.write(n+ " "); bw.newLine(); for(int i=1; i<=n; i++) { int t= (int)(Math.random()* 10000); bw.write(t + " "); if(i%30==0) bw.write(t + "\n"); } bw.close(); } catch (IOException ex) { ex.printStackTrace(); } System.out.println("\nDa tao file thanh cong!\n"); } public static void main(String[] args) { // TODO code application logic here String fn; Scanner kb= new Scanner(System.in); System.out.print("\n Nhap ten file can tao:"); fn=kb.next(); Sinh_file_gom_n_so m = new Sinh_file_gom_n_so(); m.Sinh_File(fn); } }

Page 3: Bai tap tham khao CSPE

                                                       Trang  3                                                                            [email protected] 

package xac_dinh_vi_tri_so_x_trong_file; import java.util.Scanner; import java.io.File; import java.util.Arrays; /** * * @author dieuhb */ public class Main { /** * @param args the command line arguments */ int n,x,k; int []a; public void docfile() { try{ Scanner sc = new Scanner (new File("d:\\solieu.txt")); n= sc.nextInt(); x= sc.nextInt(); a= new int[n+1]; for(int i= 1; i<=n; i++) a[i]= sc.nextInt(); }catch (java.io.FileNotFoundException e) {System.out.print(e);}; for(int i=1; i<=n; i++) System.out.print(a[i] + " "); } // Độ phức tạp : O(nlgn) void xuly() { Arrays.sort(a); int vt=1; while (a[vt]<x && vt<=n) vt++; System.out.print("\n Vi tri gia tri "+ x+ " trong file la " +vt); } void xuly1() // O(nlg) : nhưng cải tiến ở bước xd vị trí (lgn) { Arrays.sort(a); int vt=0; int l=1, r=n; while(l<=r) { if(a[l]>=x) { vt=l; break;}

Page 4: Bai tap tham khao CSPE

                                                       Trang  4                                                                            [email protected] 

else if(x== a[r]) {vt=r; break;} else if(x>a[r]) {vt=r+1; break;} else { int m=(l+r)/2; if(a[m]==x) { vt=m; break;} else if(a[m]>x) r=m-1; else l=m+1; } } System.out.print("\n ..Vi tri gia tri "+ x+ " trong file la " +vt); } // Do phuc tap la O(n) void xuly3() { try{ Scanner sc = new Scanner (new File("d:\\solieu.txt")); n= sc.nextInt(); x= sc.nextInt(); int t, vt=1; for(int i= 1; i<=n; i++) { t= sc.nextInt(); if(t>x) vt++; } System.out.print("\n Vi tri cua " + x + " trong file="+vt); }catch (java.io.FileNotFoundException e) {System.out.print(e);}; } public static void main(String[] args) { // TODO code application logic here Main m = new Main(); m.docfile(); System.out.print("\n ............ \n "); m.xuly1(); //m.xuly3(); System.out.print("\n Xong \n "); } }

Page 5: Bai tap tham khao CSPE

                                                       Trang  5                                                                            [email protected] 

package so_fibonacci; import java.util.Scanner; public class Xac_dinh_so_Fibo_nacci_thu_n { public static long Fib(int n) { if(n==1||n==0) return 1; else return Fib(n-1) + Fib(n-2); } public static long Fib1(int n) { long []F; F= new long[n+1]; F[0]=F[1]=1; for(int i=2; i<=n; i++) F[i]=F[i-1]+ F[i-2]; return F[n]; } public static long Fib2(int n) { long F0,F1,F2; F0=F1=1; for(int i=2; i<=n; i++) { F2=F1+ F0; F0= F1; F1=F2; } return F1; } public static void main(String[] args) { double t1,t2; int n; Scanner kb= new Scanner(System.in); System.out.print("nhap n:"); n=kb.nextInt(); System.out.print("\n Dang xu ly.......\n"); t1=System.currentTimeMillis(); System.out.print("\n So Fibonacci thu n la:" + Fib2(n)+"\n"); t2=System.currentTimeMillis(); System.out.print("\nThoi gian chay= "+ (t2-t1)+ " mili giay\n"); System.out.print("\n Dang xu ly.......\n"); t1=System.currentTimeMillis(); System.out.print("\n So Fibonacci thu n la:" + Fib1(n)+"\n"); t2=System.currentTimeMillis(); System.out.print("\nThoi gian chay= "+ (t2-t1)+ " mili giay\n"); System.out.print("\n Dang xu ly.......\n"); t1=System.currentTimeMillis(); System.out.print("\n So Fibonacci thu n la:" + Fib(n)+"\n"); t2=System.currentTimeMillis(); System.out.print("\nThoi gian chay= "+ (t2-t1)+ " mili giay\n"); } }

Page 6: Bai tap tham khao CSPE

                                                       Trang  6                                                                            [email protected] 

package display_list_in_sorted_order; import java.util.Arrays; public class Display_List_in_Sorted_Order { private int[] a; // sinh ngẫu nhiên mảng dữ liệu gồm n phần tử public void sinh(int n) { a= new int[n]; for(int i=0;i<n; i++ ) a[i]=(int)(Math.random()*1000); } public void xuat() { String t=""; for(int i=0;i<a.length ; i++ ) t=t + " " + a[i]; System.out.print(t); } public void Display_Sort(int n) { System.out.print("\n\n---- Hien thi danh sach giam dan ----\n\n"); int []dau; dau=new int[n]; for(int i=0; i<n; i++) dau[i]=0; for(int i=0; i<n; i++) { // tránh lấy lại các giá trị max đã lấy trước đó int vtm=0; while(dau[vtm]==1&& vtm<n) vtm++; // nếu đã chọn thì tránh // tìm phần tử min kế tiếp for(int j=1; j<n; j++) if(a[j]>a[vtm] && dau[j]==0) vtm=j; System.out.print(" " + a[vtm] ); // đánh dấu phần tử max đã chọn. dau[vtm]=1; } } public void sapxep() { Arrays.sort(a); // gọi sắp xếp của Array } public static void main(String[] args) { // TODO code application logic here Display_List_in_Sorted_Order m = new Display_List_in_Sorted_Order(); int n= 3000; m.sinh(n); double t1=System.currentTimeMillis(); m.Display_Sort(n); double t2=System.currentTimeMillis();

Page 7: Bai tap tham khao CSPE

                                                       Trang  7                                                                            [email protected] 

System.out.print("\n\n ------ Thoi gian thuc hien:" + (t2-t1) + "\n"); // thời gian tính theo mili giây t1=System.currentTimeMillis(); m.sapxep(); m.xuat(); t2=System.currentTimeMillis(); System.out.print("\n\n ------ Thoi gian thuc hien:" + (t2-t1) + "\n"); // thời gian } }

package ghi_du_lieu_xuong_file; import java.io.*; import java.util.Scanner; public class Main { int n,x; public void ghiFile() { try { FileWriter fw = new FileWriter("d:\\solieu1.inp"); BufferedWriter bw = new BufferedWriter(fw); Scanner kb= new Scanner(System.in); System.out.print("Nhap so phan tu can sinh:"); n=kb.nextInt(); System.out.print("Nhap so x can xac dinh vi tri:"); x=kb.nextInt(); bw.write(n + " " + x ); bw.newLine(); for(int i=1; i<=n; i++) { int t= (int)(Math.random()* 10000); bw.write(t + " "); if(i%30==0) bw.write(t + "\n"); } bw.close(); } catch (IOException ex) { ex.printStackTrace(); } System.out.println("\nDa tao file thanh cong!\n"); } public static void main(String[] args) { // TODO code application logic here Main m = new Main(); m.ghiFile(); } }

Page 8: Bai tap tham khao CSPE

                                                       Trang  8                                                                            [email protected] 

package liet_ke_day_nguyen_to; public class Liet_ke_day_nguyen_to { public static boolean nt(int x) { if(x<2) return false; else if(x==2) return true; else if(x%2==0) return false; else { double h= Math.sqrt(x); for(int i=3; i<=h; i=i+2) if(x%i==0) return false; return true; } } public static void main(String[] args) { // TODO code application logic here int n=8000000; int d=0; double t1,t2; System.out.print("\n Cac so nguyen to nho hon " + n+ ":\n"); t1=System.currentTimeMillis(); for(int i=2; i<=n; i++) if(nt(i)) d++;//System.out.print(" "+i ); t2=System.currentTimeMillis(); System.out.print("\n Co " + d + " so nguyen to nho hon " +n ); System.out.print("\n Thoi gian thuc hien " + (t2-t1) + " giay\n"); } } package mylist1; import java.util.Scanner; class MyListI { private int []arr; // mang luu tru danh sach private int spt; // so phan tu cua danh sach public MyListI() // ham tao danh sach { arr = new int[100+1]; spt=0; }

Page 9: Bai tap tham khao CSPE

                                                       Trang  9                                                                            [email protected] 

public String toString() { String s="====>"; for(int i=1; i<=spt; i++) s= s + arr[i] + "--->"; s=s+ " null"; return s; } void append(int x) // them vao cuoi danh sach gia tri x { if (spt== arr.length-1) { System.out.print("\n LIST IS FULL\n"); int []b= new int[(spt*2+1)]; for(int i=1; i<=spt; i++) b[i]=arr[i]; arr=b; } else arr[++spt]=x; } void add(int x, int vt) { // neu danh sach day thi cap phat lai bo nho if (spt== arr.length-1) { System.out.print("\n LIST IS FULL\n"); int []b= new int[(spt*2+1)]; for(int i=1; i<=spt; i++) b[i]=arr[i]; arr=b; } // dich cac phan tu sang phai 1 o, ke tu vi tri vt for(int j=spt; j>=vt;j--) arr[j+1]= arr[j]; // cho x vao vi tri vt arr[vt]=x; spt++; } int get(int i) { if(i<0|| i>=spt) {System.out.print("\n Vi tri khong hop le\n"); return -1;} else return arr[i]; }

Page 10: Bai tap tham khao CSPE

                                                       Trang  10                                                                            [email protected] 

void set(int i, int x) { if(i<0|| i>=spt) System.out.print("\n Vi tri khong hop le\n"); else arr[i]=x; } int remove(int vt) { if (vt<1|| vt>=spt) {System.out.print("\n Khong xoa duoc\n"); return -1;} else { int x=arr[vt]; for(int j=vt; j<spt-1; j++) arr[j]=arr[j+1]; spt--; return x; } } int indexOf(int x) { for(int i=0;i<=spt; i++ ) if(arr[i]==x) return i; return 0; } void removeDup() { for(int i=0; i<spt; i++) for(int j=i+1; j<spt; j++) if(arr[i]==arr[j]) this.remove(j); } int remove() { if (spt>0) {spt--; return arr[spt];} else {System.out.print("\n Danh sach rong\n"); return -1;} } void insert(int i, int x) { if(i<0||i>spt) System.out.print("\nVi tri khong hop le\n"); else { // cap phat vung nho neu thieu if (spt==arr.length) { System.out.print("\n Phai cap phat them\n");

Page 11: Bai tap tham khao CSPE

                                                       Trang  11                                                                            [email protected] 

int []b= new int[arr.length*2]; for(int j=0;j<spt; j++) b[j] =arr[j]; arr=b; } // thuc hien lenh chen for(int j=spt; j>i; j--) arr[j] =arr[j-1]; arr[i]=x; spt++; } } } public class My_Linked_List { public static void main(String[] args) { // tao danh sach MyListI L1= new MyListI(); int x; Scanner kb= new Scanner(System.in); int i=1; do { System.out.print("nhap so thu " + (i++)+ ":"); x= kb.nextInt(); if(x>0) L1.append(x); } while(x>0); //---------------------------- System.out.print("\n Danh sach vua tao :\n"+ L1); L1.add(300,4); System.out.print("\n Danh sach sau khi chen :\n"+ L1); /* L1.remove(); L1.remove(2); System.out.print("\n Danh sach sau khi xo p.tu cuoi va phan tu thu 2 :\n"+ L1); L1.insert(2,5 ); L1.insert(5,8); System.out.print("\n Danh sach sau khi chen :\n"+ L1); L1.removeDup(); System.out.print("\n Danh sach sau khi xoa trung :\n"+ L1); */ } }

Page 12: Bai tap tham khao CSPE

                                                       Trang  12                                                                            [email protected] 

package mylist2; import java.util.Scanner; class Node { int data; Node next; Node(){} Node(int x){ data=x; next=null;} Node(int x, Node t ) { data=x; next =t;} } class MySingleList { Node head; Node tail; void append(int x) { Node t= new Node(x, null); if(head==null) {head=tail=t;} else { tail.next=t; tail=t; } } public void tao() { head=null; tail=null; Scanner kb= new Scanner(System.in); int x; int i=1; System.out.print("Nhap gia tri de them vao danh sach\n"); do { System.out.print("\nSo thu " + (i++)+ ":"); x=kb.nextInt(); if(x<=0) break; append(x); } while (x>0); } void remove(Node p) { if(head==null) System.out.print("\ndanh sach rong, ko xoa duoc!");

Page 13: Bai tap tham khao CSPE

                                                       Trang  13                                                                            [email protected] 

else { if(head==p) head=head.next; else { Node t=head; while(t!=null && t.next!=p) t=t.next; if(t==null) System.out.print("\nKo co not p trong danh sach"); else t.next=t.next.next; } } } void remove(int k) { if(head==null||k<1) System.out.print("\nko xoa duoc!"); else { if(k==1) head=head.next; else { int vt=1; Node t=head; while(t!=null &&vt<k-1) {t=t.next; vt++;} if(t==null||t.next==null) System.out.print("\nDanh sach ko du so phan tu"); else t.next=t.next.next; } } } void insert(int k, int x) { if(k<1) System.out.print("\nko chen duoc!"); else { if(k==1) head=new Node(x,head); else { int vt=1; Node t=head; while(t!=null &&vt<k-1) {t=t.next; vt++;} if(t==null) System.out.print("\nDanh sach ko du so phan tu");

Page 14: Bai tap tham khao CSPE

                                                       Trang  14                                                                            [email protected] 

else { Node p = new Node(x,t.next); t.next=p; } } } } void duyet() { System.out.print("\n Noi dung danh sach:\n=====>"); Node t= head; while(t!=null) { System.out.print(t.data + "--->"); t=t.next; } System.out.print( " null\n"); } } public class Main { public static void main(String[] args) { // TODO code application logic here MySingleList L= new MySingleList(); L.tao(); L.duyet(); L.insert(3,100); L.insert(1,999); L.duyet(); Node p= new Node(9,null); L.remove(p); L.duyet(); p=L.head.next; L.remove(p); L.duyet(); } }

Page 15: Bai tap tham khao CSPE

                                                       Trang  15                                                                            [email protected] 

package my_double_linked_list; import java.util.Scanner; class DNode { int data; DNode next; DNode prev; DNode(){} DNode(int x){ data=x; next=null; prev=null;} DNode(int x, DNode t ) { if(t==null) { data=x; next=null; prev=null;} else { data=x; next=t; prev= null; t.prev=this; } } } class MyDoubleLinkedList { DNode head; DNode tail; void append(int x) { DNode t= new DNode(x, null); if(head==null) {head=tail=t;} else { tail.next=t; t.prev= tail; tail=t; } } public void tao() { head=null; tail=null; Scanner kb= new Scanner(System.in); int x; int i=1; System.out.print("Nhap gia tri de them vao danh sach\n"); do { System.out.print("\nSo thu " + (i++)+ ":"); x=kb.nextInt(); if(x<=0) break;

Page 16: Bai tap tham khao CSPE

                                                       Trang  16                                                                            [email protected] 

append(x); } while (x>0); } void duyet() { System.out.print("\n Noi dung danh sach:\n=====>"); DNode t= head; while(t!=null) { System.out.print(t.data + "<--->"); t=t.next; } System.out.print( " null\n"); } } public class Double_linked_List { public static void main(String[] args) { // TODO code application logic here MyDoubleLinkedList LL = new MyDoubleLinkedList(); LL.tao(); LL.duyet(); } }

Page 17: Bai tap tham khao CSPE

                                                       Trang  17                                                                            [email protected] 

package mystack1; class MyStack1 { private int []a; private int top; static final int MAXSIZE=1000; // so phan tu toi da cua nx // cac thao tac co ban MyStack1() { a= new int [MAXSIZE +1]; top=0; } //kiem tra ngan xep co rong hay ko boolean EmptyS() { return top==0;} // them 1 phan tu x vao dinh ngan xep void PushS(int x) { if(top<MAXSIZE) a[++top]=x ; else System.out.print("\n Ngan xep bi day\n"); } // Lay phan tu o dinh ngan xep ra int PopS() { if (top<1) {System.out.print("\n Ngan xep RONG \n"); return -1;} else return a[top--]; } int PeekS() { if (top<1) {System.out.print("\n Ngan xep RONG \n"); return -1;} else return a[top]; } // Hien thi noi dung ngan xep, chuyen sang dang chuoi public String toString() { String t="--> null\n"; for(int i=1; i<=top; i++) t="--->" + a[i] + t; return t; } }

Page 18: Bai tap tham khao CSPE

                                                       Trang  18                                                                            [email protected] 

public class Mystack1 { public static void main(String[] args) { MyStack1 s= new MyStack1(); /* s.PushS(5);s.PushS(3);s.PushS(8); s.PopS();s.PushS(s.PopS()+9); s.PushS(s.PeekS()+7); System.out.print("\n Noi dung ngan xep:\n" + s); */ int n=13234; System.out.print("\n Bieu dien nhi phan cua "+ n+ " la:"); while(n>0) { s.PushS(n%2); n=n/2; } // lay noi dung ngan xep ra in while(!s.EmptyS()) System.out.print(s.PopS()); System.out.println(); } } package mystack2; class Node { int data; Node next; Node(){} // tao ra 1 not co gia tri x Node(int x) { data=x; next=null;} // tao ra 1 not co gia tri x va dung truoc not t Node(int x, Node t){ data =x; next =t;} } class MyStack2 { private Node top; // cac thao tac co ban MyStack2() {top=null;} boolean EmptyS() {return top==null;} void PushS(int x) { top= new Node(x, top); }

Page 19: Bai tap tham khao CSPE

                                                       Trang  19                                                                            [email protected] 

int PopS() { if (top==null) {System.out.print("\n Ngan xep rong\n"); return -1;} else {int x=top.data; top=top.next; return x;} } int PeekS() { if (top==null) {System.out.print("\n Ngan xep rong\n"); return -1;} else return top.data; } public String toString() { String t=""; Node p=top; while (p!=null) {t= t + "--->" + p.data; p=p.next;} t=t+ "--->null"; return t; } public static void main(String[] args) { // TODO code application logic here MyStack2 s= new MyStack2(); s.PushS(5);s.PushS(3);s.PushS(8); s.PushS(55);s.PushS(33);s.PushS(88); s.PopS();s.PushS(s.PopS()+9); s.PushS(s.PeekS()+7); System.out.print("\n Noi dung ngan xep:\n" + s); } }

package khu_de_qui_thap_ha_noi; // dùng stack class khoi{ int sd; char c1,c2,c3; khoi(){} khoi(int sd1, char cc1, char cc2, char cc3) { sd= sd1; c1=cc1; c2=cc2; c3=cc3; } } class Stack_hn { khoi []a; int top; final int MAXSIZE=1000;

Page 20: Bai tap tham khao CSPE

                                                       Trang  20                                                                            [email protected] 

Stack_hn() { a= new khoi[MAXSIZE +1]; top=0; } boolean EmptyS() { if(top==0) return true; else return false; } void PushS(khoi x) { if(top==MAXSIZE) System.out.print("\nNgan sep bi day\n"); else { top++; a[top]=x; } } khoi PopS() { khoi x= new khoi(); if(top==0) { System.out.print("\nNgan xep rong\n"); return x; } else return a[top--]; } } public class Khu_de_qui_thap_ha_noi { Khu_de_qui_thap_ha_noi(){} // ham tao cua lop public void hn1( int n, char a,char b, char c) { if(n==1) System.out.print("\n Chuyen 1 dia tu " + a + " sang "+ c); else { hn1(n-1, a, c,b); hn1(1, a, b,c); hn1(n-1, b, a,c); } }

Page 21: Bai tap tham khao CSPE

                                                       Trang  21                                                                            [email protected] 

public void hn( int n, char a,char b, char c) { Stack_hn S= new Stack_hn(); khoi x= new khoi(n, a,b,c); S.PushS(x); int d=0; while(!S.EmptyS()) { khoi z= S.PopS(); if(z.sd==1) System.out.print("\n " + (++d)+ ": chuyen 1 dia tu " + z.c1 + " sang "+ z.c3); else { x= new khoi(z.sd-1, z.c2, z.c1,z.c3); S.PushS(x); x= new khoi(1, z.c1, z.c2,z.c3); S.PushS(x); x= new khoi(z.sd-1, z.c1, z.c3,z.c2); S.PushS(x); } } } public static void main(String[] args) { // TODO code application logic here Khu_de_qui_thap_ha_noi bb= new Khu_de_qui_thap_ha_noi(); bb.hn1(3,'a', 'b','c'); // goi de qui System.out.print("\n XONG\n\n"); bb.hn(3,'a', 'b','c'); // goi khu de qui System.out.print("\n XONG\n"); } }

Page 22: Bai tap tham khao CSPE

                                                       Trang  22                                                                            [email protected] 

package mytree; import java.util.Scanner; class TNode { int data; TNode left, right; TNode(int x) {data=x; left=right=null;} TNode(int x, TNode l, TNode r) {data=x; left=l; right=r;} } class Tree { TNode root; // goc cay Tree (int x) { root = new TNode(x);} Tree (int x, TNode l, TNode r ) { root = new TNode(x,l,r);} Tree () { root = null;} Tree (int x, Tree ll, Tree rr ) { if(ll!=null && rr!=null) root = new TNode(x,ll.root,rr.root); else if (ll==null && rr==null) root = new TNode(x); else if(ll!=null) root = new TNode(x,ll.root,null); else root = new TNode(x,null,rr.root); } private void duyet1(TNode r) // tien tu { if(r!=null) { System.out.print(r.data + " "); duyet1(r.left); duyet1(r.right); } } void duyettientu() { duyet1(root); } private int tong(TNode r) { if (r==null ) return 0; else return r.data + tong(r.left) + tong(r.right); } public int tongnot() { return tong(root);

Page 23: Bai tap tham khao CSPE

                                                       Trang  23                                                                            [email protected] 

} private int dn(TNode r) { if (r==null ) return 0; else return 1 + dn(r.left) + dn(r.right); } public int demnot() { return dn(root); } private int dl(TNode r) { if (r==null ) return 0; else if (r.left==null && r.right==null) return 1; else return dl(r.left) + dl(r.right); } private int dn1c(TNode r) { if (r==null || (r.left==null && r.right==null) ) return 0; else if(r.left==null || r.right==null) return 1 + dn1c(r.left) + dn1c(r.right); else return dn1c(r.left) + dn1c(r.right); } public int demnot1con() { return dn1c(root); } public int demla() { return dl(root); } private TNode chen(int x, TNode rr) { if(rr==null) return new TNode(x, null,null); else if(dn(rr.left)> dn(rr.right)) { rr.right= chen(x,rr.right); return rr;} else { rr.left=chen(x,rr.left); return rr;} } void chencb(int x) { root = chen(x, root); } void taocay()

Page 24: Bai tap tham khao CSPE

                                                       Trang  24                                                                            [email protected] 

{ root = null; Scanner kb= new Scanner(System.in); while(true) { System.out.print("\nnhap gia tri x them vao cay :"); int x= kb.nextInt(); if(x==0) break; chencb(x); } } private boolean timx(TNode r, int x) { if(r==null) return false; else if(r.data==x) return true; else if (timx(r.left,x)==false) return timx(r.right,x); return true; } public boolean timx(int x) { return timx(root,x); } private int cao(TNode r) { if(r==null) return 0; else { if(cao(r.left)> cao(r.right)) return 1 + cao(r.left); else return 1 + cao(r.right); } } public int cao() { return cao(root); } } public class My_Tree { public static void main(String[] args) { // TODO code application logic here Tree T1, p,q; /*

Page 25: Bai tap tham khao CSPE

                                                       Trang  25                                                                            [email protected] 

p= new Tree(7, new Tree(8), new Tree(5, new Tree(1), null)); q= new Tree (2, new Tree(6), new Tree(9, new Tree(4), new Tree(8))); T1= new Tree(3, q,p); */ T1= new Tree(); T1.taocay(); System.out.print("\n Ket qua duyet tien tu:"); T1.duyettientu(); System.out.print("\n Tong cac not trong cay la:" + T1.tongnot()); System.out.print("\n So la trong cay la:" + T1.demla()); System.out.print("\n So not trong cay la:" + T1.demnot()); System.out.print("\n So not 1 con trong cay la:" + T1.demnot1con()); System.out.print("\n Chieu cao cay la:" + T1.cao()); Scanner kb= new Scanner(System.in); System.out.print("\nnhap gia tri x can tim :"); int x= kb.nextInt(); if(T1.timx(x)) System.out.print("\n Co gia tri " + x +" trong cay"); else System.out.print("\n Khong co gia tri " + x +" trong cay"); System.out.print("\n XONG \n"); } } package my_bstree; import java.util.Scanner; class TNode{ int data; TNode left, right; TNode(int x) { data=x; left=right=null; } TNode(int x, TNode l, TNode r) { data=x; left=l; right=r; } } class BSTree { TNode root; BSTree() {root=null;} BSTree(int x) {root=new TNode(x);}

Page 26: Bai tap tham khao CSPE

                                                       Trang  26                                                                            [email protected] 

BSTree(int x, BSTree ll, BSTree rr) { if(ll!=null && rr!=null) root = new TNode(x,ll.root,rr.root); else if (ll==null && rr==null) root = new TNode(x); else if(ll!=null) root = new TNode(x,ll.root,null); else root = new TNode(x,null,rr.root); } private TNode chen(int x, TNode rr) { if(rr==null) return new TNode(x, null,null); else if(x<rr.data) { rr.left= chen(x,rr.left); return rr;} else if(x>rr.data) { rr.right=chen(x,rr.right); return rr;} else { System.out.print("\n Da co gia tri " + x + "trong cay\n"); return rr; } } void chen(int x) { root = chen(x, root); } private boolean tim(int x, TNode rr) { if(rr==null) return false; else if(rr.data==x) return true; else if(x<rr.data) return tim(x, rr.left); else return tim(x, rr.right); } boolean tim(int x) { return tim(x,root); } private TNode del(int x, TNode rr) { if(rr== null) return null; else if(x<rr.data) { rr.left= del(x,rr.left); return rr;} else if(x>rr.data) { rr.right= del(x,rr.right); return rr;}

Page 27: Bai tap tham khao CSPE

                                                       Trang  27                                                                            [email protected] 

else { if (rr.left==null && rr.right==null) return null; else if(rr.left==null) return rr.right; else if (rr.right==null) return rr.left; else { // xoa max con trai TNode p=rr.left; while (p.right!=null) p=p.right; rr.data=p.data; rr.left= del(p.data,rr.left); return rr; } } } void xoa(int x) { root = del(x,root); } private void duyet(TNode rr) { if(rr!=null) { duyet(rr.left); System.out.print(rr.data + " "); duyet(rr.right); } } void duyet() { duyet(root); } void taocay(int n) { Scanner kb=new Scanner(System.in); System.out.print("\n Tao cay gom " + n + "not \n"); root=null; for(int i=1; i<=n; i++) { int x; System.out.print("\n nhap gia tri thu " + i+ ":"); x=kb.nextInt(); this.chen(x);

Page 28: Bai tap tham khao CSPE

                                                       Trang  28                                                                            [email protected] 

} } } public class My_BST { public static void main(String[] args) { // TODO code application logic here BSTree T= new BSTree(); //T.taocay(7); T.chen(15);T.chen(25);T.chen(8);T.chen(10);T.chen(45);T.chen(5);T.chen(9);T.chen(3); T.chen(12);T.chen(21);T.chen(38);T.chen(24); System.out.print("\n cay vua tao:"); T.duyet(); if(T.tim(44)) System.out.print("\n co gia tri 44 trong cay\n"); else System.out.print("\n khong co gia tri 44 trong cay\n"); T.xoa(25); System.out.print("\n cay sau khi xoa gia tri 25:"); T.duyet(); } }

package my_bst_dup; import java.util.Scanner; /** * * @author dieuhb */ class TTNode { int data, count; TTNode left, right; TTNode(int x) { data=x; left=right=null; count=1; } TTNode(int x, TTNode l, TTNode r) { data=x; l= left; r= right; count=1; } }

Page 29: Bai tap tham khao CSPE

                                                       Trang  29                                                                            [email protected] 

class BST_T { TTNode root; BST_T(){root = null;} private TTNode chen(int x, TTNode rr) { if(rr==null) return new TTNode(x, null,null); else { if(x<rr.data) rr.left= chen(x,rr.left); else if(x>rr.data) rr.right=chen(x,rr.right); else rr.count++; return rr; } } public void ChenX(int x) { root = chen(x, root); } private void duyet(TTNode t) {// Duyet trung tu if(t!=null){ duyet(t.left); for(int i=1; i<= t.count; i++ )System.out.print(" "+t.data); duyet(t.right); } } void duyet(){ duyet(root); } private TTNode del(int x, TTNode rr) { if(rr== null) return null; else if(x<rr.data) { rr.left= del(x,rr.left); return rr;} else if(x>rr.data) { rr.right= del(x,rr.right); return rr;} else if(rr.count>1) {rr.count--; return rr;} else { if (rr.left==null && rr.right==null) return null; // nốt là lá else if(rr.left==null) return rr.right; // chỉ có con phải else if (rr.right==null) return rr.left; // chỉ có con trái

Page 30: Bai tap tham khao CSPE

                                                       Trang  30                                                                            [email protected] 

else // có hai con { // xoa max con trai TTNode p=rr.left; while (p.right!=null) p=p.right; // tìm max con trái rr.data=p.data; rr.count=p.count; // thay và nốt gốc p.count=1; rr.left= del(p.data,rr.left); // xóa max con trái return rr; } } } void xoa(int x) { root = del(x,root); } void taocay() // dung ki nhap so 0 { root =null; Scanner kb=new Scanner(System.in); int x; while(true) { System.out.print("Nhap gia tri chen vao cay: "); x= kb.nextInt(); if(x==0) break; ChenX(x); } } } public class BST_DUP { public static void main(String[] args) { Scanner kb=new Scanner(System.in); int x; BST_T t = new BST_T(); t.taocay(); System.out.print("\n Ket qua duyet cay:\n "); t.duyet(); System.out.print("\n Nhap gia tri can xoa:"); x= kb.nextInt(); t.xoa(x); System.out.print("\n Ket qua duyet cay sau khi xoa gia tri " + x+ ":\n "); t.duyet(); System.out.print("\n XONG\n "); } }

Page 31: Bai tap tham khao CSPE

                                                       Trang  31                                                                            [email protected] 

package my_sort; import java.util.Scanner; /** * * @author Huynh Ba Dieu .... [email protected] */ class My_Sort { int []a; int []b; void sinh() { Scanner kb= new Scanner(System.in); int n=0; System.out.print("\n Nhap so phan tu can sinh:"); n= kb.nextInt(); a= new int [n+1]; b= new int [n+1]; for(int i=1; i<=n; i++) { a[i]=(int)(Math.random()*10000)%1000; b[i]=a[i]; } } void in() { System.out.print("\n Noi dung mang:\n"); for(int i=1; i<a.length; i++) System.out.print(a[i]+ " "); } void chep() { for(int i=1; i<a.length; i++) a[i]=b[i]; } void Select_Sort() { for(int i=1; i<a.length-1; i++ ) { int imin=i; for(int j=i+1; j<a.length; j++ ) if(a[imin]>a[j]) imin=j; if(i!=imin) {int t=a[i]; a[i]=a[imin]; a[imin]=t;} } }

Page 32: Bai tap tham khao CSPE

                                                       Trang  32                                                                            [email protected] 

void Bubble_Sort() { for(int i=1; i<a.length-1; i++ ) for(int j=a.length-1;j>i; j-- ) if(a[j]<a[j-1]) {int t=a[j]; a[j]=a[j-1]; a[j-1]=t;} } void qs(int l, int r) { int x= a[(l+r)/2]; int i=l, j=r; while(i<j) { while(a[i]<x) i++; while(a[j]>x) j--; if(i<=j) { int t=a[i]; a[i]=a[j]; a[j]=t; i++; j--; } } if(i<r) qs(i,r); if(l<j) qs(l,j); } void Quick_Sort() { qs(1, a.length-1); } void Insertion_Sort() { int i,j,x; int k=a.length-1; for( i=2; i<=k; i++) { x=a[i]; j=i-1; while(a[j]>x && j>0) {a[j+1]= a[j]; j--;} a[j+1]=x; } } void Heapify(int start, int end) { int j=start; while(2*j<=end) { int maxchild=2*j; if(2*j<end && a[maxchild]<a[maxchild+1] ) maxchild=maxchild +1;

Page 33: Bai tap tham khao CSPE

                                                       Trang  33                                                                            [email protected] 

if(a[j]<a[maxchild]) { int t=a[j]; a[j]= a[maxchild]; a[maxchild]=t; j= maxchild; } else break; } } void Heap_Sort() { int i; int k= a.length-1; for(i=k/2; i>=1; i--) Heapify(i, k); for (i = k; i >1; i--) { int t = a[1]; a[1] = a[i]; a[i] = t; Heapify(1,i-1); } } void merge(int low, int mid, int high) { int []b = new int[high-low+2]; int h,i,j,k; h=low; i=0; j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) b[++i]=a[h++]; else b[++i]=a[j++]; } if(h>mid) for(k=j;k<=high;k++) b[++i]=a[k]; else for(k=h;k<=mid;k++) b[++i]=a[k]; for(k=1;k<=high-low+1;k++) a[k+low-1]=b[k]; } void Merge_Sort(int low, int high) { if(low<high) { int mid=(low+high)/2; Merge_Sort(low,mid);

Page 34: Bai tap tham khao CSPE

                                                       Trang  34                                                                            [email protected] 

Merge_Sort( mid+1,high); merge(low,mid,high); } } void Merge_Sort() { Merge_Sort(1, a.length-1); } } public class Sort_array { /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here long t1, t2; My_Sort m1= new My_Sort(); m1.sinh(); //m1.in(); t1=System.currentTimeMillis(); m1.Quick_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN QUICK SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep(); t1=System.currentTimeMillis(); m1.Heap_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN HEAP SORT:"); m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); /* m1.chep(); t1=System.currentTimeMillis(); m1.Merge_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN MERGE SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep();

Page 35: Bai tap tham khao CSPE

                                                       Trang  35                                                                            [email protected] 

t1=System.currentTimeMillis(); m1.Select_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN SELECT SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep(); t1=System.currentTimeMillis(); m1.Insertion_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN INSERT SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep(); t1=System.currentTimeMillis(); m1.Bubble_Sort(); t2=System.currentTimeMillis(); System.out.print("\n mang m1 sau khi sap xep THEO THUAT TOAN BUBBLE SORT:"); //m1.in(); System.out.print("\n thoi gian thuc hien thuat toan: " + (t2-t1)); System.out.print("\n ----XONG---\n"); */ } }

Page 36: Bai tap tham khao CSPE

                                                       Trang  36                                                                            [email protected] 

package myqueue; import java.util.Scanner; /** * * @author dieuhb */ class Node { int data; Node next; Node(){} Node(int x){ data=x; next=null;} Node(int x, Node next){ data=x; this.next=next;} } class MyQueue { Node head, tail; //1.Tao hang doi rong MyQueue() {head=tail=null;} //2 Kiem tra hang doi co rong hay ko boolean EmptyQ() { return head==null && tail==null; } void addQ(int x) { Node t= new Node(x); if(head==null && tail==null) head=tail=t; else { tail.next=t; tail=t;} } int RemoveQ() { if(head==null && tail==null) { System.out.print("\n Hang doi rong\n"); return -1; } else { int x= head.data; if (head==tail) head=tail=null; // neu hang doi co 1 phan tu thi ... else head=head.next; return x; } }

Page 37: Bai tap tham khao CSPE

                                                       Trang  37                                                                            [email protected] 

public String toString() { String t="\n"; Node p=head; while (p!=null) { t= t + "-->" + p.data; p=p.next;} t=t + "--->null\n"; return t; } public static void main(String[] args) { MyQueue Q= new MyQueue(); Q.addQ(7);Q.addQ(2);Q.addQ(3); Q.addQ(5);Q.addQ(8);Q.addQ(9); System.out.print("\n HANG DOI:" +Q); // Tinh tong cac gia tri trong hang doi int k=0,x; MyQueue Q1= new MyQueue(); while (!Q.EmptyQ()){ x= Q.RemoveQ(); k=k+ x; Q1.addQ(x);} while (!Q1.EmptyQ()) Q.addQ(Q1.RemoveQ()); System.out.print("\n Tong cac gia tri trong hang doi= " + k+ "\n"); System.out.print("\n HANG DOI:" +Q); // Lay phan tu thu k Scanner kb= new Scanner(System.in); MyQueue Q2= new MyQueue(); System.out.print("Nhap vi tri phan tu muon lay:"); int vt= kb.nextInt(); int dem=0; while (!Q.EmptyQ()) { x=Q.RemoveQ(); dem++; if(dem!=vt) Q2.addQ(x); } if(dem<vt) System.out.print("\n Hang doi khong du " + vt+ " phan tu\n"); while (!Q2.EmptyQ()) { x=Q2.RemoveQ(); Q.addQ(x); } System.out.print("\n HANG DOI sau khi lay phan tu thu " + vt +":" +Q); System.out.print("Nhap vi tri phan tu muon them:"); vt= kb.nextInt(); System.out.print("Nhap gia tri can them:"); int xx= kb.nextInt(); Q2= new MyQueue();

Page 38: Bai tap tham khao CSPE

                                                       Trang  38                                                                            [email protected] 

dem=0; while (true) { dem++; if(dem==vt) Q2.addQ(xx); else if (Q.EmptyQ()) break; else {x=Q.RemoveQ(); Q2.addQ(x);} } if(dem<vt-1) System.out.print("\n Hang doi khong du " + vt+ " phan tu\n"); while (!Q2.EmptyQ()) { x=Q2.RemoveQ(); Q.addQ(x); } System.out.print("\n HANG DOI sau khi them phan tu " + xx +" tai vi tri " +vt + ":"+Q); //int n=4567; //int h= String.valueOf(n).length(); //System.out.print("\n Do dai = "+ h); } }

package radix_sort; import java.util.Scanner; /** * * @author dieuhb */ class Node { int data; Node next; Node(){} Node(int x){ data=x; next=null;} Node(int x, Node next){ data=x; this.next=next;} } class MyQueue { Node head, tail; //1.Tao hang doi rong MyQueue() {head=tail=null;} //2 Kiem tra hang doi co rong hay ko

Page 39: Bai tap tham khao CSPE

                                                       Trang  39                                                                            [email protected] 

boolean EmptyQ() { return head==null && tail==null; } void addQ(int x) { Node t= new Node(x); if(head==null && tail==null) head=tail=t; else { tail.next=t; tail=t;} } int RemoveQ() { if(head==null && tail==null) { System.out.print("\n Hang doi rong\n"); return -1; } else { int x= head.data; if (head==tail) head=tail=null; // neu hang doi co 1 phan tu thi ... else head=head.next; return x; } } } public class Radix_Sort { int a[]; void sinhday() { int n=0; Scanner kb= new Scanner(System.in); System.out.print(" So phan tu can sinh cho day"); n= kb.nextInt(); a= new int[n+1]; for(int i=1; i<=n; i++) a[i]= (int)(Math.random()*1000); } void inday() { System.out.print("\n Noi dung day:\n"); for(int i=1; i<a.length; i++) System.out.print(" " + a[i]); }

Page 40: Bai tap tham khao CSPE

                                                       Trang  40                                                                            [email protected] 

public void RadixSort() { MyQueue QQ= new MyQueue(); MyQueue []Q= new MyQueue[10]; int i,j,k,x, max=a[1],vt, gt=1; for(j=0; j<=9; j++) Q[j]= new MyQueue(); for(i=1; i<a.length; i++ ) { QQ.addQ(a[i]); if(a[i]>max) max=a[i]; } k= String.valueOf(max).length(); for(i=1; i<=k; i++) { while(!QQ.EmptyQ()) { x= QQ.RemoveQ(); vt= (x/gt)%10; Q[vt].addQ(x); } for(j=0; j<=9; j++) while(!Q[j].EmptyQ()) QQ.addQ(Q[j].RemoveQ()); gt=gt*10; } i=1; while(!QQ.EmptyQ()) a[i++] = QQ.RemoveQ(); } public static void main(String[] args) { Radix_Sort bb = new Radix_Sort(); bb.sinhday(); bb.inday(); bb.RadixSort(); bb.inday(); } }

Page 41: Bai tap tham khao CSPE

                                                       Trang  41                                                                            [email protected] 

package sorting_with_bst; import java.util.Scanner; import java.io.*; public class Sort_with_BST { public static void main(String[] args) { int x; String fi, fo; Scanner kb=new Scanner(System.in); System.out.print("\n Nhap ten file du lieu can sap xep:"); fi= kb.next(); System.out.print("\n Nhap ten file ket sau sau sap xep:"); fo= kb.next(); BST_T T = new BST_T(); double t1,t2; t1= System.currentTimeMillis(); T.docfile(fi); T.duyet_ghi_file(fo); t2= System.currentTimeMillis(); System.out.print("\n\n Thoi gian sap xep du lieu la:" + (t2-t1) + " milisecond \n"); System.out.print("\n XONG\n "); } } class TTNode { int data, count; TTNode left, right; TTNode(int x){ data=x; left=right=null; count=1; } TTNode(int x, TTNode l, TTNode r){ data=x; l= left; r= right; count=1; } } class BST_T { TTNode root; FileInputStream fs1; FileOutputStream fos; PrintWriter out; BST_T(){root = null;}

Page 42: Bai tap tham khao CSPE

                                                       Trang  42                                                                            [email protected] 

private TTNode chen(int x, TTNode rr) { if(rr==null) return new TTNode(x, null,null); else { if(x<rr.data) rr.left= chen(x,rr.left); else if(x>rr.data) rr.right=chen(x,rr.right); else rr.count++; return rr; } } public void ChenX(int x) { root = chen(x, root); } public void docfile(String fi) { int x; root= null; try { fs1=new FileInputStream(fi); Scanner sc=new Scanner(fs1); while(sc.hasNextInt()) { x=sc.nextInt(); ChenX(x); } } catch(java.io.FileNotFoundException e) { System.out.print("\n Loi vao ra file "+ e + "\n"); } } private void duyet(TTNode t) { if(t!=null) { duyet(t.left); for(int i=1; i<= t.count; i++ ) out.print(" "+t.data); duyet(t.right); } } void duyet_ghi_file(String fo) { try { fos=new FileOutputStream(fo); out=new PrintWriter(fos,true); duyet(root); out.close(); } catch(java.io.FileNotFoundException e) { System.out.print("\n Loi ghi file "+ e + "\n"); } } }

Page 43: Bai tap tham khao CSPE

                                                       Trang  43                                                                            [email protected] 

package my_avltree; import java.util.*; //import java.util.Queue; class ANode { int data; ANode left,right; ANode parent; ANode(){} ANode(int x) { data=x; left=right=null; parent=null; } ANode(int x,ANode ll,ANode rr) { data=x; left=ll; right=rr; parent=null; if(ll!=null) ll.parent=this; if(rr!=null) rr.parent=this; } } class AVLTree { ANode root; private int cao(ANode t) { if(t==null) return 0; else return 1+Math.max(cao(t.left),cao(t.right)); } int cao() { return cao(root); } ANode qr(ANode t) { ANode t1=t.right; ANode t2=t1.left; t1.parent=t.parent; t.right=t2;

Page 44: Bai tap tham khao CSPE

                                                       Trang  44                                                                            [email protected] 

if(t2!=null) t2.parent=t; t1.left=t; t.parent=t1; return t1; } ANode ql(ANode t) { ANode x,y; x=t.left; y=x.right; x.parent= t.parent; t.left=y; if(y!=null) y.parent= t; x.right=t; t.parent=x; return x; } ANode qlr(ANode t) { ANode x,y,z,k; x=t.right; y=x.left; z=y.left; k=y.right; y.parent=t.parent; t.right=z; if(z!=null) z.parent=t; x.left=k; if(k!=null) k.parent=x; y.left=t; t.parent=x; y.right=x; x.parent=y; return y; } ANode qrl(ANode t) { ANode x,y,z,k; x=t.left; y=x.right; z=y.left; k=y.right; y.parent=t.parent; x.right=z; if(z!=null) z.parent=x;

Page 45: Bai tap tham khao CSPE

                                                       Trang  45                                                                            [email protected] 

t.left=k; if(k!=null) k.parent=t; y.left=x; x.parent=y; y.right=t; t.parent=y; return y; } ANode chenx(ANode t,int x) { if(t==null) return new ANode(x); else if(t.data==x) return t; else { if(x<t.data) { t.left=chenx(t.left,x); t.left.parent=t; } else { t.right=chenx(t.right,x); t.right.parent=t; } int h1=cao(t.left); int h2=cao(t.right); if(h1>h2+1) { int h11=cao(t.left.left); int h12=cao(t.left.right); if(h11>h12) return ql(t); else return qrl(t); } else if(h2>h1+1) { int h21=cao(t.right.left); int h22=cao(t.right.right); if(h22>h21) return qr(t); else

Page 46: Bai tap tham khao CSPE

                                                       Trang  46                                                                            [email protected] 

return qlr(t); } else return t; } } void chenx(int x) { root=chenx(root,x); } ANode del(int x, ANode rr) { if(rr== null) return null; else if(x<rr.data) { rr.left= del(x,rr.left); return rr;} else if(x>rr.data) { rr.right= del(x,rr.right); return rr;} else { if (rr.left!=null && rr.right!=null) { // xoa max con trai ANode p=rr.left; while (p.right!=null) p=p.right; rr.data=p.data; rr.left= del(p.data,rr.left); return rr; } else { ANode p=rr.parent; if(rr.left==null&& rr.right==null) rr=null; else if (rr.right==null) rr= rr.left; else rr=rr.right; while(p!=null) { //xxxxx } return rr; } } }

Page 47: Bai tap tham khao CSPE

                                                       Trang  47                                                                            [email protected] 

void taocay() { root=null;int x; Scanner kb=new Scanner(System.in); while(true) { System.out.print("nhap so x chen vao cay"); x=kb.nextInt(); if(x==0) break; chenx(x); } } void duyet2(ANode t) { if(t!=null) { duyet2(t.left); System.out.print(t.data+" "); duyet2(t.right); } } void duyettt() { duyet2(root); } void duyettheomuc(ANode T ) { Queue<ANode> Q=new LinkedList<ANode>(); Q.offer(T); while(!Q.isEmpty()) { ANode x= Q.remove(); if(x.parent!=null) System.out.print("[" + x.parent.data + "] -->"); else System.out.print("[" + root + "]--->" ); System.out.print(x.data+" "); if(x.left!=null) Q.offer(x.left); if(x.right!=null) Q.offer(x.right); } } void duyettheomuc( ) { if(root==null)System.out.print("\n\n Cay rong!\n "); else duyettheomuc( root); }

Page 48: Bai tap tham khao CSPE

                                                       Trang  48                                                                            [email protected] 

void duyettheomuc1(ANode T ) { Queue<ANode> Q=new LinkedList<ANode>(); Queue<ANode> Qp=new LinkedList<ANode>(); // cha cua nut trong q Q.offer(T); Qp.offer(null); while(!Q.isEmpty()) { ANode x= Q.remove(); ANode y= Qp.remove(); if(y!=null) System.out.print("[" + y.data + "] -->"); else System.out.print("[ root ]--->" ); System.out.print(x.data+" "); if(x.left!=null) { Q.offer(x.left); Qp.offer(x);} if(x.right!=null) {Q.offer(x.right); Qp.offer(x);} } } void duyettheomuc1( ) { if(root==null)System.out.print("\n\n Cay rong!\n "); else duyettheomuc1( root); } }// end class public class AVL { public static void main(String[] args) { AVLTree a=new AVLTree(); a.taocay(); a.duyettt(); System.out.print("\nDuyet theo muc\n "); a.duyettheomuc1(); } }

Page 49: Bai tap tham khao CSPE

                                                       Trang  49                                                                            [email protected] 

package my_hashtable; /** * * @author dieuhb */ import java.util.*; import java.io.*; public class My_Hash_Table { /** * @param args the command line arguments */ final int tablesize=101; String []ht; int num; My_Hash_Table() { ht= new String[tablesize]; num=0; } int hf( String t) // hàm băm xác định vị trí của từ trong bảng băm { int s=0; for(int i=0; i<t.length(); i++) s=s + (int) t.charAt(i); return s % tablesize; } void insert(String t) { if(num<tablesize) { int vt= hf(t); // xác định vị trí trong bảng băm while(true) { if(ht[vt]==null) {ht[vt]=t; break;} // neu co khe trong thi chen vao va thoat else if(ht[vt].equals(t)) break; // đã có giá trị trong bảng thi thoát else vt = (vt+ 1)% tablesize; // tìm ô rỗng để chèn } } else System.out.print("\n FULL HASH TABLE"); }

Page 50: Bai tap tham khao CSPE

                                                       Trang  50                                                                            [email protected] 

public String toString() { String t="{"; for(int i=0; i<tablesize; i++) { if(i%10==0) t=t + "\n"; t= t + " [" + i+ "," + ht[i] + "];"; } t= t +"}"; return t; } void LoadfromFile(String filename) { try{ FileReader fr= new FileReader(filename); Scanner sc= new Scanner(fr); while (sc.hasNext()) { String t= sc.next(); insert(t); } }catch(FileNotFoundException e){ System.out.println(e.getMessage()); }catch(IOException e){ System.out.println(e.getMessage()); } } public static void main(String[] args) { My_Hash_Table ht= new My_Hash_Table(); /* System.out.print("\n" + "a=" + ht.hf("a")); System.out.print("\n" + "demo=" + ht.hf("demo")); System.out.print("\n" + "for=" + ht.hf("for")); ht.insert("ha"); ht.insert("noi"); ht.insert("mua");ht.insert("thu");ht.insert("la"); ht.insert("roi"); ht.insert("lop");ht.insert("dop"); */ ht.LoadfromFile("d:\\thongtin.txt"); System.out.print("\n" + ht); } }