数据结构——二叉树 前序、中序、后序、递归遍历和非递归遍历

来源:互联网 发布:免费直销软件下载 编辑:程序博客网 时间:2024/04/29 17:56

这是我做过的GitHub小项目(知天气),入手安卓的朋友可以拿我的代码做参考,大家可怜可怜给个赞吧哈哈!https://github.com/Xxianglei/HeartWeather



一、基本概念

      

         每个结点最多有两棵子树,左子树和右子树,次序不可以颠倒。

性质:

1、非空二叉树的第n层上至多有2^(n-1)个元素。

2、深度为h的二叉树至多有2^h-1个结点。

满二叉树:所有终端都在同一层次,且非终端结点的度数为2。

在满二叉树中若其深度为h,则其所包含的结点数必为2^h-1。

完全二叉树:除了最大的层次即成为一颗满二叉树且层次最大那层所有的结点均向左靠齐,即集中在左面的位置上,不能有空位置。

对于完全二叉树,设一个结点为i则其父节点为i/2,2i为左子节点,2i+1为右子节点。

二、存储结构

顺序存储:

将数据结构存在一块固定的数组中。

[cpp] view plain copy
print?
  1. #define LENGTH 100    
  2.  typedef char datatype;    
  3.  typedef struct node{    
  4.      datatype data;    
  5.      int lchild,rchild;    
  6.      int parent;    
  7.  }Node;    
  8.      
  9.  Node tree[LENGTH];    
  10.  int length;    
  11.  int root;  


   虽然在遍历速度上有一定的优势,但因所占空间比较大,是非主流二叉树。二叉树通常以链式存储。

链式存储:

[cpp] view plain copy
print?
  1. typedef int Status;      
  2. typedef int TElemType;   
  3. typedef struct BiTNode{  
  4. TElemType data;  
  5. struct BiTNode *lchild,*rchild;  
  6. }BiTNode , *BiTree;   


 三.二叉树的创建(先序创建)

[cpp] view plain copy
print?
  1. void createBiTree(BiTree &T)    
  2. {    
  3.     char c;    
  4.     cin >> c;    
  5.     if('#' == c)    
  6.         T = NULL;    
  7.     else    
  8.     {    
  9.         if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))exit(-2);  
  10.         T->data = c;    
  11.         createBiTree(T->lchild);    
  12.         createBiTree(T->rchild);    
  13.     }    
  14. }    


四,输出二叉树
[cpp] view plain copy
print?
  1. //输出二叉树  
  2. void PrintBTree( BiTree &T){  
  3. if(T!=NULL){  
  4. printf("%c",T->data); //输出根节点的值  
  5. if(T->lchild!=NULL||T->rchild!=NULL){  
  6. printf("(");  
  7. PrintBTree(T->lchild);//输出左子树  
  8. if(T->rchild!=NULL)  
  9. printf(",");  
  10. PrintBTree(T->rchild);//输出右子树  
  11. printf(")");  
  12. }  
  13. }  
  14. }  
五,二叉树的(递归)遍历

遍历即将树的所有结点访问且仅访问一次。按照根节点

置的不同分为前序遍历,中序遍历,后序遍历。

前序遍历:根节点->左子树->右子树

中序遍历:左子树->根节点->右子树

后序遍历:左子树->右子树->根节点

例如:求下面树的三种遍历

 

前序遍历:abdefgc

中序遍历:debgfac

后序遍历:edgfbca

[cpp] view plain copy
print?
  1. void Preorder(BiTree &T){  
  2. if(T!=NULL){  
  3. printf("%c,",T->data);  
  4. Preorder(T->lchild);  
  5. Preorder(T->rchild);  
  6. }  
  7. }    
  8. //中序遍历  
  9. void Inorder(BiTree &T){  
  10. if(T!=NULL) {  
  11.    Inorder(T->lchild);  
  12. printf("%c,",T->data);  
  13.    Inorder(T->rchild);  
  14. }  
  15. }  
  16. //后序遍历  
  17. void Postorder(BiTree &T){  
  18. if(T!=NULL){  
  19. Postorder(T->lchild);  
  20. Postorder(T->rchild);  
  21. printf("%c,",T->data);  
  22. }  
  23. }  


六,中序非递归遍历

[cpp] view plain copy
print?
  1. // 中序非递归  
  2. void FInorder(BiTree T){  
  3. BiTree p;  
  4.     BiTree s[StackMaxSize];              //定义s数组作为存储根结点的指针的栈使用  
  5.     int top = -1;                       //栈顶指针置为-1,表示空栈  
  6.     p=T;                                //跟指针  
  7.     if(!p){  
  8.         printf("空树");  
  9.     }  
  10.     while(p||top!=-1){                 //不是空树或者栈不为空  
  11.         while(p){  
  12.         top++;                      
  13.         s[top]=p;  
  14.         p=p->lchild;  
  15.         }  
  16.         p=s[top];    //将值给p  
  17.         s[top]=NULL;   //制空s【top】这个位置  
  18.         top--;          //top向上移一个位置  
  19.         printf("%c,",p->data);  //输出p对应的数值  
  20.         p=p->rchild;            //访问右子树  
  21.     }  
  22. }  


图示过程:

 

 

 完整代码(简单代码)

[cpp] view plain copy
print?
  1. #include<iostream>    
  2. #include<stdio.h>    
  3. #include<stdlib.h>     
  4. #define QueueMaxSize 20 //定义队列数组长度  
  5. #define StackMaxSize 10 //定义栈数组长度  
  6. #define TRUE   1      
  7. #define FALSE   0      
  8. #define OK    1      
  9. #define ERROR   0      
  10. #define INFEASIBLE -1      
  11. #define OVERFLOW -2    
  12. using namespace std;          //以上是一些宏定义和头文件,在此不多赘述       
  13. typedef int Status;      
  14. typedef int TElemType;  
  15. typedef struct BiTNode{  
  16. TElemType data;  
  17. struct BiTNode *lchild,*rchild;  
  18. }BiTNode , *BiTree;  
  19.      
  20. void createBiTree(BiTree &T)    
  21. {    
  22.     char c;    
  23.     cin >> c;    
  24.     if('#' == c)    
  25.         T = NULL;    
  26.     else    
  27.     {    
  28.         if(!(T=(BiTNode*)malloc(sizeof(BiTNode))))exit(-2);  
  29.         T->data = c;    
  30.         createBiTree(T->lchild);    
  31.         createBiTree(T->rchild);    
  32.     }    
  33. }    
  34. //输出二叉树  
  35. void PrintBTree( BiTree &T){  
  36. if(T!=NULL){  
  37. printf("%c",T->data); //输出根节点的值  
  38. if(T->lchild!=NULL||T->rchild!=NULL){  
  39. printf("(");  
  40. PrintBTree(T->lchild);//输出左子树  
  41. if(T->rchild!=NULL)  
  42. printf(",");  
  43. PrintBTree(T->rchild);//输出右子树  
  44. printf(")");  
  45. }  
  46. }  
  47. }  
  48. //前序遍历  
  49. void Preorder(BiTree &T){  
  50. if(T!=NULL){  
  51. printf("%c,",T->data);  
  52. Preorder(T->lchild);  
  53. Preorder(T->rchild);  
  54. }  
  55. }    
  56. //中序遍历  
  57. void Inorder(BiTree &T){  
  58. if(T!=NULL) {  
  59.    Inorder(T->lchild);  
  60. printf("%c,",T->data);  
  61.    Inorder(T->rchild);  
  62. }  
  63. }  
  64. //后序遍历  
  65. void Postorder(BiTree &T){  
  66. if(T!=NULL){  
  67. Postorder(T->lchild);  
  68. Postorder(T->rchild);  
  69. printf("%c,",T->data);  
  70. }  
  71. }  
  72. // 中序非递归  
  73. void FInorder(BiTree T){  
  74. BiTree p;  
  75.     BiTree s[StackMaxSize];              //定义s数组作为存储根结点的指针的栈使用  
  76.     int top = -1;                       //栈顶指针置为-1,表示空栈  
  77.     p=T;                                //跟指针  
  78.     if(!p){  
  79.         printf("空树");  
  80.     }  
  81.     while(p||top!=-1){                 //不是空树或者栈不为空  
  82.         while(p){  
  83.         top++;                      
  84.         s[top]=p;  
  85.         p=p->lchild;  
  86.         }  
  87.         p=s[top];    //将值给p  
  88.         s[top]=NULL;   //制空s【top】这  
  89. 位置  
  90.         top--;          //top向上移一个  
  91. 置  
  92.         printf("%c,",p->data);  //输出p对应的数值  
  93.         p=p->rchild;            //访问右  
  94. 树  
  95.     }  
  96. }  
  97. int main()  
  98. {  
  99. BiTree bt;  
  100.     printf("先序建立二叉树,#号代表空\n");  
  101.     createBiTree(bt);  
  102.     PrintBTree(bt);  
  103.     printf("\n");  
  104.    
  105.     printf("前序:");  
  106.     Preorder(bt);  
  107.     printf("\n");  
  108.    
  109.     printf("中序:");  
  110.     Inorder(bt);  
  111.     printf("\n");  
  112.    
  113.     printf("后序:");  
  114.     Postorder(bt);  
  115.     printf("\n");  
  116.       
  117.     printf("中序非递归:");  
  118.     FInorder(bt);  
  119.  }  



自己输入一个二叉树的元素试一试,比如:

先序建立二叉树,#号代表空
ABD##E##CF##G##
A(B(D,E),C(F,G))
前序:A,B,D,E,C,F,G,
中序:D,B,E,A,F,C,G,
后序:D,E,B,F,G,C,A,

中序非递归:D,B,E,A,F,C,G,

 

阅读全文
0 0
原创粉丝点击