二叉树的递归遍历

#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
struct node
{
    struct node * lchild;
    struct node * rchild;
    int data;
}node;
typedef struct node * BTREE;
/*BTREE CreateBT(int v,BTREE ltree,BTREE rtree)
{
    BTREE root;
    root=(BTREE)malloc(sizeof(node));
    root->data=v;
    root->lchild=ltree;
    root->rchild=rtree;
    return root;
}*/
void CreateBT(BTREE &T)//创建二叉树
{
    char ch;
    scanf("%c",&ch);
    if(ch=='#')
    {
        T=NULL;


    }
    else
    {
        T=(BTREE)malloc(sizeof(node));
        if(T==NULL)
        {
            return;
        }
        T->data=ch;
        CreateBT(T->lchild);
        CreateBT(T->rchild);
    }
    return;


}
void PreOrder(BTREE T)//先序遍历
{
    if(T==NULL)
    {
        return;
    }
    printf("%c",T->data);
    PreOrder(T->lchild);
    PreOrder(T->rchild);


}
void InOrder(BTREE T)//中序遍历
{
    if(T==NULL)
    {
        return;
    }


    InOrder(T->lchild);
    printf("%c",T->data);
    InOrder(T->rchild);


}
void PostOrder(BTREE T)//后序遍历
{
    if(T==NULL)
    {
        return;
    }
    PostOrder(T->lchild);
    PostOrder(T->rchild);
    printf("%c",T->data);


}


int main()
{
     BTREE A;
     printf("按先序序列输入结点序列,'#'代表空:");
     CreateBT(A);
     printf("先序遍历节点的结果为:");
     PreOrder(A);
     printf("中序遍历节点的结果为:");
     InOrder(A);
     printf("后序遍历节点的结果为:");
     PostOrder(A);


    return 0;
}




二叉树递归遍历是一种利用递归算法对二叉树进行遍历的方法,常见的遍历方式有前序遍历、中序遍历和后序遍历。 ### 原理 递归遍历的原理基于递归的思想,递归是指在函数的定义中使用函数自身的方法。对于二叉树的每个节点,它都有左右子树,递归遍历就是不断地对节点及其子树进行相同的操作。 ### 方法 - **前序遍历**:先访问根节点,然后递归遍历左子树,最后递归遍历右子树。 - **中序遍历**:先递归遍历左子树,然后访问根节点,最后递归遍历右子树。 - **后序遍历**:先递归遍历左子树,然后递归遍历右子树,最后访问根节点。 ### 实现 以下是使用C语言实现的二叉树递归遍历代码: ```c #include <stdio.h> #include <stdlib.h> // 定义二叉树节点结构 typedef struct TreeNode { char data; struct TreeNode *left; struct TreeNode *right; } Tree; // 递归前序遍历二叉树 void preOrderRec(Tree *root) { if (root != NULL) { printf(" %c - ", root->data); preOrderRec(root->left); preOrderRec(root->right); } } // 递归中序遍历二叉树 void inOrderRec(Tree *root) { if (root != NULL) { inOrderRec(root->left); printf(" %c - ", root->data); inOrderRec(root->right); } } // 递归后序遍历二叉树 void backOrderRec(Tree *root) { if (root != NULL) { backOrderRec(root->left); backOrderRec(root->right); printf(" %c - ", root->data); } } // 这里简单模拟创建一个二叉树,实际使用中需要根据具体需求实现 Tree* create_tree() { Tree *root = (Tree*)malloc(sizeof(Tree)); root->data = 'A'; root->left = (Tree*)malloc(sizeof(Tree)); root->left->data = 'B'; root->left->left = NULL; root->left->right = NULL; root->right = (Tree*)malloc(sizeof(Tree)); root->right->data = 'C'; root->right->left = NULL; root->right->right = NULL; return root; } // 测试递归打印二叉树代码 int main() { printf("starting ------ \n"); Tree *root = create_tree(); printf("递归前序遍历--- \n"); preOrderRec(root); printf("\n"); printf("递归中序遍历--- \n"); inOrderRec(root); printf("\n"); printf("递归后序遍历--- \n"); backOrderRec(root); printf("\n"); return 0; } ``` 上述代码中,`preOrderRec` 函数实现了前序遍历,`inOrderRec` 函数实现了中序遍历,`backOrderRec` 函数实现了后序遍历。在 `main` 函数中,创建了一个简单的二叉树,并分别调用三种遍历函数进行测试 [^1]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值