概述
http://blog.csdn.net/u010366748/article/details/50765512
参考书籍:数据结构(C语言版)严蔚敏吴伟民编著清华大学出版社
1.动态二叉链表存储即遍历的实现
1.1.动态二叉链表的定义
- #include<stdio.h>
- #include<stdlib.h>
- #define NULL 0
- typedef char TElemType;
-
- typedef struct BiTNode{
- TElemType data;
- struct BiTNode *lchild, *rchild;
- }BiTNode, *BiTree;
测试用例:
测试按先序序列输入:abc..de.g..f...#,点号代表空树,#为输入结束符
另外下面的算法有使用到栈的,栈的相关实现如下:
- #define MAXSIZE 100
- typedef BiTNode* SElemType;
- typedef struct SqStack{
- SElemType data[MAXSIZE];
- int top;
- }SqStack;
-
-
- void initStack(SqStack &s){
- s.top = 0;
- }
-
-
- bool isEmpty(SqStack s){
- if(s.top == 0){
-
- return true;
- }else{
- return false;
- }
- }
-
-
- bool isFull(SqStack s){
- if(s.top == MAXSIZE){
- return true;
- }
- else{
- return false;
- }
- }
-
-
- void getTopElem(SqStack s, SElemType &e){
- if(!isEmpty(s))
- e = s.data[s.top-1];
- else
- printf("此栈为空栈,取栈顶元素失败n");
- }
-
-
- void push(SqStack &s, SElemType e){
- if(!isFull(s)){
- s.data[s.top] = e;
- s.top++;
- }else
- printf("此栈已满,入栈操作失败n");
- }
-
-
- void pop(SqStack &s, SElemType &e){
- if(!isEmpty(s)){
- e = s.data[s.top-1];
- s.top--;
- }
- else
- printf("此栈为空栈,出栈操作失败n");
- }
更详细的栈实现请参见我的另一篇博文:数据结构(5)--栈的定义以及相关操作的实现http://blog.csdn.net/u010366748/article/details/50639195
1.2按先序序列输入创建二叉树的递归算法
-
-
-
- void createBiTreeByPreOrder(BiTree &T){
-
-
- char ch;
- scanf("%c", &ch);
-
- if(ch != '#'){
- if(ch == '.'){
- T = NULL;
- }else{
- T = (BiTNode *)malloc(sizeof(BiTNode));
- T->data = ch;
- createBiTreeByPreOrder(T->lchild);
- createBiTreeByPreOrder(T->rchild);
- }
- }
- }
1.3先序遍历二叉树的递归算法
-
- void preOrderPrint(BiTree T){
- if(T){
- printf("%c ", T->data);
- preOrderPrint(T->lchild);
- preOrderPrint(T->rchild);
- }
- }
1.4先序遍历二叉树的非递归算法
-
- void preOrderPrint2(BiTree T){
- SqStack s;
- initStack(s);
- BiTNode *p = T;
- while(p || !isEmpty(s)){
- if(p){
- printf("%c ", p->data);
- push(s, p);
- p = p->lchild;
- }else{
-
- pop(s, p);
- p = p->rchild;
- }
- }
- }
演示:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- printf("先序遍历打印二叉树:n");
- preOrderPrint(T);
- printf("n");
-
- printf("先序遍历打印二叉树(非递归算法):n");
- preOrderPrint2(T);
- printf("n");
- }
1.5中序遍历二叉树的递归算法
-
- void inOrderPrint(BiTree T){
- if(T){
- inOrderPrint(T->lchild);
- printf("%c ", T->data);
- inOrderPrint(T->rchild);
- }
- }
1.6中序遍历二叉树的非递归算法
-
- void inOrderPrint2(BiTree T){
- SqStack s;
- initStack(s);
- BiTNode *p = T;
- while(p || !isEmpty(s)){
- if(p){
- push(s, p);
- p = p->lchild;
- }else{
- pop(s, p);
- printf("%c ", p->data);
- p = p->rchild;
- }
- }
- }
演示:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- printf("中序遍历打印二叉树:n");
- inOrderPrint(T);
- printf("n");
-
- printf("中序遍历打印二叉树(非递归算法):n");
- inOrderPrint2(T);
- printf("n");
- }
1.7后序遍历二叉树的递归算法
-
- void postOrderPrint(BiTree T){
- if(T){
- postOrderPrint(T->lchild);
- postOrderPrint(T->rchild);
- printf("%c ", T->data);
- }
- }
1.8后序遍历二叉树的非递归算法
-
- void postOrderPrint2(BiTree T){
- SqStack s;
- initStack(s);
- BiTNode *p = T;
- while(p || !isEmpty(s)){
- if(p){
- push(s, p);
- p = p->lchild;
- }else{
- BiTNode *top;
- getTopElem(s, top);
- if(top->data > 0){
- p = top->rchild;
- top->data = -top->data;
-
- }else{
- printf("%c ", -top->data);
- pop(s, top);
-
- }
- }
- }
- }
演示:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- printf("后序遍历打印二叉树:n");
- postOrderPrint(T);
- printf("n");
-
- printf("后序遍历打印二叉树(非递归算法):n");
- postOrderPrint2(T);
- printf("n");
- }
1.9按层次遍历二叉树的非递归算法
- typedef BiTNode* QElemType;
- typedef struct{
- QElemType data[20];
- int f;
- int r;
- }SqQueue;
-
- void initQueue(SqQueue &Q){
- Q.f = Q.r = 0;
- }
-
-
- void hierarchicalTraversePrint(BiTree T){
-
-
- SqQueue Q;
- initQueue(Q);
-
- if(T){
-
- Q.data[Q.r] = T;
- Q.r++;
- }
- while(Q.f != Q.r){
-
- if(Q.data[Q.f]->lchild){
- Q.data[Q.r] = Q.data[Q.f]->lchild;
- Q.r++;
- }
-
- if(Q.data[Q.f]->rchild){
- Q.data[Q.r] = Q.data[Q.f]->rchild;
- Q.r++;
- }
-
- printf("%c ", Q.data[Q.f]->data);
- Q.f++;
- }
- printf("n");
- }
演示:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- printf("按层次遍历打印二叉树(非递归算法):n");
- hierarchicalTraversePrint(T);
- }
2.遍历二叉树的应用
2.1求二叉树的深度
-
- int getBiTreeDepth(BiTree T){
- if(!T){
- return 0;
- }
- int leftTreeDepth = getBiTreeDepth(T->lchild);
- int rightTreeDepth = getBiTreeDepth(T->rchild);
- return leftTreeDepth > rightTreeDepth ? (leftTreeDepth+1) : (rightTreeDepth+1);
- }
演示:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- int depth = getBiTreeDepth(T);
- printf("该二叉树树的深度为%dn", depth);
- }
2.2求二叉树的结点数
-
- int getBiTreeSize(BiTree T){
- if(!T)
- return 0;
- int leftTreeSize = getBiTreeSize(T->lchild);
- int rightTreeSize = getBiTreeSize(T->rchild);
- return leftTreeSize + rightTreeSize + 1;
- }
演示:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- int size = getBiTreeSize(T);
- printf("该二叉树树的结点数为%dn", size);
- }
2.3求二叉树的叶子节点数
-
- int getBiTreeLeafNodesNum2(BiTree T){
- if(T){
- if(!T->lchild && !T->rchild)
- return 1;
- else{
- int leftTreeLeafNodesNum = getBiTreeLeafNodesNum2(T->lchild);
- int rightTreeLeafNodesNum = getBiTreeLeafNodesNum2(T->rchild);
- return leftTreeLeafNodesNum + rightTreeLeafNodesNum;
- }
- }else{
- return 0;
- }
- }
或者:
-
- void getBiTreeLeafNodesNum(BiTree T, int &count){
- if(T){
- if(!T->lchild && !T->rchild)
- count++;
-
- getBiTreeLeafNodesNum(T->lchild, count);
- getBiTreeLeafNodesNum(T->rchild, count);
-
- }
- }
演示:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- int leafNodesNum2 = 0;
- leafNodesNum2 = getBiTreeLeafNodesNum2(T);
- printf("该二叉树树的叶子点数为%dn", leafNodesNum2);
- }
或者:
- void main(){
- BiTree T;
- printf("请按先序次序输入二叉树各节点的值,以空格表示空树,以#号结束:n");
- createBiTreeByPreOrder(T);
-
- int leafNodesNum = 0;
- getBiTreeLeafNodesNum(T, leafNodesNum);
- printf("该二叉树树的叶子点数为%dn", leafNodesNum);
- }
最后
以上就是完美日记本为你收集整理的数据结构(13)二叉树的动态链表存储和遍历的实现 1.动态二叉链表存储即遍历的实现 2.遍历二叉树的应用的全部内容,希望文章能够帮你解决数据结构(13)二叉树的动态链表存储和遍历的实现 1.动态二叉链表存储即遍历的实现 2.遍历二叉树的应用所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复