二叉树操作

最近在温习数据结构,把书中的代码写了一遍,下面是二叉树的基本操作,包括:

(1) 四种遍历二叉树的方法: 前序遍历、中序遍历、后序遍历和层序遍历,其中又包括了递归的和非递归;

(2) 两种创建二叉树的方法: 根据二叉树的前序和中序序列创建二叉树和根据二叉树的中序后序序列创建二叉树。


1. 二叉树的存储结构

/* headfiles/BiNode.h*/
#ifndef BINODE_H_INCLUDED
#define BINODE_H_INCLUDED

typedef struct BiNode {
    char data;
    struct BiNode *lchild, *rchild;
}BiNode, *BiList;

#endif // BINODE_H_INCLUDED

2. 二叉树的遍历

(1)前序、中序和后续的递归算法遍历

递归算法比较简单:

/*sharedSource/preOrder.c*/
void preOrder(BiNode *T) {
    if (T == NULL)
        return;
    printf("%c ", T->data);    //在此访问为前序遍历
    preOrder(T->lchild);
    //printf("%c ", T->data);  //在此访问为中序遍历
    preOrder(T->rchild);
    //printf("%c ", T->data);  //在此访问为后序遍历
}

3. 非递归算法

(1) 前序遍历非递归

/*sharedSource/BiNode.c*/
void preOrder2(BiNode *T)  //非递归算法前序遍历二叉树
{ //initialize the stack
    BiNode *stack[50];
    int top = 0;

    BiNode *p;
    p = T;
    while (p || top > 0) {
        while (p) {
            printf("%c ", p->data);
            stack[top++] = p;
            p = p->lchild;
        }
        if (top > 0) {
            p = stack[--top];
            p = p->rchild;
        }
    } //while
}

(2) 中序遍历非递归

/* sharedSource/inOrder2.c*/
void inOrder2(BiNode *T){  //非递归算法中序遍历二叉树
    //initialize the stack
    BiNode *stack[50];
    int top = 0;

    BiNode *p;
    p = T;
    while (p || top > 0) {
        while (p) {
            //printf("%c ", p->data);
            stack[top++] = p;
            p = p->lchild;
        }
        if (top > 0) {
            p = stack[--top];
            printf("%c ", p->data);
            p = p->rchild;
        }
    } //while
}

(3) 后序遍历非递归

/* sharedSource/postOrder2.c */
void postOrder2(BiNode *T) {  //非递归算法后序遍历二叉树
    //initialize the stack
    BiNode *stack[50];
    int top = 0;

    BiNode *p;
    p = T;
    while (p || top > 0) {
        while (p) {
            stack[top++] = p;
            if (p->lchild)
                p = p->lchild;
            else
                p = p->rchild;
        }
        if (top > 0) {
            p = stack[--top];
            printf("%c ", p->data);
            if (top > 0 && stack[top - 1]->rchild != p)//when returned from the right child,
                                                        //stack[top - 1]->rchild = p
                p = stack[top - 1]->rchild;
            else
                p = NULL;
        }
    } //while
}

(4) 层序遍历非递归

/* sharedSource/levelOrder.c*/
void levelOrder(BiNode *T) {  //非递归层序遍历二叉树
    //initial the queue
    int N = 50;
    int front = 0, rear = 0;
    BiNode *queue[N], *p;

    if (T == NULL)
        return;
    //printf("%c ", T->data);
    queue[rear] = T;
    rear = (rear + 1) % N;

    while (rear != front) {
        p = queue[front];
        front = (front + 1) % N;
        printf("%c ", p->data);
        if (p->lchild) {
            queue[rear] = p->lchild;
            rear = (rear + 1) % N;
        }
        if (p->rchild) {
            queue[rear] = p->rchild;
            rear = (rear + 1) % N;
        }
    }// while
}

4. 测试结果

#include <stdio.h>
//包含各个文件省略

int main(void)
{
    char *pre  = "ABCDEFG";
    char *in  =  "CBEDAFG";
    char *post = "CEDBGFA";
    BiNode *T = NULL;
    T = createBi_pre_in(pre, in, 0, 0, 7);
    //T = createBi_in_post(post, in, 6, 6, 7);

    printf("preOrder:   "); preOrder(T); printf("\n");

    printf("preOrder2:  "); preOrder2(T); printf("\n");

    printf("inOrder:    ");inOrder(T); printf("\n");

    printf("inOrder2:   "); inOrder2(T); printf("\n");

    printf("postOrder:  "); postOrder(T); printf("\n");

    printf("postOrder2: "); postOrder2(T); printf("\n");

    printf("levelOrder: "); levelOrder(T); printf("\n");

    return 0;
}

输出:




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值