#include <stdio.h>
#include <conio.h>
#define MaxLength 10
#define NIL -1
typedef int Node;
typedef char DataType;
typedef struct Tree{
DataType Data[MaxLength];
Node Parent[MaxLength];
int MaxNode;
};
void MakeNullTree(Tree &T){
T.MaxNode=0;
}
int EmptyTree (Tree T){
return (T.MaxNode == 0);
}
Node Parent (Node n,Tree T){
if(EmptyTree(T) || n>T.MaxNode-1)
return NIL;
else
return T.Parent[n];
}
DataType LabelNode(Node n, Tree T){
if(!EmptyTree(T) && n<=T.MaxNode-1)
return T.Data[n];
}
Node Root(Tree T){
if(EmptyTree(T))
return NIL;
else
return 0;
}
Node LeftMostChild (Node n, Tree T){
if (n<0) return NIL;
for(Node i=n+1;i<T.MaxNode;i++){
if(Parent(i,T) ==n)
return i;
}
return NIL;
}
Node RightSibling(Node n, Tree T){
if(n<0) return NIL;
Node parent = Parent(n,T);
for(Node i=n+1;i<T.MaxNode;i++){
if(Parent(i,T)==parent)
return i;
}
return NIL;
}
void PreOrder(Node n, Tree T){
if(!EmptyTree(T)){
printf("%c ",LabelNode(n,T));
Node i = LeftMostChild(n,T);
while(i!=NIL){
PreOrder(i,T);
i=RightSibling(i,T);
}
}
}
void InOrder(Node n, Tree T){
Node i = LeftMostChild(n,T);
if(i!=NIL)
InOrder(i,T);
printf("%c ",LabelNode(n,T));
i=RightSibling(i,T);
while(i!=NIL){
InOrder(i,T);
i=RightSibling(i,T);
}
}
void PostOrder(Node n,Tree T){
Node i=LeftMostChild(n,T);
while (i!=NIL){
PostOrder(i,T);
i=RightSibling(i,T);
}
printf("%c ",LabelNode(n,T));
}
int Depth(Node n, Tree T){
Node p=Parent(n,T);
if(p==-1)
return 0;
return 1+Depth(p,T);
}
int OrderNode(Node n, Tree T){
int bac=0;
for(Node i=n+1;i<T.MaxNode;i++)
bac+=(Parent(i,T)==n);
return bac;
}
int OrderTree(Tree T){
int m=0;
for(Node i=0;i<T.MaxNode;i++){
int n=OrderNode(i,T);
if (m<n)
m=n;
}
return m;
}
int IsLeaf(Node n, Tree T){
return (LeftMostChild(n,T)==NIL);
}
int Height(Tree T){
int h=0;
for(Node i = 0; i<T.MaxNode;i++)
if(IsLeaf(i,T)){
int d=Depth(i,T);
if (h<d)
h=d;
}
return h;
}
int IsAncestor(Node a, Node d, Tree T){
while (d!=NIL){
if (a==d)
return 1;
else
d=Parent(d,T);
}
return 0;
}
Node CommonAncestor(Node n,Node m, Tree T){
int dn=Depth(n,T) , dm=Depth(m,T);
if (Parent(n,T)== Parent(m,T)) return Parent(n,T);
if (Parent(n,T)== m) return m;
if (Parent(m,T)== n) return n;
if (dn > dm)
CommonAncestor(Parent(n,T),m,T);
else if (dn<dm)
CommonAncestor(n,Parent(m,T),T);
else
CommonAncestor(Parent(n,T),Parent(m,T),T);
}
int main(){
Tree T;
MakeNullTree(T);
char data[]={'A','B','C','D','E','F','G','H','I','J'};
int parent[]={-1, 0, 0, 1, 1, 4, 4, 4, 2, 2 };
for(int i=0;i<10;i++){
T.Data[i]= data[i];
T.Parent[i]=parent[i];
}
T.MaxNode=10;
printf("Duyet tien tu: "); PreOrder(0,T);
printf("\nDuyet trung tu: "); InOrder(0,T);
printf("\nDuyet hau tu: "); PostOrder(0,T);
printf("\nDo sau cua nut D: %d",Depth(3,T));
printf("\nBac cua nut A = %d va nut E = %d",OrderNode(0,T),OrderNode(4,T));
printf("\nBac cua cay T = %d",OrderTree(T));
printf("\nIsLeaf(B) ? %d ", IsLeaf(1,T));
printf("\nIsLeaf(J) ? %d ", IsLeaf(9,T));
printf("\nChieu cao cay T = %d",Height(T));
printf("\nIsAncestor(B,H) ? %d",IsAncestor(1,7,T));
printf("\nIsAncestor(D,H) ? %d",IsAncestor(3,7,T));
printf("\nIsAncestor(D,A) ? %d",IsAncestor(3,0,T));
printf("\nIsAncestor(D,D) ? %d",IsAncestor(3,3,T));
printf("\nCommonAncestor(F,B) ? %c",LabelNode(CommonAncestor(5,1,T),T));
printf("\nCommonAncestor(D,F) ? %c",LabelNode(CommonAncestor(3,5,T),T));
printf("\nCommonAncestor(F,D) ? %c",LabelNode(CommonAncestor(5,3,T),T));
printf("\nCommonAncestor(D,J) ? %c",LabelNode(CommonAncestor(3,8,T),T));
return 0;
}
Thứ Sáu, 5 tháng 9, 2014
Đăng ký:
Đăng Nhận xét (Atom)
Bài đăng phổ biến
-
* lưu ý: tính năng này yêu cầu bạn phải có kết nối Internet khi sử dụng phần mềm. Bạn vui lòng thực hiện các bước sau để đăng ký dùng miễn p...
-
Công cụ Đăng Ký Bản Quyền Sử Dụng Kế Toán Smart Pro ( 2.0 - 2.5 - 3.0) Video Hướng Dẫn Đăng Ký Bản Quyền Kế Toán Smart Pro (2.0 - 2.5 - 3.0)...
-
HTsoft POS .NET là phần mềm quản lý Kho-Bán hàng và Chăm sóc khách hàng chuyên nghiệp, áp dụng tốt cho nhiều lĩnh vực kinh doanh khác nhau ...
-
Clover 3.0.386 - Tạo Tabs File Explorer cho Windows 8.1 http://www.softpedia.com/progDownload/Clover-EJIE-Download-220301.html
-
Đề bài: nhập 2 số nguyên dương a,b. Tính ước số chung lớn nhất và bội chung nhỏ nhất của a,b. Bài giải: Cách 1: #include <stdio.h> int...
-
Phần mềm quản lý bán hàng TTV Sales là giải pháp giúp các doanh nghiệp quản lý các chuỗi cửa hàng, sản phẩm, nhân viên một cách có hệ thống ...
-
#include <conio.h> #include <stdio.h> #define max 100 /*Hàm nhập ma trận hệ số*/ void NhapMaTran ( float A [ max ][ max ], in...
-
Đề bài: nhập vào tử số, mẫu số (khác 0) của một phân số. Hãy rút gọn phân số này. Chú ý chọn dạng xuất thích hợp trong trường hợp mẫu số bằn...
-
HDMI hiện là cổng giao tiếp phổ biến nhất trên TV. Nhưng nếu muốn kết nối máy tính với TV (hay màn hình mới), bạn sẽ có nhiều tùy chọn hơn n...
-
MAGIX Video Pro X5 - là giải pháp hoàn hảo để chỉnh sửa video. Chương trình được thiết kế đặc biệt để đáp ứng các đòi hỏi yêu cầu nhiều nh...
0 nhận xét:
Đăng nhận xét