C/C++實(shí)現(xiàn)樹操作的實(shí)例代碼
預(yù)處理命令
#include <stdio.h> #include <stdlib.h> #define TRUE 1 #define FALSE 0 typedef int elemtype; typedef struct tNode* tree; typedef struct tNode { elemtype elem; tree left; tree right; }tNode;
計算樹的節(jié)點(diǎn)個數(shù)
//明確函數(shù)的功能:返回傳入樹的節(jié)點(diǎn)個數(shù) //定好尾頭:尾:當(dāng)傳入的節(jié)點(diǎn)尾NULL時 頭:1 + count(t->left) + count(t->right) int count(tree t) { if (t == NULL) return 0; return 1 + count(t->left) + count(t->right); }
求樹中節(jié)點(diǎn)數(shù)據(jù)為num的節(jié)點(diǎn)個數(shù)
//明確函數(shù)功能:返回節(jié)點(diǎn)數(shù)據(jù)為num的節(jié)點(diǎn)個數(shù) //定好尾頭:尾:NULL 頭:1 + func(左) + func(右) // 或者 func(左) + func(右) int count_num(tree t, elemtype num) { if (t == NULL) return 0; else { if (t->elem == num) return 1 + count_num(t->left, num) + count_num(t->right, num); else return count_num(t->left, num) + count_num(t->right, num); } }
求樹中節(jié)點(diǎn)數(shù)據(jù)的總和
//明確函數(shù)功能:返回總和 //定好尾頭:尾:NULL 頭:root-> elem + func(左) + func(右) int add(tree t) { if (t == NULL) return 0; else return t->elem + add(t->left) + add(t->right); }
判斷樹中有無數(shù)據(jù)為num的節(jié)點(diǎn)
//兩種方式:一種是可以達(dá)成目的就結(jié)束,一種是需要遍歷完全才結(jié)束 //明確函數(shù)功能:判斷其中有沒有值為num的節(jié)點(diǎn)返回1或0 //定好尾頭:尾:值為num ,頭: int inTree_1(tree t, elemtype num) { if (t->elem == num) return TRUE; else { if (t->left != NULL) intree(t->left, num); // 使用遞歸將其遞到子節(jié)點(diǎn) if (t->right != NULL) intree(t->right, num); } return FALSE; } //確定函數(shù)功能:根據(jù)num的有無,返回0/非0 //定好尾頭:尾:NULL 頭:有:return 1 + func(左)+func(右) 無:func(左)+func(右) int inTree_2(tree t, elemtype num) { if (t == NULL) return 0; int res; if (t->elem == num) res = 1+ intree(t->left, num) + intree(t->right, num); if (t->elem == num) res = intree(t->left, num) + intree(t->right, num); return res; }
計算值為num的個數(shù)
int count_elem(tree t, elemtype val, int* num) { int val_l, val_r; if (t->left == NULL) return t->elem; if (t->right == NULL) return t->elem; else { val_l = count_elem(t->left, val, num); if (val == val_l) (*num)++; val_r = count_elem(t->right, val, num); if (val == val_r) (*num)++; return t->elem; } return *num; }
打印trunk
//明確函數(shù)功能:打印trunk //定好尾頭 尾:NULL 頭:第一步是判斷本節(jié)點(diǎn)是否是樹干然后打印,再func(左)去打印左邊的樹干 func(右)去打印右邊的樹干 void print_trunk(tree t) { if (t == NULL) return; if (t->right != NULL || t->left != NULL) printf("%d", t->elem); print_trunk(t->right); print_trunk(t->left); }
判斷兩棵樹是否一樣
int same(tree t1, tree t2) { if (count(t1) == count(t2)) { if (t1->elem != t2->elem) return FALSE; if (t1->left != NULL && t2->left != NULL) same(t1->left, t2->left); if (t1->right != NULL && t2->right != NULL) same(t1->right, t2->right); return TRUE; } else return FALSE; }
求樹的高度
#define max(x, y) (x > y) ? x : y int height(tree t) { if (t == NULL)return -1; return 1 + max(height(t->right), height(t->left)); }
打印樹中某值的層數(shù)
//明確函數(shù)功能:尋找放入的數(shù)的層數(shù)并打印 //確定尾://找到特定值的節(jié)點(diǎn) 找到NULL 頭:若是則打印,若不是則去左右子樹尋找layer++,當(dāng)孩子尋找完都沒有時layer-- bool flag = false; //flag標(biāo)記可以用于提前結(jié)束遞歸 void getTreeLayer(Node * root, int num, int &layer) { if (root == NULL) return; if (flag == true) return; if (root->data == num) { cout << "num值" << num << "的層數(shù)為:" << layer << endl; flag = true; return; } layer++; getTreeLayer(root->lChild, num); getTreeLayer(root->rChild, num); layer--; }
求節(jié)點(diǎn)的路徑
vector<int> path; bool flag = false; //flag標(biāo)記可以用于提前結(jié)束遞歸 void getTreeLayer(Node * root, int num, int &layer) { if (root == NULL) return; if (flag == true) return; if (root->data == num) { for(int x : path) cout << x << " "; bool flag = true; return; } path.push_back(); getTreeLayer(root->lChild, num); getTreeLayer(root->rChild, num); path.pop_back(); }
總結(jié)
以上所述是小編給大家介紹的C/C++實(shí)現(xiàn)樹操作的實(shí)例代碼,希望對大家有所幫助!
相關(guān)文章
基于Windows C++ 應(yīng)用程序通用日志組件的使用詳解
眾所周知,在調(diào)試、跟蹤和執(zhí)行應(yīng)用程序的過程中,程序的日志能為這些工作提供大量有價值的運(yùn)行信息。因此,程序的日志對應(yīng)用程序的運(yùn)行、維護(hù)至關(guān)重要2013-05-05C語言中strspn()函數(shù)和strcspn()函數(shù)的對比使用
這篇文章主要介紹了C語言中strspn()函數(shù)和strcspn()函數(shù)的對比使用,strspn是計算屬于字符串的字符數(shù)而strcspn則是判斷不屬于,需要的朋友可以參考下2015-08-08C++基于easyx圖形庫實(shí)現(xiàn)打磚塊游戲
這篇文章主要為大家詳細(xì)介紹了C++基于easyx圖形庫實(shí)現(xiàn)打磚塊游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-05-05探究C++中string類的實(shí)現(xiàn)原理以及擴(kuò)展使用
這篇文章主要介紹了C++中string類的實(shí)現(xiàn)原理以及擴(kuò)展使用,從內(nèi)存分配角度進(jìn)行了深入探究,需要的朋友可以參考下2015-12-12