《數據結構》樹和二叉樹代碼整理(C語言實現)


前言:

  排版很難看,沒辦法,我絕對不是因為懶得排而懶得排,而是因為只有被命運石之門選中的人才能從頭到尾夠看到底。

先序創建二叉樹

(這里用了C++  <引用>的特性,使用二重指針代替或者將函數返回值設成指針再做點小修改也能實現)

 

 1 void CreateTree(TreeRoot &Root)
 2 {//創建(先序)
 3     char c;
 4     c=getchar();
 5     if(c!='#')
 6     {
 7         Root=(TreeRoot)malloc(sizeof(TNode));
 8         Root->data=c;
 9         CreateTree(Root->pleft);
10         CreateTree(Root->pright);
11     }
12     else
13     {
14         Root=NULL;
15     }
16 }

 

 

二叉樹遍歷(前|中|后 序)--遞歸(核心代碼)

 1  
 2 void InorderTraversal( BinTree BT )
 3 {
 4     if( BT ) {
 5         InorderTraversal( BT->Left );
 6         /* 此處假設對BT結點的訪問就是打印數據 */
 7         printf("%d ", BT->Data); /* 假設數據為整型 */
 8         InorderTraversal( BT->Right );
 9     }
10 }
11  
12 void PreorderTraversal( BinTree BT )
13 {
14     if( BT ) {
15         printf("%d ", BT->Data );
16         PreorderTraversal( BT->Left );
17         PreorderTraversal( BT->Right );
18     }
19 }
20  
21 void PostorderTraversal( BinTree BT )
22 {
23     if( BT ) {
24         PostorderTraversal( BT->Left );
25         PostorderTraversal( BT->Right );
26         printf("%d ", BT->Data);
27     }
28 }
29 
30 ————————————————
31 版權聲明:本文為CSDN博主「BrianOne」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
32 原文鏈接:https://blog.csdn.net/Brianone/article/details/89440024

二叉樹遍歷(前|中|后|層 序)--非遞歸(核心代碼)

 1 void PreOrderFDG(TreeRoot Root)
 2 {//先序遍歷非遞歸
 3     SqStack S;
 4     InitStack(S);
 5     while(Root!=NULL || Empty(S)==0)
 6     {
 7         if(Root!=NULL)
 8         {
 9             printf("%c ",Root->data);
10             Push(S,Root);
11             Root=Root->pleft;
12         }
13         else
14         {
15             Pop(S,Root);
16             Root=Root->pright;
17         }
18     }
19 }
 1 void InOrderFDG(TreeRoot Root)
 2 {//中序遍歷非遞歸
 3     SqStack S;
 4     InitStack(S);
 5     while(Root!=NULL || Empty(S)==0)
 6     {
 7         if(Root!=NULL)
 8         {
 9             Push(S,Root);
10             Root=Root->pleft;
11         }
12         else
13         {
14             Pop(S,Root);
15             printf("%c ",Root->data);
16             Root=Root->pright;
17         }
18     }
19 }
 1 //后序遍歷非遞歸算法
 2 typedef struct SNode{
 3     BiTree p;
 4     int tag;
 5 }SNode;
 6 //后序遍歷使用的進棧函數
 7 void postpush(SNode *a,SNode sdata){
 8     a[++top]=sdata;
 9 }
10 //后序遍歷函數
11 void PostOrderTraverse(BiTree Tree){
12     SNode a[20];//定義一個順序棧
13     BiTNode * p;//臨時指針
14     int tag;
15     SNode sdata;
16     p=Tree;
17     while (p||top!=-1) {
18         while (p) {
19             //為該結點入棧做准備
20             sdata.p=p;
21             sdata.tag=0;//由於遍歷是左孩子,設置標志位為0
22             postpush(a, sdata);//壓棧
23             p=p->lchild;//以該結點為根結點,遍歷左孩子
24         }
25         sdata=a[top];//取棧頂元素
26         pop();//棧頂元素彈棧
27         p=sdata.p;
28         tag=sdata.tag;
29         //如果tag==0,說明該結點還沒有遍歷它的右孩子
30         if (tag==0) {
31             sdata.p=p;
32             sdata.tag=1;
33             postpush(a, sdata);//更改該結點的標志位,重新壓棧
34             p=p->rchild;//以該結點的右孩子為根結點,重復循環
35         }
36         //如果取出來的棧頂元素的tag==1,說明此結點左右子樹都遍歷完了,可以調用操作函數了
37         else{
38             displayElem(p);
39             p=NULL;
40         }
41     }
42 }

后序 雙棧法

 1 void NotRecursionPostOrder(BTree T){
 2   PLinkStack S,CS;
 3   S=Init_Stack();
 4   CS=Init_Stack();
 5   while(T || !empty_Stack(S)){
 6     if(T){
 7       Push_Stack(S,T);
 8       Push_Stack(CS,T);
 9       T=T->right;
10     }else{
11       T=Pop_Stack(S)->data;
12       T=T->left;
13     }
14   }
15   while(CS->top!=NULL){
16     printf("%c",CS->top->data->element);
17     CS->top=CS->top->next;
18   }
19   DestroyStack(CS);
20 }

 

 1 void LevelorderTraversal ( BinTree BT )//層序遍歷
 2 { 
 3     Queue Q; 
 4     BinTree T;
 5  
 6     if ( !BT ) return; /* 若是空樹則直接返回 */
 7      
 8     Q = CreatQueue(); /* 創建空隊列Q */
 9     AddQ( Q, BT );
10     while ( !IsEmpty(Q) ) {
11         T = DeleteQ( Q );
12         printf("%d ", T->Data); /* 訪問取出隊列的結點 */
13         if ( T->Left )   AddQ( Q, T->Left );
14         if ( T->Right )  AddQ( Q, T->Right );
15     }
16 
17 ————————————————
18 版權聲明:本文為CSDN博主「BrianOne」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
19 原文鏈接:https://blog.csdn.net/Brianone/article/details/89440024

 

點這里有個C++ 版,方法很多,只會C的話應該能看懂思路

點這里有思路清晰的C語言版本

點這里有個后續遍歷的不錯思路——節點里增加了一個變量記錄次數(或者用哈希表也可以)

后序遍歷方法挺多,多搜一搜,特別是C++ 代碼最好也能看看,有很多優秀的實現都是c++版的。

求二叉樹高度

 1 int PostTreeDepth(TreeRoot Root)
 2 {//求二叉樹的高度
 3     int hl,hr,max;
 4     if(Root!=NULL)
 5     {
 6         hl=PostTreeDepth(Root->pleft);
 7         hr=PostTreeDepth(Root->pright);
 8         max=hl>hr?hl:hr;
 9         return max+1;
10     }
11     else
12         return 0;
13 }

按樹狀打印二叉樹

 1 void PrintTree(TreeRoot Root,int nLayer)
 2 {//按樹狀打印二叉樹
 3     int i;
 4     if(Root==NULL)
 5         return ;
 6     PrintTree(Root->pright,nLayer+1);
 7     for(i=0;i<=nLayer;i++)
 8     {
 9         printf("  ");
10     }
11     printf("%c\n",Root->data);
12     PrintTree(Root->pleft,nLayer+1);
13 }

輸出二叉樹葉子節點並統計葉子節點的數目

 1 void PreOrderLeaf(TreeRoot Root)
 2 {//輸出二叉樹的葉子結點,並統計葉子結點的數目
 3     if(Root!=NULL)
 4     {
 5         if(Root->pleft==NULL && Root->pright==NULL)
 6         {
 7             printf("%c ",Root->data);
 8             leafcount++;
 9         }
10         PreOrderLeaf(Root->pleft);
11         PreOrderLeaf(Root->pright);
12     }
13 }

 

哈夫曼樹(編碼)

 

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 typedef struct hfNode {
  5     int weight;
  6     struct hfNode* lch, * rch, * pnt;
  7 }hfNode;
  8 
  9 void selMin(hfNode* nodes, int n, int* a, int* b) { //求兩個最小值的下標
 10     int temp[3] = { 0x3f3f3f3f,0x3f3f3f3f,0x3f3f3f3f };
 11     int i;
 12     for (i = 0; i < n; ++i) {
 13         //printf("%d,%d;", temp[1], temp[2]);
 14         if (nodes[i].pnt == 0) {
 15             int t = nodes[i].weight;
 16             if (temp[1] == 0x3f3f3f3f) {
 17                 temp[1] = t;
 18                 *a = i;
 19             }
 20             else if (temp[2] == 0x3f3f3f3f) {
 21                 temp[2] = t;
 22                 *b = i;
 23             }
 24             else if (temp[1] > t) {
 25                 temp[1] = t;
 26                 *a = i;
 27             }
 28             else if (temp[2] > t) {
 29                 temp[2] = t;
 30                 *b = i;
 31             }
 32         }
 33         //printf("%d,%d;\n", temp[1], temp[2]);
 34     }
 35 }
 36 
 37 void CreateTree(hfNode* nodes, char** hfCode, int* w, int n) {  //構造 Huffman Tree   規定向左走則生成0  右 1
 38     int m = 2 * n - 1;
 39     int j;
 40     int len;
 41     char* helper = (char*)malloc(2 * n * sizeof(char));
 42 
 43     for (int i = n; i < m; ++i) {
 44         int a, b;
 45         selMin(nodes, m, &a, &b);
 46         nodes[i].pnt = 0;
 47         nodes[i].lch = &nodes[a];
 48         nodes[i].rch = &nodes[b];
 49         nodes[a].pnt = &nodes[i];
 50         nodes[b].pnt = &nodes[i];
 51         nodes[i].weight = nodes[a].weight + nodes[b].weight;
 52     }
 53     //樹建立完畢 下面開始生成哈夫曼碼
 54     for (int i = 0; i < n; ++i) {
 55         hfNode* cur = nodes + i;
 56         j = 0;
 57         while (cur->pnt != 0) {
 58             hfNode* p = cur->pnt;
 59             if (p->lch == cur) {
 60                 helper[j++] = '0';
 61             }
 62             else {
 63                 helper[j++] = '1';
 64             }
 65             cur = cur->pnt;
 66         }
 67         hfCode[i] = (char*)malloc(sizeof(char) * (j + 1));
 68         len = 0;
 69         while (j > 0) {
 70             hfCode[i][len++] = helper[--j];
 71         }
 72         hfCode[i][len] = '\0';
 73     }
 74     free(helper);
 75 }
 76 void show(char** hfCode, int n) {
 77     //int n;
 78     int i;
 79     printf("\n按照輸入順序   哈夫曼編碼依次為:\n");
 80     //printf("%d,%d\n", sizeof(hfCode) , sizeof(char*));
 81     //n = sizeof(hfCode) / sizeof(char*);
 82     for (i = 0; i < n; ++i) {
 83         printf("%s\n", hfCode[i]);
 84     }
 85     printf("\n================================\n");
 86 }
 87 void main() {
 88     hfNode* nodes;
 89     char** hfCode;
 90     int n;
 91     int* w;
 92     int i;
 93     printf("請輸入字符數量:\n");
 94     scanf("%d", &n);
 95     w = (int*)malloc(n * sizeof(char*));
 96     hfCode = (char**)malloc(n * sizeof(char*));
 97     nodes = (hfNode*)malloc((2 * n - 1) * sizeof(hfNode));
 98     printf("請依次輸入字符權重:\n");
 99     for (i = 0; i < n; ++i) {
100         scanf("%d", w + i);
101         (nodes + i)->weight = w[i];
102         (nodes + i)->lch = (nodes + i)->rch = (nodes + i)->pnt = 0;
103     }
104     CreateTree(nodes, hfCode, w, n);
105     show(hfCode, n);
106     printf("%p,%p", (nodes + 1)->pnt, (nodes + 3)->pnt);
107     free(w);
108     free(nodes);
109     free(hfCode);
110 
111 }
112 /*
113 A  B  C  D
114 3  1  2  1
115 應該的編碼之一應該為:
116 
117 0
118 101
119 11
120 100
121 */

 

二叉搜索樹--遞歸--插入,刪除,和查找

 1 //二叉查找樹 遞歸實現 插入,刪除和查找
 2 #include<stdio.h>
 3 #include<stdlib.h>
 4 
 5 typedef struct Node {  6     int val;  7     struct Node* left;  8     struct Node* right;  9 }Node;  10 typedef int bool;  11 
 12 Node * Find(Node* root,int a);   //如果找到了返回指針 如果找不到則返回空指針
 13 Node *Insert(Node* root, int a);  //將a插入到root中並且返回根節點指針
 14 Node *Delete(Node* root,int a);   //將a從root中刪除並且返回根節點指針 
 15 Node* FindMin(Node* root);//找到最小節點並且返回該節點的指針
 16 Node* FindMax(Node* root);//找到最大節點並且返回該節點的指針
 17 void PreorderTraversal(Node* root); //先序遍歷
 18 int main(){  19     Node* root = (Node*)malloc(sizeof(Node));//根節點
 20     Node * BST, *MinP, *MaxP, *Tmp;   //分別是樹根節點 最小節點 最大節點 和一個臨時用的節點指針變量
 21     int X;  22     int N, i;  23     BST = NULL;  24     scanf("%d", &N);//插入節點
 25     for (i = 0; i < N; i++) {  26         scanf("%d", &X);  27         BST = Insert(BST, X);  28  }  29     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍歷
 30     MinP = FindMin(BST);  31     MaxP = FindMax(BST);  32     scanf("%d", &N);//查找節點
 33     for (i = 0; i < N; i++) {  34         scanf("%d", &X);  35         Tmp = Find(BST, X);  36         if (Tmp == NULL) printf("%d is not found\n", X);  37         else {  38             printf("%d is found\n", Tmp->val);  39             if (Tmp == MinP) printf("%d is the smallest key\n", Tmp->val);  40             if (Tmp == MaxP) printf("%d is the largest key\n", Tmp->val);  41  }  42  }  43     scanf("%d", &N);//刪除節點
 44     for (i = 0; i < N; i++) {  45         scanf("%d", &X);  46         BST = Delete(BST, X);  47  }  48     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍歷
 49     return 0;  50 }  51 
 52 Node *Find(Node* root,int a) { //查找
 53     if (!root) return NULL;  54     if (a == root->val) {  55         return root;  56  }  57     else if (a > root->val) {  58         return Find(root->right, a);  59  }  60     else {//a< root->val
 61         return Find(root->left, a);  62  }  63     return NULL;//實際上這一行是沒有必要的
 64 }  65 Node *Insert(Node* root, int a) {  66     if (!root) {  67         Node* newNode = (Node*)malloc(sizeof(Node));  68         newNode->val = a;  69         newNode->left = newNode->right = NULL;  70         return newNode;  71  }  72     else if (a > root->val) {  73         root->right = Insert(root->right, a);  74  }  75     else if (a < root->val) {  76         root->left = Insert(root->left, a);  77  }  78     else {  79         //這時候root的節點值等於a則不處理
 80  }  81     return root;  82 }  83 Node* Delete(Node* root, int a) {  84     if (!root) return root;  85     if (a < root->val)       root->left = Delete(root->left, a);  86     else if(a > root->val)   root->right = Delete(root->right, a);  87     else{  //找到這個節點 那么進行刪除操作 此刻要考慮四種情況:左右空 左右非空 左空右非空 左非空右空
 88         if (!root->left) { Node* temp = root; root = root->right; free(temp); }  89         else if (!root->right) { Node* temp = root; root = root->left;free(temp); }  90         else {  91             Node* temp = root->right;  92             while (temp->left) {  93                 temp = temp->left;  94  }  95             temp->left = root->left;  96             root = root->right;  97  }  98  }  99     return root;//這一句實際上沒有必要
100 } 101 Node* FindMin(Node* root) { 102     if (!root) { 103         return NULL; 104  } 105     if (!root->left) { 106         return root; 107  } 108     return FindMin(root->left); 109 } 110 Node* FindMax(Node* root) { 111     if (!root) { 112         return NULL; 113  } 114     if (!root->right) { 115         return root; 116  } 117     return FindMax(root->right); 118 } 119 void PreorderTraversal(Node* root) { 120     if (!root) return; 121     printf("%d ", root->val); 122     PreorderTraversal(root->left); 123     PreorderTraversal(root->right); 124 }

 二叉搜索樹--非遞歸--插入、刪除和查找

  1 #define _CRT_SECURE_NO_WARNINGS
  2 //二叉查找樹   非遞歸實現  插入,刪除和查找
  3 #include<stdio.h>
  4 #include<stdlib.h>
  5 
  6 typedef struct Node {
  7     int val;
  8     struct Node* left;
  9     struct Node* right;
 10 }Node;
 11 typedef int bool;
 12 
 13 Node* Find(Node* root, int a);   //如果找到了返回指針 如果找不到則返回空指針
 14 Node* Insert(Node* root, int a);  //將a插入到root中並且返回根節點指針
 15 Node* Delete(Node* root, int a);   //將a從root中刪除並且返回根節點指針
 16 Node* FindMin(Node* root);//找到最小節點並且返回該節點的指針
 17 Node* FindMax(Node* root);//找到最大節點並且返回該節點的指針
 18 void PreorderTraversal(Node* root); //先序遍歷
 19 int main() {
 20     Node* root = (Node*)malloc(sizeof(Node));//根節點
 21     Node* BST, * MinP, * MaxP, * Tmp;   //分別是樹根節點   最小節點 最大節點 和一個臨時用的節點指針變量
 22     int X;
 23     int N, i;
 24     BST = NULL;
 25     scanf("%d", &N);//插入節點
 26     for (i = 0; i < N; i++) {
 27         scanf("%d", &X);
 28         BST = Insert(BST, X);
 29     }
 30     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍歷
 31     MinP = FindMin(BST);
 32     MaxP = FindMax(BST);
 33     scanf("%d", &N);//查找節點
 34     for (i = 0; i < N; i++) {
 35         scanf("%d", &X);
 36         Tmp = Find(BST, X);
 37         if (Tmp == NULL) printf("%d is not found\n", X);
 38         else {
 39             printf("%d is found\n", Tmp->val);
 40             if (Tmp == MinP) printf("%d is the smallest key\n", Tmp->val);
 41             if (Tmp == MaxP) printf("%d is the largest key\n", Tmp->val);
 42         }
 43     }
 44     scanf("%d", &N);//刪除節點
 45     for (i = 0; i < N; i++) {
 46         scanf("%d", &X);
 47         BST = Delete(BST, X);
 48     }
 49     printf("Preorder:"); PreorderTraversal(BST); printf("\n");//先序遍歷
 50     return 0;
 51 }
 52 Node* Find(Node* root, int a) { //查找
 53     if (!root) return NULL;
 54     while (root) {
 55         if (a == root->val) {
 56             break;
 57         }
 58         else if (a > root->val) {
 59             root = root->right;
 60         }
 61         else {//a< root->val
 62             root = root->left;
 63         }
 64     }
 65     return root;
 66 }
 67 Node* Insert(Node* root, int a) {
 68     if (!root) {
 69         root = (Node*)malloc(sizeof(Node));
 70         root->val = a;
 71         root->left = root->right = NULL;
 72     }
 73     else {
 74         Node* cur = root;
 75         Node* pre = NULL;
 76         while (cur) {
 77             if (a > cur->val) {
 78                 pre = cur;
 79                 cur = cur->right;
 80             }
 81             else if (a < cur->val) {
 82                 pre = cur;
 83                 cur = cur->left;
 84             }
 85             else {
 86                 break;//找到了值為a的節點
 87             }
 88         }
 89         if (!cur) {  //如果沒有找到 則插入
 90             if (a < pre->val) {
 91                 pre->left = (Node*)malloc(sizeof(Node));
 92                 pre->left->left = pre->left->right = NULL;
 93                 pre->left->val = a;
 94             }
 95             else {
 96                 pre->right = (Node*)malloc(sizeof(Node));
 97                 pre->right->left = pre->right->right = NULL;
 98                 pre->right->val = a;
 99             }
100         }
101     }
102     return root;
103 }
104 Node* Delete(Node* root, int a) {//為什么代碼這么長?因為沒用到遞歸也沒用到引用 就需要一個pre
105     if (!root) return root;//根節點為空
106     Node* pre = NULL;
107     Node* cur = root;
108     while (cur) {
109         if (a < cur->val) {
110             pre = cur;
111             cur = cur->left;
112         }
113         else if (a > cur->val) {
114             pre = cur;
115             cur = cur->right;
116         }
117         else {
118             if ((!cur->left) && (!cur->right)) {
119                 if (!pre) {//cur為根節點
120                     free(cur);
121                     return pre;
122                 }
123                 if (a < pre->val) {
124                     pre->left = NULL;
125                 }
126                 else {
127                     pre->right = NULL;
128                 }
129                 return root;
130             }
131             else if (!cur->right) {
132                 if (!pre) {
133                     cur = cur->left;
134                     free(root);
135                     return cur;
136                 }
137                 if (a < pre->val) {
138                     pre->left = cur->left;
139                     free(cur);
140                 }
141                 else {
142                     pre->right = cur->left;
143                     free(cur);
144                 }
145                 return root;
146             }
147             else if (!cur->left) {
148                 if (!pre) {
149                     cur = cur->right;
150                     free(root);
151                     return cur;
152                 }
153                 if (a < pre->val) {
154                     pre->left = cur->right;
155                     free(cur);
156                 }
157                 else {
158                     pre->right = cur->right;
159                     free(cur);
160                 }
161                 return root;
162             }
163             else {
164                 Node* temp = cur->left;
165                 while (temp->right) {
166                     temp = temp->right;
167                 }
168                 temp->right = cur->right;
169                 if (!pre) { Node* t = cur->left; free(cur); return t; };
170                 if (a < pre->val) {
171                     pre->left = cur->left;
172                 }
173                 else {
174                     pre->right = cur->left;
175                 }
176                 free(cur);
177                 return root;
178             }
179         }
180     }
181     return root;
182 }
183 Node* FindMin(Node* root) {
184     if (!root) {
185         return NULL;
186     }
187     while (root->left) {
188         root = root->left;
189     }
190     return root;
191 }
192 Node* FindMax(Node* root) {
193     if (!root) {
194         return NULL;
195     }
196     while (root->right) {
197         root = root->right;
198     }
199     return root;
200 }
201 void PreorderTraversal(Node* root) {
202     if (!root) return;
203     printf("%d  ", root->val);
204     PreorderTraversal(root->left);
205     PreorderTraversal(root->right);
206 }

 由前序中序重建二叉樹

 由中序后序重建二叉樹

【Morris_Traversal】二叉樹遍歷(前|中|后 序)--非遞歸--不用棧--(完整代碼)

  1 #define _CRT_SECURE_NO_WARNINGS
  2 #include<stdio.h>
  3 #include<stdlib.h>
  4 #define DataType int
  5 #define nullptr 0
  6 #define MAXN 2048
  7 typedef struct node {
  8     DataType data;
  9     struct node* pleft;
 10     struct node* pright;
 11 }TNode, * TreeRoot;
 12 TNode* CreateTree() {//構建二叉樹  (先序)
 13     int a;
 14     scanf("%d", &a);
 15     if (a != -1){
 16         TNode* ne = (TNode*)malloc(sizeof(TNode));
 17         ne->data = a;
 18         ne->pleft = CreateTree();
 19         ne->pright = CreateTree();
 20         return ne;
 21     }
 22     return nullptr;
 23 }
 24 void traversal_pre(TNode * root) { //先序遍歷二叉樹morris_traversal  (已過測試集)
 25     while (root) {
 26         if (root->pleft) {
 27             TNode* rightmost = root->pleft;
 28             while (rightmost->pright&&rightmost->pright!=root) {
 29                 rightmost = rightmost->pright;
 30             }
 31             if (rightmost->pright) {
 32                 rightmost->pright = nullptr;
 33                 root = root->pright;
 34             }
 35             else {
 36                 printf("%d ", root->data); //輸出數據
 37                 rightmost->pright = root;
 38                 root = root->pleft;
 39             }
 40         }
 41         else {
 42             printf("%d ", root->data); //輸出數據
 43             root = root->pright;
 44         }
 45     }
 46 }
 47 void traversal_mid(TNode* root) { //中序遍歷二叉樹morris_traversal  (已過測試集)
 48     while (root) {
 49         if (root->pleft) {
 50             TNode* rightmost = root->pleft;
 51             while (rightmost->pright && rightmost->pright != root) {
 52                 rightmost = rightmost->pright;
 53             }
 54             if (rightmost->pright) {
 55                 rightmost->pright = nullptr;
 56                 printf("%d ", root->data); //輸出數據
 57                 root = root->pright;
 58             }
 59             else {
 60                 rightmost->pright = root;
 61                 root = root->pleft;
 62             }
 63         }
 64         else {
 65             printf("%d ", root->data); //輸出數據
 66             root = root->pright;
 67         }
 68     }
 69 }
 70 void traversal_pos(TNode* root) { //后序遍歷二叉樹morris_traversal    (已過測試集)
 71     int temp[MAXN];
 72     int n = 0;
 73     while (root) {
 74         if (root->pright) {
 75             TNode* lm = root->pright;
 76             while (lm->pleft&&lm->pleft != root) {
 77                 lm = lm->pleft;
 78             }
 79             if (lm->pleft) {
 80                 lm->pleft = nullptr;
 81                 root = root->pleft;
 82             }
 83             else {
 84                 lm->pleft = root;
 85                 //printf("%d", root->data);
 86                 temp[n++] = root->data;
 87                 root = root->pright;
 88             }
 89         }
 90         else {
 91             temp[n++] = root->data;
 92             root = root->pleft;
 93         }
 94     }
 95     while (n) {
 96         printf("%d ", temp[--n]);
 97     }
 98 }
 99 int main() {
100     TNode * tree = CreateTree();  //創建二叉樹
101     
102     //traversal_pre(tree);
103     //traversal_mid(tree);
104     traversal_pos(tree);
105     //system("pause");
106     return 0;
107 }
108 /*
109 空樹: -1
110  ==============
111      1
112 
113 :1 -1 -1
114 先序遍歷結果; 1
115 中序遍歷結果: 1
116 后序遍歷結果:1
117 =======================
118       1
119     2  
120 
121 :1 2 -1 -1 -1
122 先序遍歷結果: 1  2
123 中序遍歷結果: 2  1
124 后序遍歷結果:2  1
125 =======================
126       1
127         2
128 
129 :1 -1 2 -1 -1
130 先序遍歷結果: 1  2
131 中序遍歷結果: 1  2
132 后序遍歷結果:2  1
133 =======================
134        1
135     2      3
136       4   5  6
137 
138 :1 2 -1 4 -1 -1 3 5 -1 -1 6 -1 -1
139 先序遍歷結果:1  2  4  3  5  6
140 中序遍歷結果:2  4  1  5  3  6 
141 后序遍歷結果: 4  2  5  6  3  1
142 */

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM