(1)建立二叉树,节点的结构体为: typedef struct { int num; /学号 char name[10]; /姓名 } student; (2)完成前、中、后二叉树非递归遍历程序; (3)从上至下、从左至右层次遍历程序;从上至下、从右至左层次遍历程序;
时间: 2023-07-16 09:11:31 浏览: 90
以下是基于C语言的实现,包括节点结构体定义、二叉树的创建、前、中、后序非递归遍历以及从上至下、从左至右层次遍历程序和从上至下、从右至左层次遍历程序。
```
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int num; // 学号
char name[10]; // 姓名
struct node* left; // 左子树
struct node* right; // 右子树
} TreeNode, *TreeNodePtr;
// 创建二叉树
TreeNodePtr createBinaryTree() {
TreeNodePtr root = NULL, current = NULL;
int num;
char name[10];
printf("请输入根节点的学号和姓名:");
scanf("%d%s", &num, name);
if (num == 0) {
return root;
}
root = (TreeNodePtr)malloc(sizeof(TreeNode));
root->num = num;
strcpy(root->name, name);
root->left = NULL;
root->right = NULL;
current = root;
while (1) {
printf("请输入要插入节点的学号和姓名:");
scanf("%d%s", &num, name);
if (num == 0) {
break;
}
TreeNodePtr node = (TreeNodePtr)malloc(sizeof(TreeNode));
node->num = num;
strcpy(node->name, name);
node->left = NULL;
node->right = NULL;
while (1) {
if (num < current->num) {
if (current->left == NULL) {
current->left = node;
break;
}
else {
current = current->left;
}
}
else {
if (current->right == NULL) {
current->right = node;
break;
}
else {
current = current->right;
}
}
}
current = root;
}
return root;
}
// 前序非递归遍历
void preOrder(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr stack[100];
int top = -1;
stack[++top] = root;
while (top >= 0) {
TreeNodePtr node = stack[top--];
printf("%d %s ", node->num, node->name);
if (node->right != NULL) {
stack[++top] = node->right;
}
if (node->left != NULL) {
stack[++top] = node->left;
}
}
}
// 中序非递归遍历
void inOrder(TreeNodePtr root) {
TreeNodePtr stack[100];
int top = -1;
TreeNodePtr node = root;
while (top >= 0 || node != NULL) {
while (node != NULL) {
stack[++top] = node;
node = node->left;
}
if (top >= 0) {
node = stack[top--];
printf("%d %s ", node->num, node->name);
node = node->right;
}
}
}
// 后序非递归遍历
void postOrder(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr stack1[100], stack2[100];
int top1 = -1, top2 = -1;
stack1[++top1] = root;
while (top1 >= 0) {
TreeNodePtr node = stack1[top1--];
stack2[++top2] = node;
if (node->left != NULL) {
stack1[++top1] = node->left;
}
if (node->right != NULL) {
stack1[++top1] = node->right;
}
}
while (top2 >= 0) {
TreeNodePtr node = stack2[top2--];
printf("%d %s ", node->num, node->name);
}
}
// 层次遍历
void levelOrder(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr queue[100];
int front = -1, rear = -1;
queue[++rear] = root;
while (front != rear) {
TreeNodePtr node = queue[++front];
printf("%d %s ", node->num, node->name);
if (node->left != NULL) {
queue[++rear] = node->left;
}
if (node->right != NULL) {
queue[++rear] = node->right;
}
}
}
// 从上至下、从左至右层次遍历
void levelOrderLeftToRight(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr queue[100];
int front = -1, rear = -1;
queue[++rear] = root;
while (front != rear) {
int count = rear - front;
for (int i = 0; i < count; i++) {
TreeNodePtr node = queue[++front];
printf("%d %s ", node->num, node->name);
if (node->left != NULL) {
queue[++rear] = node->left;
}
if (node->right != NULL) {
queue[++rear] = node->right;
}
}
printf("\n");
}
}
// 从上至下、从右至左层次遍历
void levelOrderRightToLeft(TreeNodePtr root) {
if (root == NULL) {
return;
}
TreeNodePtr queue[100];
int front = -1, rear = -1;
queue[++rear] = root;
while (front != rear) {
int count = rear - front;
for (int i = 0; i < count; i++) {
TreeNodePtr node = queue[++front];
if (node->left != NULL) {
queue[++rear] = node->left;
}
if (node->right != NULL) {
queue[++rear] = node->right;
}
}
for (int i = 0; i < count; i++) {
TreeNodePtr node = queue[front + i + 1];
printf("%d %s ", node->num, node->name);
}
printf("\n");
}
}
int main() {
TreeNodePtr root = createBinaryTree();
printf("前序遍历结果:");
preOrder(root);
printf("\n中序遍历结果:");
inOrder(root);
printf("\n后序遍历结果:");
postOrder(root);
printf("\n层次遍历结果:");
levelOrder(root);
printf("\n从上至下、从左至右层次遍历结果:\n");
levelOrderLeftToRight(root);
printf("从上至下、从右至左层次遍历结果:\n");
levelOrderRightToLeft(root);
return 0;
}
```
阅读全文