#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
-
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 ...
-
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)...
-
Có thể xài được nhưng cũng có thể không xài được nếu như chủ nhân đã đổi pass. Acc: hoanglinh1714 Pass: A01656101024LINH Acc: hieukenpt19999...
-
Bổ sung vào các chức năng đã có ở hai phiên bản trước, phần mềm Quản lý gia phả phiên bản Advanced được nâng cấp thêm các tính năng nổi trội...
-
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 ...
-
Gia tộc 4.6 Một số thay đổi nhỏ giao diện. Thêm chức năng cập nhật chi phái hàng loạt. Thay đổi mẫu xuất hồ sơ thành viên đủ tốt để in ấn. ...
-
Những số liệu thú vị về sự tương tác của người dùng với nội dung post lên Facebook Một khi đã đăng trạng thái lên Facebook thì ít nhiều các ...
-
Phòng tránh "lỗi màn hình xanh" trên Windows 8 Vấn đề “màn hình xanh” dẫn tới treo máy hay thậm chí tổn hại đến cả phần cứng và ph...
-
Các vụ trộm cắp, giết người, khủng bố, phân biệt chủng tộc, bạo hành khiến con người đứng trước nguy cơ một thế giới không hòa bình. Cùng tì...
-
Những điều nên và không nên làm vào thứ sáu ngày 13 Đây là quan niệm ở một số nước phương Tây và nếu đi du lịch đúng vào ngày này, các bạn c...

0 nhận xét:
Đăng nhận xét