使用链表实现二叉树的构造并完成中序遍历。
首先在头文件中定义结点结构体如下:
typedef struct node {
int x;
struct node* left;
struct node* right;
} BN;
其次要实现两个函数,函数buildTree通过读入一串整数以层次优先从左到右的方式构建一个二叉树,函数outputTree输出此树的中序遍历。两个函数的原型如下:
void buildTree(BN** rootptr);
void outputTree(BN* root);
输入:N个正整数,1<=N<=30,以空格间隔,以-1结束。注意-1不包含在此树的结构中,只是作为输入结束的符号。
输出:一行按照中序遍历排列的正整数,以空格间隔,最后一个输出的元素后仍然有空格,且没有换行
注意:main函数已经给出,并且在其中给出了树的根节点的指针以及整棵树free的过程,buildTree函数内所有的结点应以malloc形式生成,如果一个结点没有左儿子或者右儿子,则相应的指针应该等于NULL。
Sample:
input:
1 2 3 4 5 6 7 8 9 -1
output:
8 4 9 2 5 1 6 3 7
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXI 50
typedef struct node {
int x;
struct node* left;
struct node* right;
}BN;
void buildTree(BN** rootptr) {//按层次有限来建立二叉树
int num, tot = 1, head = 1;
BN* a[50];//用队列,保存一系列内存地址
scanf("%d", &num);
BN* p = (BN*)malloc(sizeof(BN));
*rootptr = p;
(*rootptr) -> x = num;
a[1] = *rootptr;
a[1] -> left = NULL;
a[1] -> right = NULL;
while (num != -1) {
scanf("%d", &num);
if (num != -1) {
tot++;
BN* left = (BN*)malloc(sizeof(BN));
left -> x = num;
left -> left = NULL;
left -> right = NULL;
a[tot] = left;
a[head] -> left = left;
scanf("%d", &num);
}
if (num != -1) {
tot++;
BN* right = (BN*)malloc(sizeof(BN));
right -> x = num;
right -> right = NULL;
right -> left = NULL;
a[tot] = right;
a[head] -> right = right;
}
head++;
}
}
void outputTree(BN* root) {
if (root -> left != NULL) {
outputTree(root -> left);
}
printf("%d ", root -> x);
if (root -> right != NULL) {
outputTree(root -> right);
}
}
int main() {
BN* root;
BN* que[MAXI]; /*used for free*/
int head = 0; /*head of que*/
int tail = 1; /*tail of que*/
buildTree(&root);
outputTree(root);
/*the free procedure*/
que[0] = root;
while (head != tail) {
if (que[head]->left != NULL) {
que[tail] = que[head]->left;
tail = (tail + 1) % MAXI;
}
if (que[head]->right != NULL) {
que[tail] = que[head]->right;
tail = (tail + 1) % MAXI;
}
free(que[head]);
head = (head + 1) % MAXI;
}
return 0;
}
from 雨蓓。
成功!
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXI 50
typedef struct node {
int x;
struct node* left;
struct node* right;
} BN;
void buildTree(BN** rootptr){
int n;
BN* a[50];
int head = 1;
scanf("%d", &n);
BN* p = (BN*)malloc(sizeof(BN));
*rootptr = p;
(*rootptr) -> x = n;
a[0] = *rootptr;
a[0] -> left = NULL;
a[0] -> right = NULL;
scanf("%d", &n);
while (n != -1) {
a[head] = (BN*)malloc(sizeof(BN));
a[head]->x = n;
a[head]->right = NULL;
a[head]->left = NULL;
scanf("%d", &n);
head++;
}
int i;
for (i = 0; i < head; i++) {
if ((i + 1) * 2 - 1 < head) {
a[i]->left = a[(i + 1) * 2 - 1];
} else {
a[i]->left = NULL;
}
if ((i + 1) * 2 < head) {
a[i]->right = a[(i + 1) * 2];
} else {
a[i]->right = NULL;
}
}
}
void outputTree(BN* root) {
if (root) {
outputTree(root->left);
printf("%d ", root->x);
outputTree(root->right);
}
}
int main() {
BN* root;
BN* que[MAXI]; /*used for free*/
int head = 0; /*head of que*/
int tail = 1; /*tail of que*/
buildTree(&root);
outputTree(root);
/*the free procedure*/
que[0] = root;
while (head != tail) {
if (que[head]->left != NULL) {
que[tail] = que[head]->left;
tail = (tail + 1) % MAXI;
}
if (que[head]->right != NULL) {
que[tail] = que[head]->right;
tail = (tail + 1) % MAXI;
}
free(que[head]);
head = (head + 1) % MAXI;
}
return 0;
}
有未知bug待查。
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAXI 50
typedef struct node {
int x;
struct node* left;
struct node* right;
} BN;
void buildTree(BN** rootptr){
int n;
BN *p;
int head = 0;
scanf("%d", &n);
while (n != -1) {
p = (BN*)malloc(sizeof(BN));
p->x = n;
p->right = NULL;
p->left = NULL;
rootptr[head++] = p;
scanf("%d", &n);
}
int i;
for (i = 0; i < head; i++) {
if ((i + 1) * 2 - 1 < head) {
rootptr[i]->left = rootptr[(i + 1) * 2 - 1];
} else {
rootptr[i]->left = NULL;
}
if ((i + 1) * 2 < head) {
rootptr[i]->right = rootptr[(i + 1) * 2];
} else {
rootptr[i]->right = NULL;
}
}
}
void outputTree(BN* root) {
if (root) {
outputTree(root->left);
printf("%d ", root->x);
outputTree(root->right);
}
}
int main() {
BN* root;
BN* que[MAXI]; /*used for free*/
int head = 0; /*head of que*/
int tail = 1; /*tail of que*/
buildTree(&root);
outputTree(root);
/*the free procedure*/
que[0] = root;
while (head != tail) {
if (que[head]->left != NULL) {
que[tail] = que[head]->left;
tail = (tail + 1) % MAXI;
}
if (que[head]->right != NULL) {
que[tail] = que[head]->right;
tail = (tail + 1) % MAXI;
}
free(que[head]);
head = (head + 1) % MAXI;
}
return 0;
}