二叉树的前中后序、层序遍历代码(递归与非递归)

#include <bits/stdc++.h>
using namespace std;

typedef struct BTNode {  //链式存储结构
    char data;
    struct BTNode *lchild;
    struct BTNode *rchild;
};

void preorder(BTNode *p) {  //递归前序遍历
    if(*p!=NULL) {
        visit(p);
        if(p->lchild!=NULL)
            preorder(p->lchild);
        if(p->rchild!=NULL)
            preorder(p->rchild);
    }
}

void inorder(BTNode *p) {  //递归中序遍历
    if(*p!=NULL) {
        if(p->lchild!=NULL)
            inorder(p->lchild);
        visit(p);
        if(p->rchild!=NULL)
            inorder(p->rchild);
    }
}

void postorder(BTNode *p) {  //递归后序遍历
    if(*p!=NULL) {
        if(p->lchild!=NULL)
            postorder(p->lchild);
        if(p->rchild!=NULL)
            postorder(p->rchild);
        visit(p);
    }
}

void level(BTNode *p) {  //层序遍历
    if(p!=NULL) {
        int front=0,rear=0;
        BTNode *que[MAXN];
        BTNode *q;
        rear=(rear+1)%maxsize;
        que[rear]=p;
        while(rear!=front) {
            front =(front+1)%maxsize;
            q=que[front];
            visit(q);
            if(q->lchild!=NULL) {
                rear=(rear+1)%maxsize;
                que[rear]=p->lchild;
            }
            if(q->rchild!=NULL) {
                rear=(rear+1)%maxsize;
                que[rear]=p->rchild;
            }
        }
    }
}

void preorder(BTNode *bt) {  //非递归前序遍历
    if(bt!=NULL) {
        BTNode *stack[maxsize];
        BTNode *q;
        int top=-1;
        stack[++top]=bt;
        while(top!=-1) {
            q=stack[top];
            visit[q];
            top--;
            if(q->rchild!=NULL) {
                stack[++top]=bt->rchild;
            }
            if(q->lchild!=NULL) {
                stack[++top]=bt->lchild;
            }
        }
    }
}

void inorder(BTNode *bt) {  //非递归中序遍历
    if(bt!=NULL) {
        BTNode *stack[maxsize];
        BTNode *p;
        p=bt;
        int top=-1;
        while(top!=-1||p!=NULL) {
            while(p!=NULL) {
                stack[++top]=p;
                p=p->lchild;
            }
            if(top!=-1) {
                p=stack[top--];
                visit(p);
                p=p->rchild;
            }
        }
    }
}

void postorder(BTNode *bt) {  //非递归后序遍历
    if(bt!=NULL) {
        BTNode *stack1[maxsize],*stack2[maxsize];
        int top1=-1,top2=-1;
        BTNode *p;
        p=bt;
        stack1[++top1]=bt;
        while(top1!=-1) {
            p=stack1[top1--];
            stack2[++top2]=p;
            if(p->lchild!=NULL) {
                stack1[++top1]=p->lchild;
            }
            if(p->rchild!=NULL) {
                stack1[++top1]=p->rchild;
            }
        }
        while(top2!=-1) {
            p=stack2[top2--];
            visit(p);
        }
    }
}

int main() 
{

    return 0;
}

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
当然,二叉树的遍历有三种主要方式:先序遍历(根-左-右)、序遍历(左-根-右)和后序遍历(左-右-根)。非递归层次遍历(也叫广度优先遍历,从上到下、从左到右)通常使用队列来辅助实现。 这里分别给出这些遍历的非递归算法代码: 1. 层序遍历(广度优先遍历): ```c #include <stdio.h> #include <stdlib.h> #include <queue> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; void levelOrder(struct TreeNode* root) { if (root == NULL) return; // 使用队列存储每一层的节点 queue<struct TreeNode*> q; q.push(root); while (!q.empty()) { int size = q.size(); for (int i = 0; i < size; i++) { struct TreeNode* node = q.front(); q.pop(); printf("%d ", node->val); // 打印当节点值 if (node->left != NULL) q.push(node->left); if (node->right != NULL) q.push(node->right); } printf("\n"); // 换行表示新的一层 } } ``` 2. 先序遍历(递归非递归两种方式,这里是非递归版本,使用栈): ```c void preorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; s.push(root); while (!s.empty()) { struct TreeNode* node = s.top(); s.pop(); printf("%d ", node->val); // 打印当节点值 if (node->right != NULL) s.push(node->right); if (node->left != NULL) s.push(node->left); } } ``` 3. 序遍历(非递归,同样使用栈): ```c void inorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s; struct TreeNode* curr = root; while (curr != NULL || !s.empty()) { while (curr != NULL) { s.push(curr); curr = curr->left; } curr = s.top(); s.pop(); printf("%d ", curr->val); // 打印当节点值 curr = curr->right; } } ``` 4. 后序遍历(非递归,使用两个栈): ```c void postorderNonRecursive(struct TreeNode* root) { if (root == NULL) return; stack<struct TreeNode*> s1, s2; s1.push(root); while (!s1.empty()) { struct TreeNode* node = s1.top(); s1.pop(); s2.push(node); if (node->left != NULL) s1.push(node->left); if (node->right != NULL) s1.push(node->right); } while (!s2.empty()) { struct TreeNode* node = s2.top(); s2.pop(); printf("%d ", node->val); // 打印当节点值 } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

烟敛寒林o

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值