实现二叉树先序、中序、后序,层次遍历代码(C语言)

         快期考了,大家数据结构复习的怎么样了呢,这里我复习了一下二叉树的先序、中序、后序,层次遍历,合上书自己挑战了下能不能Coding出来,结果发现自己不太记得各种遍历的函数名字了(悲),其他方面都还余裕。各位不妨挑战一下自己能不能码出来吧,下面的代码我只测试了一组例子,仅供参考,代码里的备注已经很详细了,这里我就不在做补充了。

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define MAXSIZE 32

//二叉树
typedef struct TreeNode{
    int data;
    struct TreeNode *Lchild;
    struct TreeNode *Rchild;
}Tree;

//循环队列
typedef struct {
    Tree* queue[MAXSIZE];
    int front;
    int rear;
}Queue;

//初始化树并给节点赋值
Tree *InitTree(int data){
    Tree *t;
    t = (Tree*)malloc(sizeof(Tree));
    t->data = data;
    t->Lchild = NULL;
    t->Rchild = NULL;
    return t;
}

//初始化循环队列
Queue *InitQueue(){
    Queue *q;
    q = (Queue*)malloc(sizeof(Queue));
    q->front = 0;
    q->rear = 0;
    return q;
}

//判断队列是否满
bool QueueStatusFull(Queue *q){
    if((q->front + 1) % MAXSIZE
  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
``` #include<stdio.h> #include<stdlib.h> //定义二叉树结构体 struct TreeNode { int data; struct TreeNode *left; struct TreeNode *right; }; //创建节点函数 struct TreeNode *createNode(int data) { struct TreeNode *newNode = (struct TreeNode *)malloc(sizeof(struct TreeNode)); newNode->data = data; newNode->left = NULL; newNode->right = NULL; return newNode; } //插入节点函数 struct TreeNode *insert(struct TreeNode *root, int data) { if (root == NULL) { return createNode(data); } else if (data <= root->data) { root->left = insert(root->left, data); } else { root->right = insert(root->right, data); } return root; } //递归先序遍历函数 void preOrder(struct TreeNode *root) { if (root == NULL) { return; } printf("%d ", root->data); preOrder(root->left); preOrder(root->right); } //递归中序遍历函数 void inOrder(struct TreeNode *root) { if (root == NULL) { return; } inOrder(root->left); printf("%d ", root->data); inOrder(root->right); } //递归后序遍历函数 void postOrder(struct TreeNode *root) { if (root == NULL) { return; } postOrder(root->left); postOrder(root->right); printf("%d ", root->data); } //非递归层次遍历函数 void levelOrder(struct TreeNode *root) { if (root == NULL) { return; } struct TreeNode *queue[100]; int front = 0; int rear = 0; queue[rear] = root; rear++; while (front <= rear) { struct TreeNode *current = queue[front]; front++; printf("%d ", current->data); if (current->left != NULL) { queue[rear] = current->left; rear++; } if (current->right != NULL) { queue[rear] = current->right; rear++; } } } int main() { struct TreeNode *root = NULL; root = insert(root, 5); root = insert(root, 3); root = insert(root, 7); root = insert(root, 2); root = insert(root, 4); root = insert(root, 6); root = insert(root, 8); printf("递归先序遍历:"); preOrder(root); printf("\n"); printf("递归中序遍历:"); inOrder(root); printf("\n"); printf("递归后序遍历:"); postOrder(root); printf("\n"); printf("非递归层次遍历:"); levelOrder(root); printf("\n"); return 0; } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值