C語言數據結構之二叉樹的實現


本篇博文是博主在學習C語言算法與數據結構的一些應用代碼實例,給出了以二叉鏈表的形式實現二叉樹的相關操作。如創建,遍歷(先序,中序后序遍歷),求樹的深度,樹的葉子節點數,左右兄弟,父節點。

代碼清單如下:

  1 #pragma once
  2 #include<stdio.h>
  3 #include"stdlib.h"
  4 typedef char TElemType;
  5 typedef struct BiTNode
  6 {
  7     TElemType data;
  8     struct BiTNode *lchild, *rchild;
  9 
 10 }BiTNode, *BiTree;
 11 void InitBiTree(BiTree&T)
 12 {
 13     T = NULL;
 14 }
 15 
 16 //創建二叉樹
 17 int CreateBiTree(BiTree&T)
 18 {
 19     printf("請輸入樹的節點:");
 20     TElemType ch;
 21     getchar();
 22     scanf("%c", &ch);
 23     if (ch == ' ')
 24     {
 25         T = NULL;
 26 
 27     }
 28     else
 29     {
 30         T = (BiTNode*)malloc(sizeof(BiTNode));
 31         if (!T)
 32         {
 33             exit(EOVERFLOW);
 34         }
 35         T->data = ch;
 36         CreateBiTree(T->lchild);
 37         CreateBiTree(T->rchild);
 38     }
 39     return true;
 40 }
 41 
 42 //判斷二叉樹是否為空
 43 int BiTreeEmpty(BiTree&T)
 44 {
 45     if (T)
 46     {
 47         return false;
 48     }
 49     else
 50     {
 51         return true;
 52     }
 53 }
 54 
 55 //求樹的深度
 56 int BiTreeDepth(BiTree T)
 57 {
 58     int i, j;
 59     if (!T)
 60     {
 61         return 0;
 62     }
 63     if (T->lchild)
 64     {
 65         i = BiTreeDepth(T->lchild);
 66     }
 67     else
 68     {
 69         i = 0;
 70     }
 71     if (T->rchild)
 72     {
 73         j = BiTreeDepth(T->rchild);
 74     }
 75     else
 76     {
 77         j = 0;
 78     }
 79     return i > j ? i + 1 : j + 1;
 80 }
 81 
 82 //打印節點
 83 void Visit(TElemType e)
 84 {
 85     printf("%c", e);
 86 }
 87 
 88 //以遞歸形式先序遍歷二叉樹 
 89 void PreOrderTraverse(BiTree T, void(*Visit)(TElemType))
 90 {
 91     if (T)
 92     {
 93         Visit(T->data);
 94         PreOrderTraverse(T->lchild, Visit);
 95         PreOrderTraverse(T->rchild, Visit);
 96     }
 97 }
 98 //以遞歸形式中序遍歷二叉樹 
 99 void InOrderTraverse(BiTree T, void(*Visit)(TElemType))
100 {
101     if (T)
102     {
103         InOrderTraverse(T->lchild, Visit);
104         Visit(T->data);
105         InOrderTraverse(T->rchild, Visit);
106     }
107 }
108 
109 //以遞歸形式后序遍歷二叉樹 
110 void PostOrderTraverse(BiTree T, void(*Visit)(TElemType))
111 {
112     if (T)
113     {
114         PostOrderTraverse(T->lchild, Visit);
115         PostOrderTraverse(T->rchild, Visit);
116         Visit(T->data);
117     }
118 }
119 
120 //計算葉子節點數
121 int CountLeaf(BiTree &T, int &count)
122 {
123     if (T)
124     {
125 
126         if ((T->lchild == NULL) && (T->rchild == NULL))
127         {
128 
129             count++;
130         }
131         CountLeaf(T->lchild, count);
132         CountLeaf(T->rchild, count);
133     }
134     return count;    //返回葉子節點數
135 }
136 int max(int a, int b)
137 {
138     return a > b ? a : b;
139 }
140 
141 //求父節點的值 注意返回值為char類型
142 char Parent(BiTree T, ElemType e) //二叉樹存在,e是T中某個節點,若e為非根結點,則返回它的雙親,否則返回空
143 {
144 
145     if (T != NULL)
146     {
147         if ((T->lchild && (T->lchild->data == e)) || (T->rchild && (T->rchild->data == e)))
148             return(T->data);
149         else
150         {
151             Parent(T->lchild, e);
152             Parent(T->rchild, e);
153         }
154     }
155     return ' ';
156 
157 }
158 
159 //尋找某個節點 
160 BiTree FindNode(BiTree T, char e)  //返回指向這個節點的指針
161 {
162     if (T == NULL)
163     {
164         return NULL;
165     }
166     if (T->data == e)
167     {
168         return T;
169     }
170     BiTree node;
171     if ((node = FindNode(T->lchild, e)) != NULL)
172         return node;
173     else
174         return FindNode(T->rchild, e);
175 }
176 
177 //輸出某個節點的左兄弟
178 void LeftSibling(BiTree &T, char e)
179 {
180     char ch = Parent(T, e);
181     BiTree tmp = FindNode(T, ch);
182     if (tmp->lchild != NULL)
183     {
184         if (tmp->lchild->data != e)
185         {
186             printf("此節點的左兄弟為:%c ", tmp->lchild->data);
187         }
188         else
189         {
190             printf("此節點沒有左兄弟 ");
191         }
192         //return tmp->lchild->data;
193     }
194     else
195     {
196         printf("此節點沒有左兄弟 ");
197 
198     }
199 }
200 
201 //輸出某個節點的左兄弟
202 void RightSibling(BiTree &T, char e)
203 {
204     char ch = Parent(T, e);
205     BiTree tmp = FindNode(T, ch);
206     if (tmp->rchild != NULL)
207     {
208         if (tmp->rchild->data != e)
209             printf("此節點的右兄弟為:%c \n", tmp->rchild->data);
210         //return tmp->rchild->data;
211         else
212         {
213             printf("此節點沒有右兄弟 ");
214         }
215     }
216     else
217     {
218         printf("此節點沒有右兄弟 ");
219 
220     }
221 }


 


免責聲明!

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



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