C/C++實現(xiàn)樹操作的實例代碼
更新時間:2020年02月12日 15:22:13 作者:我的霹靂貓阿洛
這篇文章主要介紹了C/C++實現(xiàn)樹操作的實例代碼,代碼簡單易懂,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下
預處理命令
#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é)點個數(shù)
//明確函數(shù)的功能:返回傳入樹的節(jié)點個數(shù)
//定好尾頭:尾:當傳入的節(jié)點尾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é)點數(shù)據(jù)為num的節(jié)點個數(shù)
//明確函數(shù)功能:返回節(jié)點數(shù)據(jù)為num的節(jié)點個數(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é)點數(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é)點
//兩種方式:一種是可以達成目的就結束,一種是需要遍歷完全才結束
//明確函數(shù)功能:判斷其中有沒有值為num的節(jié)點返回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é)點
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é)點是否是樹干然后打印,再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é)點 找到NULL 頭:若是則打印,若不是則去左右子樹尋找layer++,當孩子尋找完都沒有時layer--
bool flag = false; //flag標記可以用于提前結束遞歸
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é)點的路徑
vector<int> path;
bool flag = false; //flag標記可以用于提前結束遞歸
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();
}
總結
以上所述是小編給大家介紹的C/C++實現(xiàn)樹操作的實例代碼,希望對大家有所幫助!
相關文章
C++ 實現(xiàn)求最大公約數(shù)和最小公倍數(shù)
這篇文章主要介紹了c++ 實現(xiàn)求最大公約數(shù)和最小公倍數(shù)的相關資料,需要的朋友可以參考下2017-05-05

