kiem tien, kiem tien online, kiem tien truc tuyen, kiem tien tren mang
Thứ Sáu, 5 tháng 9, 2014

#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;
}
05 Sep 2014

0 nhận xét:

Đăng nhận xét

:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.

domain, domain name, premium domain name for sales

Bài đăng phổ biến