树的各种遍历(c 语言实现)
Posted 南冥修士
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了树的各种遍历(c 语言实现)相关的知识,希望对你有一定的参考价值。
在实现过程中遇到的一个问题就是我太马虎,总是缺这缺那,对细节没有太多注意导致我一直调试 不过,还无法发现错误,期间多次想放弃,但还是忍住了,最后发现了问题,(该死的非(!);
树的遍历很多包括,先序、中序、后序、层次,其中先中后序都可分两种方式:递归和非递归(我用栈实现),层次我用两种方法:栈和队列;代码如下(第一次发博):
1 //BinTree.c 2 #include <stdbool.h> 3 //树节点 4 struct TreeNode{ 5 char Num; 6 struct TreeNode * Left; 7 struct TreeNode * Right; 8 }; 9 typedef TreeNode *BinTree; 10 bool CreateTree(BinTree *); 11 12 void PostOrderTraversal(BinTree); 13 void PreOrderTraversal(BinTree); 14 void InOrderTraversal(BinTree); 15 void LevelTaversal(BinTree); 16 int PostOrderGetHeigh(BinTree); 17 18 //栈节点 19 struct SNode{ 20 BinTree BT; 21 struct SNode *Next; 22 }; 23 24 typedef struct SNode * Stack; 25 26 Stack CreateStack(); 27 bool Push(Stack, BinTree); 28 bool IsEmpty(Stack); 29 BinTree Pop(Stack); 30 bool DestroyStack(Stack); 31 32 //队列节点 33 struct Node{ 34 BinTree BT; 35 struct Node *Next; 36 }; 37 typedef struct Node * Position; 38 39 struct QNode{ 40 Position Front; 41 Position Rear; 42 }; 43 typedef struct QNode * Queue; 44 45 Queue CreateQueue(); 46 bool AddQ(Queue, BinTree); 47 bool IsEmpty(Queue); 48 BinTree DeleteQ(Queue); 49 bool DestroyQueue(Queue);
1 #include "BinTree.h" 2 #include <stdio.h> 3 #include <stdlib.h> 4 5 6 7 bool CreateTree(BinTree *BT) 8 { 9 10 11 char ch; 12 scanf_s("%c ", &ch); 13 14 15 if (ch == ‘#‘){ 16 *BT = NULL; 17 } 18 else 19 { 20 21 (*BT)= (BinTree)malloc(sizeof(struct TreeNode)); 22 if (!BT) 23 exit(0); 24 (*BT)->Num = ch; 25 CreateTree(&(*BT)->Left); 26 CreateTree(&(*BT)->Right); 27 28 } 29 30 31 return true; 32 } 33 34 void PreOrderTraversal(BinTree BT) 35 { 36 //递归实现先序遍历 37 //if (BT) 38 //{ 39 // if (BT->Left == NULL&&BT->Right == NULL) 40 // printf("%c ", BT->Num); 41 // PreOrderTraversal(BT->Left); 42 // PreOrderTraversal(BT->Right); 43 //} 44 45 46 //非递归实现先序遍历 47 48 BinTree T = BT; 49 Stack S = CreateStack(); 50 while (T || !IsEmpty(S)){ 51 while (T){ 52 printf("%c", T->Num); 53 Push(S, T); 54 T = T->Left; 55 } 56 if (!IsEmpty(S)){ 57 T = Pop(S); 58 T = T->Right; 59 } 60 } 61 } 62 void InOrderTraversal(BinTree BT) 63 { 64 //递归实现中序遍历 65 /*if (BT) 66 { 67 InOrderTraversal(BT->Left); 68 printf("%c", BT->Num); 69 InOrderTraversal(BT->Right); 70 }*/ 71 72 //非递归实现 73 BinTree T = BT; 74 Stack S = CreateStack(); 75 while (T || !IsEmpty(S)){ 76 while (T){ 77 Push(S, T); 78 T = T->Left; 79 } 80 if (!IsEmpty(S)){ 81 T = Pop(S); 82 printf("%c", T->Num); 83 T = T->Right; 84 } 85 } 86 } 87 void PostOrderTraversal(BinTree BT) 88 { 89 //递归实现后序遍历 90 if (BT) 91 { 92 PostOrderTraversal(BT->Left); 93 PostOrderTraversal(BT->Right); 94 printf("%c", BT->Num); 95 } 96 97 //非递归实现 98 //没写 99 } 100 101 void LevelTraversal(BinTree BT) 102 { 103 //层次遍历,栈实现 104 /*BinTree T = BT; 105 Stack S = CreateStack(); 106 if(!BT) 107 return; 108 Push(S, T); 109 while (!IsEmpty(S)){ 110 T = Pop(S); 111 printf("%c", T->Num); 112 if (T->Right != NULL) 113 Push(S, T->Right); 114 if (T->Left != NULL) 115 Push(S, T->Left); 116 117 } 118 DestroyStack(S);*/ 119 120 //层次遍历,队列实现 121 BinTree T = BT; 122 Queue Q = CreateQueue(); 123 if (!T) 124 return; 125 AddQ(Q, T); 126 while (!IsEmpty(Q)){ 127 T = DeleteQ(Q); 128 printf("%c", T->Num); 129 if (T->Left != NULL) 130 AddQ(Q, T->Left); 131 if (T->Right != NULL) 132 AddQ(Q, T->Right); 133 } 134 DestroyQueue(Q); 135 136 } 137 //后序遍历实现树高 138 int PostOrderGetHeight(BinTree BT) 139 { 140 int HL, HR, MaxH; 141 if (BT){ 142 HL = PostOrderGetHeight(BT->Left); 143 HR = PostOrderGetHeight(BT->Right); 144 MaxH = (HL > HR)?HL:HR; //根最后还要执行一次 145 return (MaxH + 1); 146 } 147 148 else 149 return 0; 150 } 151 int main() 152 { 153 BinTree BT ; 154 155 CreateTree(&BT); 156 PreOrderTraversal(BT); 157 printf("\n\n"); 158 InOrderTraversal(BT); 159 160 printf("\n\n"); 161 PostOrderTraversal(BT); 162 163 printf("\n\n"); 164 LevelTraversal(BT); 165 166 167 putchar(‘\n‘); 168 printf("%d", PostOrderGetHeight(BT)); 169 system("pause"); 170 return 0; 171 172 } 173 174
1 //栈实现 2 3 #include "BinTree.h" 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 Stack CreateStack() 8 { 9 Stack S; 10 S = (Stack)malloc(sizeof(struct SNode)); 11 S->Next = NULL; 12 return S; 13 } 14 15 bool Push(Stack S,BinTree BT) 16 { 17 Stack tmp; 18 tmp = (Stack)malloc(sizeof(struct SNode)); 19 tmp->BT = BT; 20 tmp->Next= S->Next; 21 S->Next = tmp; 22 23 return true; 24 } 25 bool IsEmpty(Stack S) 26 { 27 return S->Next == NULL; 28 } 29 30 BinTree Pop(Stack S) 31 { 32 if (IsEmpty(S)) 33 exit(0); 34 Stack tmp; 35 BinTree T; 36 tmp = S->Next; 37 S->Next = tmp->Next; 38 T = tmp->BT; 39 free(tmp); 40 return T; 41 } 42 43 bool DestroyStack(Stack S) 44 { 45 while (!IsEmpty(S)) 46 Pop(S); 47 free(S); 48 return true; 49 }
1 //队列实现 2 3 #include "BinTree.h" 4 #include <stdio.h> 5 #include <stdlib.h> 6 7 Queue CreateQueue() 8 { 9 Queue Q; 10 Q = (Queue)malloc(sizeof(struct QNode)); 11 Q->Rear=(Position)malloc(sizeof(struct Node)); 12 Q->Rear->Next = NULL; 13 Q->Front = Q->Rear; 14 return Q; 15 } 16 17 bool AddQ(Queue Q, BinTree BT) 18 { 19 Position tmp; 20 tmp = (Position)malloc(sizeof(struct Node)); 21 tmp->BT = BT; 22 tmp->Next = NULL; 23 Q->Rear->Next = tmp; 24 Q->Rear = tmp; 25 26 return true; 27 } 28 bool IsEmpty(Queue Q) 29 { 30 return Q->Front->Next == NULL; 31 } 32 33 BinTree DeleteQ(Queue Q) 34 { 35 Position tmp; 36 BinTree BT; 37 tmp = Q->Front->Next; 38 if (Q->Front->Next == Q->Rear) 39 Q->Rear = Q->Front; //关键 Q->Rrear必须指回头节点 40 Q->Front->Next = tmp->Next; 41 BT = tmp->BT; 42 43 free(tmp); 44 return BT; 45 } 46 47 bool DestroyQueue(Queue Q) 48 { 49 if (!IsEmpty(Q)){ 50 DeleteQ(Q); 51 } 52 free(Q->Front); 53 Q->Front =Q->Rear= NULL; 54 return true; 55 }
第一次写博客,不知道怎么写,就随便写吧,希望坚持吧。
以上是关于树的各种遍历(c 语言实现)的主要内容,如果未能解决你的问题,请参考以下文章