Binary tree traversal recursive implementation (preorder, inorder, postorder and level traversal)

Defined by the apparent binary tree, a binary tree root consists of three parts, the left and right subtree subtree composition. Therefore, as long as it traversed the three sections can be achieved through the entire binary tree. In terms of D, L, R represent the root node traversal, the left subtree, right subtree, the binary tree traversal recursion can be about three ways:

Preorder (DLR)

Preorder recursive process is

(1) Access root node
(2) preorder left subtree of the root
(3) preorder right subtree root

Example:

Code:

void PreOrder(BiTree bt)
{
    if(bt ==NULL)return;    //递归的结束条件----某结点为空时
    printf("bt->data);      //这里用printf data表示访问结点的数据域
    PreOrder(bt->lchild);   //递归遍历左孩子
    PreOrder(bt->rclild);   //递归遍历右孩子
}

Inorder traversal (the LDR )

Preorder traversal left subtree root node (1)
(2) access to the root node
(3) in order to traverse the right subtree of the root node

Example:

Code:

void PreOrder(BiTree bt)
{
    if(bt ==NULL)return;    //递归的结束条件----某结点为空时
    PreOrder(bt->lchild);   //递归遍历左孩子
    printf("bt->data);      //这里用printf data表示访问结点的数据域
    PreOrder(bt->rclild);   //递归遍历右孩子
}

Postorder (LRD)

(1) after traversing Binary Tree left subtree
(2) traversing Binary Tree right subtree
(3) root access.

Example:

Code:

void PreOrder(BiTree bt)
{
    if(bt ==NULL)return;    //递归的结束条件----某结点为空时
    PreOrder(bt->lchild);   //递归遍历左孩子
    PreOrder(bt->rclild);   //递归遍历右孩子
    printf("bt->data);      //这里用printf data表示访问结点的数据域
}     

Traverse the level

(1) the root node of the stack
(2) a left subtree of the root node of the stack, the root, the right subtree sequentially stack
the left subtree (3) the root node from the stack, the left subtree junction left subtree point, have the right subtree stack
(4) .......

Example:

Code:

//层次遍历二叉树
void LevelOrder(BiTree T)
{
    BiTree Queue[MAX],b;        //用一维数组表示队列,front和rear表示队首和队尾的指针
    int front,rear;
    front=rear=0;
    if(T)
    //若树为空    
    {
        Queue[rear++]=T;    //根节点入队列
        while(front!=rear)  //当队列非空
        {
            b=Queue[front++];     //队首元素出队列,并访问这个节点 
            printf("%2c",b->data);
            if(b->lchild!=NULL) Queue[rear++]=b->lchild ;  //若左子树非空,则入队列
            if(b->rchild!=NULL) Queue[rear++]=b->rchild ;  //若右子树非空,则入队列 
        } 
    }   
} 

The final code:

#include<stdio.h>
#include<stdlib.h>
#define MAX 20
typedef char TElemType;
typedef int Status;
typedef struct BiTNode
{
    TElemType data;   
    struct BiTNode *lchild,*rchild; //左右孩子的指针 
} BiTNode,*BiTree;
//先序创建二叉树
void CreateBiTree(BiTree *T)
{
    char ch;
    ch=getchar();
    if(ch=='#')(*T)=NULL;   //#代表空指针
    else
    {
        (*T)=(BiTree)malloc(sizeof(BiTNode));  //申请节点
        (*T)->data=ch;      //生成跟节点
        CreateBiTree(&(*T)->lchild);
        CreateBiTree(&(*T)->rchild);
    }   
} 
//先序输出二叉树
void PreOrder(BiTree T)
{
    if(T)
    {
        printf("%2c",T->data);   //访问根节点,此处为输出根节点的数据值   
        PreOrder(T->lchild);     //先序遍历左子树
        PreOrder(T->rchild);     //先序遍历右子树
    } 
} 
//中序输出二叉树
void InOrder(BiTree T)
{
if(T)
    {
        PreOrder(T->lchild);
        printf("%2c",T->data);
        PreOrder(T->rchild);
    }   
} 
//后序输出二叉树
void PostOrder(BiTree T)
{
if(T)
    {
        PreOrder(T->lchild);
        PreOrder(T->rchild);
        printf("%2c",T->data);
    }   
} 
//层次遍历二叉树
void LevelOrder(BiTree T)
{
    BiTree Queue[MAX],b;        //用一维数组表示队列,front和rear表示队首和队尾的指针
    int front,rear;
    front=rear=0;
    if(T)
    //若树为空    
    {
        Queue[rear++]=T;    //根节点入队列
        while(front!=rear)  //当队列非空
        {
            b=Queue[front++];     //队首元素出队列,并访问这个节点 
            printf("%2c",b->data);
            if(b->lchild!=NULL) Queue[rear++]=b->lchild ;  //若左子树非空,则入队列
            if(b->rchild!=NULL) Queue[rear++]=b->rchild ;  //若右子树非空,则入队列 
        } 
    }   
} 
//求树的深度
int depth(BiTree T)
{
    int dep1,dep2;
    if(T==NULL) return 0;
    else
    {
        dep1=depth(T->lchild);
        dep2=depth(T->rchild);
        return dep1>dep2?dep1+1:dep2+1; 
    }   
} 
int main()
{
    BiTree T=NULL;
    printf("\n 创建一棵二叉树: \n");
    CreateBiTree(&T);  //创建二叉树
    printf("\n先序遍历的结果为:\n");
    PreOrder(T);  //先序遍历
    printf("\n中序遍历的结果为:\n");
    InOrder(T);  //中序遍历 
    printf("\n 后序遍历的结果为: \n");
    PostOrder(T); 
    printf("\n 层次遍历的结果为: \n");
    LevelOrder(T); //层次遍历
    printf("\n 树的深度为:%d\n",depth(T)); 
}

Example result:

Ah, you do not intend to white prostitute it, thumbs up it, concerned about it?

Guess you like

Origin www.cnblogs.com/kangna/p/11846154.html