Upcoming SlideShare
×

# Bai tap tham khao CSPE

370
-1

Published on

0 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• Be the first to comment

• Be the first to like this

Views
Total Views
370
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
3
0
Likes
0
Embeds 0
No embeds

No notes for slide

### Bai tap tham khao CSPE

1. 1. 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); }}                                                       Trang  1                                                                            dieuhb@gmail.com
2. 2. 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); }}                                                       Trang  2                                                                            dieuhb@gmail.com
3. 3. 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;}                                                       Trang  3                                                                            dieuhb@gmail.com
4. 4. 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 "); }}                                                       Trang  4                                                                            dieuhb@gmail.com
5. 5. 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 giayn"); 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 giayn"); 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 giayn"); }}                                                       Trang  5                                                                            dieuhb@gmail.com
6. 6. 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("nn---- Hien thi danh sach giam dan ----nn"); 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();                                                       Trang  6                                                                            dieuhb@gmail.com
7. 7. System.out.print("nn ------ Thoi gian thuc hien:" + (t2-t1) + "n"); // thời giantính theo mili giây t1=System.currentTimeMillis(); m.sapxep(); m.xuat(); t2=System.currentTimeMillis(); System.out.print("nn ------ 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(); }}                                                       Trang  7                                                                            dieuhb@gmail.com
8. 8. 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) + " giayn"); }}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; }                                                       Trang  8                                                                            dieuhb@gmail.com
9. 9. 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 FULLn"); 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 FULLn"); 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 len"); return -1;} else return arr[i]; }                                                       Trang  9                                                                            dieuhb@gmail.com
10. 10. void set(int i, int x) { if(i<0|| i>=spt) System.out.print("n Vi tri khong hop len"); else arr[i]=x; } int remove(int vt) { if (vt<1|| vt>=spt) {System.out.print("n Khong xoa duocn"); 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 rongn"); return -1;} } void insert(int i, int x) { if(i<0||i>spt) System.out.print("nVi tri khong hop len"); else { // cap phat vung nho neu thieu if (spt==arr.length) { System.out.print("n Phai cap phat themn");                                                       Trang  10                                                                            dieuhb@gmail.com
11. 11. 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); */ }}                                                       Trang  11                                                                            dieuhb@gmail.com
12. 12. 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 sachn"); 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!");                                                       Trang  12                                                                            dieuhb@gmail.com
13. 13. 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");                                                       Trang  13                                                                            dieuhb@gmail.com
14. 14. 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( " nulln"); }}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(); }}                                                       Trang  14                                                                            dieuhb@gmail.com
15. 15. 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 sachn"); do { System.out.print("nSo thu " + (i++)+ ":"); x=kb.nextInt(); if(x<=0) break;                                                       Trang  15                                                                            dieuhb@gmail.com
16. 16. 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( " nulln"); }}public class Double_linked_List { public static void main(String[] args) { // TODO code application logic here MyDoubleLinkedList LL = new MyDoubleLinkedList(); LL.tao(); LL.duyet(); }}                                                       Trang  16                                                                            dieuhb@gmail.com
17. 17. 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 dayn"); } // 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="--> nulln"; for(int i=1; i<=top; i++) t="--->" + a[i] + t; return t; }}                                                       Trang  17                                                                            dieuhb@gmail.com
18. 18. 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); }                                                       Trang  18                                                                            dieuhb@gmail.com
19. 19. int PopS() { if (top==null) {System.out.print("n Ngan xep rongn"); return -1;} else {int x=top.data; top=top.next; return x;} } int PeekS() { if (top==null) {System.out.print("n Ngan xep rongn"); 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 stackclass 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;                                                       Trang  19                                                                            dieuhb@gmail.com
20. 20. 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 dayn"); else { top++; a[top]=x; } } khoi PopS() { khoi x= new khoi(); if(top==0) { System.out.print("nNgan xep rongn"); 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); } }                                                       Trang  20                                                                            dieuhb@gmail.com
21. 21. 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 XONGnn"); bb.hn(3,a, b,c); // goi khu de qui System.out.print("n XONGn"); }}                                                       Trang  21                                                                            dieuhb@gmail.com
22. 22. 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);                                                       Trang  22                                                                            dieuhb@gmail.com
23. 23. } 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()                                                       Trang  23                                                                            dieuhb@gmail.com
24. 24. { 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; /*                                                       Trang  24                                                                            dieuhb@gmail.com
25. 25. 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);}                                                       Trang  25                                                                            dieuhb@gmail.com
26. 26. 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 cayn"); 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;}                                                       Trang  26                                                                            dieuhb@gmail.com
27. 27. 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);                                                       Trang  27                                                                            dieuhb@gmail.com
28. 28. } }}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 cayn"); else System.out.print("n khong co gia tri 44 trong cayn"); 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; }}                                                       Trang  28                                                                            dieuhb@gmail.com
29. 29. 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                                                       Trang  29                                                                            dieuhb@gmail.com
30. 30. 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 XONGn "); }}                                                       Trang  30                                                                            dieuhb@gmail.com
31. 31. package my_sort;import java.util.Scanner;/** * * @author Huynh Ba Dieu .... dieuhb@gmail.com */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;} } }                                                       Trang  31                                                                            dieuhb@gmail.com
32. 32. 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;                                                       Trang  32                                                                            dieuhb@gmail.com
33. 33. 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);                                                       Trang  33                                                                            dieuhb@gmail.com
34. 34. 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 QUICKSORT:"); //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 HEAPSORT:"); 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 MERGESORT:"); //m1.in(); System.out.print("n thoi gian thuc hien thuat toan: " + (t2-t1)); m1.chep();                                                       Trang  34                                                                            dieuhb@gmail.com
35. 35. t1=System.currentTimeMillis(); m1.Select_Sort(); t2=System.currentTimeMillis(); System.out.print("n mang m1 sau khi sap xep THEO THUAT TOAN SELECTSORT:"); //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 INSERTSORT:"); //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 BUBBLESORT:"); //m1.in(); System.out.print("n thoi gian thuc hien thuat toan: " + (t2-t1)); System.out.print("n ----XONG---n");*/ }}                                                       Trang  35                                                                            dieuhb@gmail.com
36. 36. 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 rongn"); 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; } }                                                       Trang  36                                                                            dieuhb@gmail.com
37. 37. public String toString() { String t="n"; Node p=head; while (p!=null) { t= t + "-->" + p.data; p=p.next;} t=t + "--->nulln"; 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 tun"); 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();                                                       Trang  37                                                                            dieuhb@gmail.com
38. 38. 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 tun"); 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                                                       Trang  38                                                                            dieuhb@gmail.com
39. 39. 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 rongn"); 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]); }                                                       Trang  39                                                                            dieuhb@gmail.com
40. 40. 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(); }}                                                       Trang  40                                                                            dieuhb@gmail.com
41. 41. 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("nn Thoi gian sap xep du lieu la:" + (t2-t1) + " milisecond n"); System.out.print("n XONGn "); }}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;}                                                       Trang  41                                                                            dieuhb@gmail.com
42. 42. 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"); } } }                                                       Trang  42                                                                            dieuhb@gmail.com
43. 43. 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;                                                       Trang  43                                                                            dieuhb@gmail.com
44. 44. 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;                                                       Trang  44                                                                            dieuhb@gmail.com
45. 45. 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                                                       Trang  45                                                                            dieuhb@gmail.com
46. 46. 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; } } }                                                       Trang  46                                                                            dieuhb@gmail.com
47. 47. 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("nn Cay rong!n "); else duyettheomuc( root); }                                                       Trang  47                                                                            dieuhb@gmail.com
48. 48. 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("nn Cay rong!n "); else duyettheomuc1( root); }}// end classpublic class AVL { public static void main(String[] args) { AVLTree a=new AVLTree(); a.taocay(); a.duyettt(); System.out.print("nDuyet theo mucn "); a.duyettheomuc1(); }}                                                       Trang  48                                                                            dieuhb@gmail.com
49. 49. 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"); }                                                       Trang  49                                                                            dieuhb@gmail.com
50. 50. 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); }}                                                       Trang  50                                                                            dieuhb@gmail.com