Skip to content

Commit 4e6f377

Browse files
committed
第四章 Part1
1 parent 6e91775 commit 4e6f377

40 files changed

+2064
-0
lines changed

3-3-4

0 Bytes
Binary file not shown.

3-3-4.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ int main(){
7373
DeQueue(q2,x);
7474
cout<<x<<" ";
7575
n++;
76+
}else if(!QueueEmpty(q1)){
77+
DeQueue(q1,x);
78+
cout<<x<<" ";
79+
n++;
7680
}
7781
}
7882
cout<<endl;

4-2-5-BTree-Seq

13 KB
Binary file not shown.

4-2-5-BTree-Seq.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include<iostream>
2+
#define ELEMTYPE int
3+
#define MAXSIZE 50
4+
using namespace std;
5+
typedef struct{
6+
ELEMTYPE treelsit[MAXSIZE];
7+
int nums;
8+
}BiTree;
9+
int fun(BiTree* T,int i,int j){
10+
if(i<T->nums&&j<T->nums){
11+
while(i!=j){
12+
if(i<j)j/=2;
13+
else i/=2;
14+
}
15+
return T->treelsit[i];
16+
}
17+
cout<<"error"<<endl;
18+
return -1;
19+
}
20+
int main(){
21+
BiTree *T=(BiTree*)malloc(sizeof(BiTree));
22+
int x;
23+
cin>>x;
24+
T->nums=x;
25+
//顺序存储的二叉树初始化
26+
for(int i=0;i<T->nums;i++){
27+
cin>>x;
28+
T->treelsit[i]=x;
29+
}
30+
int m,n;
31+
cout<<"input i,j:"<<endl;
32+
cin>>m>>n;
33+
cout<<"result:"<<fun(T,m,n)<<endl;;
34+
for(int i=0;i<T->nums;i++){
35+
cout<<T->treelsit[i]<<" ";
36+
}
37+
free(T);
38+
cout<<endl;
39+
return 0;
40+
}

4-3-10

13.2 KB
Binary file not shown.

4-3-10.cpp

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

4-3-11

13.3 KB
Binary file not shown.

4-3-11.cpp

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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 FreeTree(BiTree &T){
17+
if(T){
18+
FreeTree(T->lchild);
19+
FreeTree(T->rchild);
20+
free(T);
21+
}
22+
}
23+
void fun_start(BiTree &T);
24+
void fun(BiTree &T,int k){
25+
if(T){
26+
if(T->data==k){
27+
FreeTree(T);
28+
T=NULL;
29+
}
30+
else{
31+
fun(T->lchild,k);
32+
fun(T->rchild,k);
33+
}
34+
}
35+
}
36+
37+
38+
39+
40+
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].lchild=x;
52+
L[i].rchild=y;
53+
L[i].data=d;
54+
}
55+
}
56+
void InitTreeWithList(BiTree &T,List L[],int index){
57+
if(index!=NOP){
58+
BiTree nd=(BiTree)malloc(sizeof(BiNode));
59+
nd->data=L[index].data;
60+
T=nd;
61+
InitTreeWithList(T->lchild,L,L[index].lchild);
62+
InitTreeWithList(T->rchild,L,L[index].rchild);
63+
}else{
64+
T=NULL;
65+
}
66+
}
67+
// 1 for PreOrder,2 for InOrder,3 for PostOrder
68+
void PrintTree(BiTree T,int x,int tag){
69+
if(T){
70+
if(x==1){
71+
cout<<T->data<<" ";
72+
}
73+
PrintTree(T->lchild,x,tag);
74+
if(x==2){
75+
cout<<T->data<<" ";
76+
}
77+
PrintTree(T->rchild,x,tag);
78+
if(x==3){
79+
cout<<T->data<<" ";
80+
}
81+
if(tag)free(T);
82+
}
83+
}
84+
void fun_start(BiTree &T){
85+
//以下是一个预置的初始化数组
86+
//不使用第一项
87+
List L0[10]={{0,0,0},{2,3,11},{4,5,12},{6,NOP,13},{NOP,7,14},{NOP,8,15},{NOP,NOP,16},{9,NOP,17},{NOP,NOP,18},{NOP,NOP,19}};
88+
List L[MAXSIZE];
89+
//InitList(L);
90+
InitTreeWithList(T,L0,1);
91+
PrintTree(T,1,0);
92+
int k=0;
93+
cin>>k;
94+
fun(T,k);
95+
PrintTree(T,1,1);
96+
}
97+
98+
99+
100+
int main(){
101+
BiTree T;
102+
fun_start(T);
103+
cout<<endl;
104+
return 0;
105+
}

4-3-12

13.2 KB
Binary file not shown.

4-3-12.cpp

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
17+
void fun_start(BiTree &T);
18+
void fun(BiTree &T,BiTree (&stk)[MAXSIZE],int &top,int k){
19+
BiTree p,last=NULL,sn;
20+
for(p=T;p;p=p->lchild){
21+
stk[top++]=p;
22+
}
23+
while(top){
24+
sn=stk[top-1];
25+
if(!sn->rchild||sn->rchild==last){
26+
p=stk[--top];
27+
if(p->data==k)break;
28+
last=p;
29+
}else{
30+
p=sn->rchild;
31+
for(;p;p=p->lchild){
32+
stk[top++]=p;
33+
}
34+
}
35+
}
36+
}
37+
38+
39+
40+
41+
42+
43+
//不使用第一项
44+
void InitList(List L[]){
45+
int x=0,y=0,n=0;
46+
ELEMTYPE d;
47+
L[0]={0,0,0};
48+
cout<<"input numbers of tree node:"<<endl;
49+
cin>>n;
50+
for(int i=1;n<MAXSIZE&&i<=n;i++){
51+
cin>>x>>y>>d;
52+
L[i].lchild=x;
53+
L[i].rchild=y;
54+
L[i].data=d;
55+
}
56+
}
57+
void InitTreeWithList(BiTree &T,List L[],int index){
58+
if(index!=NOP){
59+
BiTree nd=(BiTree)malloc(sizeof(BiNode));
60+
nd->data=L[index].data;
61+
T=nd;
62+
InitTreeWithList(T->lchild,L,L[index].lchild);
63+
InitTreeWithList(T->rchild,L,L[index].rchild);
64+
}else{
65+
T=NULL;
66+
}
67+
}
68+
// 1 for PreOrder,2 for InOrder,3 for PostOrder
69+
void PrintTree(BiTree T,int x){
70+
if(T){
71+
if(x==1){
72+
cout<<T->data<<" ";
73+
}
74+
PrintTree(T->lchild,x);
75+
if(x==2){
76+
cout<<T->data<<" ";
77+
}
78+
PrintTree(T->rchild,x);
79+
if(x==3){
80+
cout<<T->data<<" ";
81+
}
82+
free(T);
83+
}
84+
}
85+
void fun_start(BiTree &T){
86+
//以下是一个预置的初始化数组
87+
//不使用第一项
88+
List L0[10]={{0,0,0},{2,3,11},{4,5,12},{6,NOP,13},{NOP,7,14},{NOP,8,15},{NOP,NOP,16},{9,NOP,17},{NOP,NOP,18},{NOP,NOP,19}};
89+
// List L[MAXSIZE];
90+
// InitList(L);
91+
InitTreeWithList(T,L0,1);
92+
cout<<"fun:"<<endl;
93+
ELEMTYPE k;
94+
cin>>k;
95+
BiTree stk[MAXSIZE];
96+
int top=0;
97+
fun(T,stk,top,k);
98+
while(--top>=0)cout<<stk[top]->data<<" ";
99+
cout<<endl;
100+
cout<<"BiTree:"<<endl;
101+
PrintTree(T,3);
102+
}
103+
104+
105+
106+
int main(){
107+
BiTree T;
108+
fun_start(T);
109+
cout<<endl;
110+
return 0;
111+
}

4-3-13

13.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)