Latest topics | » Thật buồn khi 4r không còn hoạt động như xưa nữa :(Sat Jun 09, 2012 10:03 pm by bankimo » Bình chọn áo đấu các CLB, đội tuyểnTue May 29, 2012 4:38 pm by holyangell » Game dàn trận theo lược PS1 còn gọi là Thập Tự Chinh(Rất hay nghe..)Sat Apr 14, 2012 4:24 pm by zjnxct » Xem bóng đá ngoại hạng anh miễn phái K+1 HD và K+ NS Sun Mar 11, 2012 11:05 pm by bankimo » Có thật lòng yêu ?Wed Mar 07, 2012 2:48 pm by holyangell » Auto Võ lâm 2-Chip Auto Võ lâm 2 crack BẢn Mới Nhất Một Số Auto HOtTue Mar 06, 2012 10:11 am by muabanitem » Auto Võ Lâm 2, ChipAuto auto kích tống liêu, train lv. Auto chế phù, chế đồTue Mar 06, 2012 10:10 am by muabanitem » Auto Võ Lâm 2: tất cả các loại auto Víp và crack đầy đủ hoàn thiện 100%Tue Mar 06, 2012 10:09 am by muabanitem » Auto Chien Truong Võ Lâm 2Tue Mar 06, 2012 10:08 am by muabanitem |
May 2024 | Sun | Mon | Tue | Wed | Thu | Fri | Sat |
---|
| | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | Calendar |
|
Thắc mắc | Người Quản TrịModerator |
Statistics | Diễn Đàn hiện có 7385 thành viên Chúng ta cùng chào mừng thành viên mới đăng ký: lanhuong dinh
Tổng số bài viết đã gửi vào diễn đàn là 5059 in 1953 subjects
|
| | Tác giả | Thông điệp |
---|
caingocthanh Moderators
Tổng số bài gửi : 559 Join date : 27/04/2009 Age : 35 Đến từ : Cà Mau
| Tiêu đề: Bài CTDL mới thực hành Sun May 09, 2010 1:59 pm | |
| | | | | Ở đây mới chỉ là duyệt cây theo preorder thôi,còn inorder và posorder thì mình sẽ post sau. - Trích dẫn :
- #include<conio.h>
#include<stdio.h> #include<string.h> #define NIL -1 #define MaxLength 100 typedef int Node; typedef char DataType; typedef struct Tree { DataType Data[MaxLength];//ham luu tru data Node Parent[MaxLength]; //ham luu tru parent int MaxNode; //so nut thuc su trong cay } Tree; Tree T; //khoi tao cay rong void MakeNULL_Tree(Tree*T) { T->MaxNode=0; } //kiem tra cay rong int Empty_Tree(Tree T) { return T.MaxNode==0; } void ReadTree(Tree*T) { int i; MakeNULL_Tree(&(*T)); do { printf("So nut cua cay la:"); scanf("%d",&(*T).MaxNode); } while(((*T).MaxNode<1)||((*T).MaxNode>MaxLength)); printf("Nhap gia tri nut goc"); fflush(stdin); scanf("%c",&(*T).Data[0]); (*T).Parent[0]=NIL; for(i=1;i<=(*T).MaxNode-1;i++) { printf("Nhap cha cua nut %d la",i); scanf("%d",&(*T).Parent[i]); printf("Nhap gia tri cua nut %d la",i); fflush(stdin); scanf("%c",&(*T).Data[i]); } } //xac dinh cha cua nut n tren cay T Node Parent(Node n,Tree T) { if(Empty_Tree(T)||n>T.MaxNode-1) return NIL; else return T.Parent[n]; } //ham xac dinh gia tri cua nut tren cay T DataType Data(Node n,Tree T) { if(Empty_Tree(T) || n>T.MaxNode-1) { printf("Error!\n"); return '*'; } else return T.Data[n]; } //ham lay nut con trai nhat Node LeftMostChild(Node n,Tree T) { if(n<0||Empty_Tree(T)||n>=T.MaxNode-1) { return NIL; } else { Node i=n+1; while(i<T.MaxNode) { if(T.Parent[i]==n) return i; else i++; } return NIL; } } //ham lay anh em ruot phai cua nut n trong cay Node RightSibling(Node n,Tree T) { if(n<0) return NIL; Node i=n+1; while(i<T.MaxNode) { if(T.Parent[n]==T.Parent[i]) return i; else i++; } return NIL; } //ham xac dinh nut doc cua cay Node Root(Tree T) { if(!Empty_Tree(T)) return 0; else return NIL; } //ham duyet tien tu cay void Preorder(Node n,Tree T) { Node i; printf("%c",Data(n,T)); i=LeftMostChild(n,T); while(i!=NIL) { Preorder(i,T); i=RightSibling(i,T); } } void main() { clrscr(); Tree T; printf("Bat dau nhap du lieu cho cay tong quat:\n"); ReadTree(&T); printf("Danh sach duyet tien tu cua cay vua nhap la:"); Preorder(Root(T),T); printf("Danh sach duyet trung tu cua cay la:"); getch(); } | | | | |
|
| | | vo minh phuc Ký Danh
Tổng số bài gửi : 5 Join date : 27/03/2010 Age : 34
| Tiêu đề: Re: Bài CTDL mới thực hành Wed May 12, 2010 10:14 pm | |
| |
| | | lethanhthuan Nhập Môn
Tổng số bài gửi : 31 Join date : 14/05/2009 Age : 35 Đến từ : ĐỒNG THÁP
| Tiêu đề: Re: Bài CTDL mới thực hành Thu May 20, 2010 10:42 pm | |
| | | | | #include<conio.h> #include<stdio.h> #include<string.h> #define NIL -1 #define MaxLength 100 typedef int Node; typedef char DataType; typedef struct Tree { DataType Data[MaxLength];//ham luu tru data Node Parent[MaxLength]; //ham luu tru parent int MaxNode; //so nut thuc su trong cay } Tree; Tree T; //khoi tao cay rong void MakeNULL_Tree(Tree*T) { T->MaxNode=0; } //kiem tra cay rong int Empty_Tree(Tree T) { return T.MaxNode==0; } void ReadTree(Tree*T) { int i; MakeNULL_Tree(&(*T)); do{ printf("So nut cua cay la:"); scanf("%d",&(*T).MaxNode); } while(((*T).MaxNode<1)||((*T).MaxNode>MaxLength)); printf("Nhap gia tri nut goc"); fflush(stdin); scanf("%c",&(*T).Data[0]); (*T).Parent[0]=NIL; for(i=1;i<=(*T).MaxNode-1;i++) { printf("Nhap cha cua nut %d la",i); scanf("%d",&(*T).Parent[i]); printf("Nhap gia tri cua nut %d la",i); fflush(stdin); scanf("%c",&(*T).Data[i]); } } //xac dinh cha cua nut n tren cay T Node Parent(Node n,Tree T) { if(Empty_Tree(T)||n>T.MaxNode-1) return NIL; else return T.Parent[n]; } //ham xac dinh gia tri cua nut tren cay T DataType Data(Node n,Tree T) { if(Empty_Tree(T) || n>T.MaxNode-1) { printf("Error!\n"); return '*'; } else return T.Data[n]; } //ham lay nut con trai nhat Node LeftMostChild(Node n,Tree T) { if(n<0||Empty_Tree(T)||n>=T.MaxNode-1) { return NIL; } else { Node i=n+1; while(i<T.MaxNode) { if(T.Parent[i]==n) return i; else i++; } return NIL; } } //ham lay anh em ruot phai cua nut n trong cay Node RightSibling(Node n,Tree T) { if(n<0) return NIL; Node i=n+1; while(i<T.MaxNode) { if(T.Parent[n]==T.Parent[i]) return i; else i++; } return NIL; } //ham xac dinh nut doc cua cay Node Root(Tree T) { if(!Empty_Tree(T)) return 0; else return NIL; } //ham duyet tien tu cay void Preorder(Node n,Tree T) { Node i; printf("%c",Data(n,T)); i=LeftMostChild(n,T); while(i!=NIL) { Preorder(i,T); i=RightSibling(i,T); } } int baccuacay(tree T) { int max=MIL; (for(int i=0;i<T.MaxNode;i++) { int temp=0; for(int j=0;j<T.MaxNode;j++) { if(T.Parent[j]==i) temp++; } if(max<temp) max=temp; } return max; } int chieucaocay(Node n,Tree T) { if(LeftMostChild(n,T)==NIL) return 0; int temp=0; Node i=LeftMostChild(n,T) ; while(i!=NIL) { if(temp<chieucaocay(i,T)) temp=chieucaocay(i,T); i=RightSibling(i,T); } temp++; return temp ; } void main() { clrscr(); Tree T; printf("Bat dau nhap du lieu cho cay tong quat:\n"); ReadTree(&T); printf("Danh sach duyet tien tu cua cay vua nhap la:"); Preorder(Root(T),T); printf("Danh sach duyet trung tu cua cay la:"); printf("bac cua cay la %d =",baccuacay(T)); printf("\n"); printf("chieu cao cua cay la %d ",chieucaocay(Root(T),T)); getch(); }
ham void main() dung ko may ban | | | | |
|
| | | Sponsored content
| Tiêu đề: Re: Bài CTDL mới thực hành | |
| |
| | | |
Trang 1 trong tổng số 1 trang | |
| Permissions in this forum: | Bạn không có quyền trả lời bài viết
| |
| |
| |