Bai tap tham khao CSPE

  • Published on
    05-Dec-2014

  • View
    581

  • Download
    0

Embed Size (px)

DESCRIPTION

 

Transcript

  • 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; in) inkq(); else { for(char c= a; c 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()Trang23dieuhb@gmail.com
  • 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; /*Trang24dieuhb@gmail.com
  • 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);}Trang25dieuhb@gmail.com
  • 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(xrr.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(xrr.data) { rr.right= del(x,rr.right); return rr;}Trang26dieuhb@gmail.com
  • 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