一种二叉树层序遍历实现
2017-12-21 16:34
375 查看
今天终于有时间尝试自己实现二叉树层序遍历,因为层序遍历需要借用一个队列,所以不像前序遍历、中序遍历和后序遍历(直接递归即可)那样容易实现,但是经过一番折腾后,居然成功了,呵呵,为防止自己遗忘,故写下此博客。
编程思路如下:
1、分别建立一颗二叉树和一个队列
2、先访问根节点
3、再判断是否有左右儿子节点,若存在则将其入队(先左儿子再右儿子)
4、出队一次
5、以出队节点指针为新根节点指针,递归调用2-4,直至新根节点指针为NULL
编程思路如下:
1、分别建立一颗二叉树和一个队列
2、先访问根节点
3、再判断是否有左右儿子节点,若存在则将其入队(先左儿子再右儿子)
4、出队一次
5、以出队节点指针为新根节点指针,递归调用2-4,直至新根节点指针为NULL
#include <stdio.h> #include <stdlib.h> typedef struct BTNode *BTptr; typedef struct BTNode { char data; struct BTNode *lchild; struct BTNode *rchild; }BTree; struct WorkQueue { struct BTNode *data; struct WorkQueue *next; }; //-------------------------------------- //globe var //-------------------------------------- struct WorkQueue *head,*tail; struct WorkQueue *wq = NULL; //----------------------------------------------------- //workqueue implement //----------------------------------------------------- static void create_workqueue(struct WorkQueue **q) { if(NULL == *q) { *q = malloc(sizeof(struct WorkQueue)); if(NULL == *q) { printf("Error: out of memory!!\n"); return; } head = tail = NULL; (*q)->next = NULL; } return; } static int queue(struct WorkQueue *q,struct BTNode *c) { struct WorkQueue *tmp = NULL; if(head == NULL) { tmp = malloc(sizeof(struct WorkQueue)); if(tmp == NULL) { printf("Error: out of memory!!\n"); return -1; } head = tail = tmp; tmp->next = NULL; tmp->data = c; } else { tmp = malloc(sizeof(struct WorkQueue)); if(tmp == NULL) { printf("Error: out of memory!!\n"); return -1; } tail->next = tmp; tail = tmp; tmp->data = c; tmp->next = NULL; } return 0; } static struct BTNode *dequeue(struct WorkQueue *q) { struct BTNode *c; if(head == NULL) { return NULL; } if(head == tail) { c = head->data; head = tail = NULL; } else { c = head->data; head = head->next; } return c; } //----------------------------------------------------- //Binary tree implement //----------------------------------------------------- void create_tree(BTptr *T) { char c; scanf("%c",&c); if('#' == c) { *T = NULL; return; } *T = (BTptr)malloc(sizeof(BTree)); if(*T == NULL) { printf("Error: no memory to alloc in system!!\n"); return; } (*T)->data = c; create_tree(&((*T)->lchild)); create_tree(&((*T)->rchild)); } void print_preorder(BTptr T) { if(T == NULL) return; printf("\t%c\t",T->data); print_preorder(T->lchild); print_preorder(T->rchild); } void print_inorder(BTptr T) { if(T == NULL) return; print_inorder(T->lchild); printf("\t%c\t",T->data); print_inorder(T->rchild); } void print_postorder(BTptr T) { if(T == NULL) return; print_postorder(T->lchild); print_postorder(T->rchild); printf("\t%c\t",T->data); } int print_layerorder(BTptr T) { if(T == NULL) return 0; printf("\t%c\t",T->data); if(T->lchild != NULL) queue(wq,T->lchild); if(T->rchild != NULL) queue(wq,T->rchild); return print_layerorder(dequeue(wq)); } int main(void) { BTptr T; create_tree(&T); printf("当前树的前序遍历为:"); print_preorder(T); printf("\n"); //printf("当前树的中序遍历为:"); //print_inorder(T); //printf("\n"); //printf("当前树的后序遍历为:"); //print_postorder(T); //printf("\n"); create_workqueue(&wq); printf("当前树的层序遍历为:"); print_layerorder(T); printf("\n"); return 0; }
相关文章推荐
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现
- 建立二叉树,实现二叉树的层序遍历
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
- 数据结构面试题/实现一颗二叉树的层序遍历
- Java实现二叉树的前序、中序、后序、层序遍历(递归方法)
- 二叉树的建立及层序遍历的实现
- 二叉树的前/中/后/层序遍历(前/中/后序递归实现,层序队列实现)
- 二叉树的层序遍历算法实现
- JAVA下实现二叉树的先序、中序、后序、层序遍历(递归和循环)
- 二叉树的遍历:先序中序后序遍历的递归与非递归实现及层序遍历
- 【LeetCode-面试算法经典-Java实现】【102-Binary Tree Level Order Traversal(二叉树层序遍历)】
- [置顶] 树:二叉树的层序遍历算法(超简洁实现及详细分析)
- java实现的二叉树(前序、中序、后序)递归和非递归遍历,包含层序遍历
- Java实现二叉树的递归构建及先序、中序、后序、层序遍历(递归和循环)
- 二叉树的遍历:前序,中序,后序,层序--包括递归和非递归实现(转)
- Java实现二叉树带行号的层序遍历
- 用java实现二叉树相关操作(前序建树,前中后递归非递归遍历,层序遍历)
- 【LeetCode-面试算法经典-Java实现】【107-Binary Tree Level Order Traversal II(二叉树层序遍历II)】
- C语言非递归实现二叉树的先序、中序、后序、层序遍历
- 实现二叉树的层序遍历,说说Java中的队列结构(实现一个Java的队列)