二叉树操作

来源:互联网 发布:toastr.js 编辑:程序博客网 时间:2024/05/01 08:27

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

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

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


1. 二叉树的存储结构

/* headfiles/BiNode.h*/#ifndef BINODE_H_INCLUDED#define BINODE_H_INCLUDEDtypedef 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;}

输出:




原创粉丝点击