Skip to content

Commit dfa5b20

Browse files
committed
update
1 parent 4e6f377 commit dfa5b20

11 files changed

+458
-0
lines changed

4-3-15

13.3 KB
Binary file not shown.

4-3-15.cpp

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#include<iostream>
2+
#define ELEMTYPE int
3+
#define MAXSIZE 50
4+
#define END 9999
5+
#define NOP -1
6+
using namespace std;
7+
typedef struct BiNode{
8+
ELEMTYPE data;
9+
struct BiNode *lchild,*rchild;
10+
}BiNode,*BiTree;
11+
typedef struct ListNode{
12+
int lchild;
13+
int rchild;
14+
ELEMTYPE data;
15+
}List;
16+
void fun_start(BiTree &T);
17+
void fun(ELEMTYPE A[],ELEMTYPE B[],int astart,int bstart,int n){
18+
if(n>0){
19+
int rt=(n-1)/2;
20+
fun(A,B,astart+1,bstart,rt);
21+
fun(A,B,astart+rt+1,bstart+rt,rt);
22+
B[bstart+n-1]=A[astart];
23+
}
24+
}
25+
//不使用第一项
26+
void InitList(List L[]){
27+
int x=0,y=0,n=0;
28+
ELEMTYPE d;
29+
L[0]={0,0,0};
30+
cout<<"input numbers of tree node:"<<endl;
31+
cin>>n;
32+
for(int i=1;n<MAXSIZE&&i<=n;i++){
33+
cin>>x>>y>>d;
34+
L[i].lchild=x;
35+
L[i].rchild=y;
36+
L[i].data=d;
37+
}
38+
}
39+
void InitTreeWithList(BiTree &T,List L[],int index){
40+
if(index!=NOP){
41+
BiTree nd=(BiTree)malloc(sizeof(BiNode));
42+
nd->data=L[index].data;
43+
T=nd;
44+
InitTreeWithList(T->lchild,L,L[index].lchild);
45+
InitTreeWithList(T->rchild,L,L[index].rchild);
46+
}else{
47+
T=NULL;
48+
}
49+
}
50+
ELEMTYPE A[MAXSIZE];
51+
int ai=0;
52+
// 1 for PreOrder,2 for InOrder,3 for PostOrder
53+
void PrintTree(BiTree T,int x){
54+
if(T){
55+
if(x==1){
56+
cout<<T->data<<" ";
57+
A[ai++]=T->data;
58+
}
59+
PrintTree(T->lchild,x);
60+
if(x==2){
61+
cout<<T->data<<" ";
62+
}
63+
PrintTree(T->rchild,x);
64+
if(x==3){
65+
cout<<T->data<<" ";
66+
}
67+
if(x==3)free(T);
68+
}
69+
}
70+
void fun_start(BiTree &T){
71+
//以下是一个预置的初始化数组
72+
//不使用第一项
73+
List L0[10]={{0,0,0},{2,3,11},{4,5,12},{6,7,13},{NOP,NOP,14},{NOP,NOP,15},{NOP,NOP,16},{NOP,NOP,17},{NOP,NOP,18},{NOP,NOP,19}};
74+
//List L[MAXSIZE];
75+
//InitList(L);
76+
InitTreeWithList(T,L0,1);
77+
cout<<"tree:"<<endl;
78+
PrintTree(T,1); //set A[]
79+
cout<<endl;
80+
PrintTree(T,3);//postorder
81+
cout<<endl;
82+
A[ai]=END;
83+
ELEMTYPE B[MAXSIZE];
84+
fun(A,B,0,0,ai);
85+
B[ai]=END;
86+
cout<<"fun:PostOrder"<<endl;
87+
for(int i=0;B[i]!=END;i++){
88+
cout<<B[i]<<" ";
89+
}
90+
}
91+
int main(){
92+
BiTree T;
93+
fun_start(T);
94+
cout<<endl;
95+
return 0;
96+
}

4-4-5

13.2 KB
Binary file not shown.

4-4-5.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include<iostream>
2+
#define ELEMTYPE int
3+
#define MAXSIZE 50
4+
#define END 9999
5+
#define NOP -1
6+
using namespace std;
7+
typedef struct TreeNode{
8+
ELEMTYPE data;
9+
struct TreeNode *firstchild,*nextsibling;
10+
}TreeNode,*Tree;
11+
typedef struct ListNode{
12+
int firstchild;
13+
int nextsibling;
14+
ELEMTYPE data;
15+
}List;
16+
int fun(Tree T){
17+
if(T){
18+
int tmp;
19+
if(T->firstchild)tmp=fun(T->firstchild);
20+
else tmp=1;
21+
tmp+=fun(T->nextsibling);
22+
return tmp;
23+
}
24+
return 0;
25+
}
26+
void InitTreeWithList(Tree &T,List L[],int index);
27+
void PrintTree(Tree T,int x);
28+
void InitList(List L[]);
29+
int main(){
30+
Tree T;
31+
//以下是一个预置的初始化数组
32+
//不使用第一项
33+
List L0[12]={{0,0,0},{2,NOP,11},{NOP,3,12},{6,4,13},{8,5,14},{NOP,NOP,15},{NOP,7,16},{9,NOP,17},{NOP,NOP,18},{NOP,10,19},{NOP,11,20},{NOP,NOP,21}};
34+
List L[MAXSIZE];
35+
//InitList(L);
36+
InitTreeWithList(T,L0,1);
37+
cout<<"count:"<<fun(T)<<endl;
38+
PrintTree(T,1);
39+
cout<<endl;
40+
return 0;
41+
}
42+
//不使用第一项
43+
void InitList(List L[]){
44+
int x=0,y=0,n=0;
45+
ELEMTYPE d;
46+
L[0]={0,0,0};
47+
cout<<"input numbers of tree node:"<<endl;
48+
cin>>n;
49+
for(int i=1;n<MAXSIZE&&i<=n;i++){
50+
cin>>x>>y>>d;
51+
L[i].firstchild=x;
52+
L[i].nextsibling=y;
53+
L[i].data=d;
54+
}
55+
}
56+
void InitTreeWithList(Tree &T,List L[],int index){
57+
if(index!=NOP){
58+
Tree nd=(Tree)malloc(sizeof(TreeNode));
59+
nd->data=L[index].data;
60+
T=nd;
61+
InitTreeWithList(T->firstchild,L,L[index].firstchild);
62+
InitTreeWithList(T->nextsibling,L,L[index].nextsibling);
63+
}else{
64+
T=NULL;
65+
}
66+
}
67+
// 1 for PreOrder,2 for InOrder,3 for PostOrder
68+
void PrintTree(Tree T,int x){
69+
if(T){
70+
if(x==1){
71+
cout<<T->data<<" ";
72+
}
73+
PrintTree(T->firstchild,x);
74+
if(x==2){
75+
cout<<T->data<<" ";
76+
}
77+
PrintTree(T->nextsibling,x);
78+
if(x==3){
79+
cout<<T->data<<" ";
80+
}
81+
free(T);
82+
}
83+
}
84+

4-4-6

13.2 KB
Binary file not shown.

4-4-6.cpp

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#include<iostream>
2+
#define ELEMTYPE int
3+
#define MAXSIZE 50
4+
#define END 9999
5+
#define NOP -1
6+
using namespace std;
7+
typedef struct TreeNode{
8+
ELEMTYPE data;
9+
struct TreeNode *firstchild,*nextsibling;
10+
}TreeNode,*Tree;
11+
typedef struct ListNode{
12+
int firstchild;
13+
int nextsibling;
14+
ELEMTYPE data;
15+
}List;
16+
int fun(Tree T){
17+
if(T){
18+
int tmp1,tmp2;
19+
if(T->firstchild)tmp1=1+fun(T->firstchild);
20+
else tmp1=0;
21+
tmp2=fun(T->nextsibling);
22+
return max(tmp1,tmp2);
23+
}
24+
return 1;
25+
}
26+
void InitTreeWithList(Tree &T,List L[],int index);
27+
void PrintTree(Tree T,int x);
28+
void InitList(List L[]);
29+
int main(){
30+
Tree T;
31+
//以下是一个预置的初始化数组
32+
//不使用第一项
33+
List L0[13]={{0,0,0},{2,NOP,11},{NOP,3,12},{6,4,13},{8,5,14},{NOP,NOP,15},{NOP,7,16},{9,NOP,17},{NOP,NOP,18},{NOP,10,19},{12,11,20},{NOP,NOP,21},{NOP,NOP,22}};
34+
List L[MAXSIZE];
35+
//InitList(L);
36+
InitTreeWithList(T,L0,1);
37+
cout<<"count:"<<fun(T)<<endl;
38+
PrintTree(T,1);
39+
cout<<endl;
40+
return 0;
41+
}
42+
//不使用第一项
43+
void InitList(List L[]){
44+
int x=0,y=0,n=0;
45+
ELEMTYPE d;
46+
L[0]={0,0,0};
47+
cout<<"input numbers of tree node:"<<endl;
48+
cin>>n;
49+
for(int i=1;n<MAXSIZE&&i<=n;i++){
50+
cin>>x>>y>>d;
51+
L[i].firstchild=x;
52+
L[i].nextsibling=y;
53+
L[i].data=d;
54+
}
55+
}
56+
void InitTreeWithList(Tree &T,List L[],int index){
57+
if(index!=NOP){
58+
Tree nd=(Tree)malloc(sizeof(TreeNode));
59+
nd->data=L[index].data;
60+
T=nd;
61+
InitTreeWithList(T->firstchild,L,L[index].firstchild);
62+
InitTreeWithList(T->nextsibling,L,L[index].nextsibling);
63+
}else{
64+
T=NULL;
65+
}
66+
}
67+
// 1 for PreOrder,2 for InOrder,3 for PostOrder
68+
void PrintTree(Tree T,int x){
69+
if(T){
70+
if(x==1){
71+
cout<<T->data<<" ";
72+
}
73+
PrintTree(T->firstchild,x);
74+
if(x==2){
75+
cout<<T->data<<" ";
76+
}
77+
PrintTree(T->nextsibling,x);
78+
if(x==3){
79+
cout<<T->data<<" ";
80+
}
81+
free(T);
82+
}
83+
}
84+

4-4-7

13.2 KB
Binary file not shown.

4-4-7.cpp

+108
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
#include<iostream>
2+
#define ELEMTYPE int
3+
#define MAXSIZE 50
4+
#define END 9999
5+
#define NOP -1
6+
using namespace std;
7+
typedef struct TreeNode{
8+
ELEMTYPE data;
9+
struct TreeNode *firstchild,*nextsibling;
10+
}TreeNode,*Tree;
11+
typedef struct ListNode{
12+
int firstchild;
13+
int nextsibling;
14+
ELEMTYPE data;
15+
}List;
16+
typedef struct DegreeList{
17+
int degree;
18+
ELEMTYPE data;
19+
}DegNode;
20+
void fun(List L[],DegNode tl[]){
21+
L[1].data=tl[1].data;
22+
L[1].nextsibling=NOP;
23+
int pre=1,last=2,next=2;//各层次起始
24+
while(pre<next){
25+
last=next;//保存下层next
26+
for(;pre<last;pre++){//扫描前一层 从pre开始至下层next
27+
L[pre].firstchild=NOP;
28+
L[pre].data=tl[pre].data;
29+
if(tl[pre].degree){
30+
L[pre].firstchild=next;
31+
for(int j=0;j<tl[pre].degree-1;j++){
32+
L[next].nextsibling=next+1;
33+
next++;
34+
}
35+
L[next].nextsibling=NOP;
36+
next++;
37+
}
38+
}
39+
}
40+
}
41+
void InitTreeWithList(Tree &T,List L[],int index);
42+
void PrintTree(Tree T,int x);
43+
void InitList(List L[]);
44+
int main(){
45+
Tree T1;
46+
Tree T2;
47+
//以下是一个预置的初始化数组
48+
//不使用第一项
49+
List L0[12]={{0,0,0},{2,NOP,11},{NOP,3,12},{6,4,13},{8,5,14},{NOP,NOP,15},{NOP,7,16},{9,NOP,17},{NOP,11,18},{NOP,10,19},{NOP,NOP,20},{NOP,NOP,21}};
50+
List L[MAXSIZE];
51+
DegNode tl[12]={{0,0},{4,11},{0,12},{2,13},{1,14},{0,15},{0,16},{2,17},{1,18},{0,19},{0,20},{0,21}};
52+
//InitList(L);
53+
InitTreeWithList(T1,L0,1);
54+
fun(L,tl);
55+
InitTreeWithList(T2,L,1);
56+
for(int i=1;i<12;i++)
57+
{
58+
cout<<"{"<<L[i].firstchild<<","<<L[i].nextsibling<<","<<L[i].data<<"},";
59+
}
60+
cout<<endl<<"T2:"<<endl;
61+
PrintTree(T2,1);
62+
cout<<endl<<"T1:"<<endl;
63+
PrintTree(T1,1);
64+
cout<<endl;
65+
return 0;
66+
}
67+
//不使用第一项
68+
void InitList(List L[]){
69+
int x=0,y=0,n=0;
70+
ELEMTYPE d;
71+
L[0]={0,0,0};
72+
cout<<"input numbers of tree node:"<<endl;
73+
cin>>n;
74+
for(int i=1;n<MAXSIZE&&i<=n;i++){
75+
cin>>x>>y>>d;
76+
L[i].firstchild=x;
77+
L[i].nextsibling=y;
78+
L[i].data=d;
79+
}
80+
}
81+
void InitTreeWithList(Tree &T,List L[],int index){
82+
if(index!=NOP){
83+
Tree nd=(Tree)malloc(sizeof(TreeNode));
84+
nd->data=L[index].data;
85+
T=nd;
86+
InitTreeWithList(T->firstchild,L,L[index].firstchild);
87+
InitTreeWithList(T->nextsibling,L,L[index].nextsibling);
88+
}else{
89+
T=NULL;
90+
}
91+
}
92+
// 1 for PreOrder,2 for InOrder,3 for PostOrder
93+
void PrintTree(Tree T,int x){
94+
if(T){
95+
if(x==1){
96+
cout<<T->data<<" ";
97+
}
98+
PrintTree(T->firstchild,x);
99+
if(x==2){
100+
cout<<T->data<<" ";
101+
}
102+
PrintTree(T->nextsibling,x);
103+
if(x==3){
104+
cout<<T->data<<" ";
105+
}
106+
free(T);
107+
}
108+
}

4-Tree

13.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)