Example code of tree operation in C / C + +

Time:2021-12-27

Preprocessing command


#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;

Calculate the number of nodes in the tree

//Specify the function: return the number of nodes in the incoming tree
//Fixed tail header: Tail: when the incoming node tail is null, the header: 1 + count (T - > left) + count (T - > right)
int count(tree t)
{
 if (t == NULL) return 0;
 return 1 + count(t->left) + count(t->right);
}

Find the number of nodes in the tree whose node data is num

//Explicit function function: returns the number of nodes whose node data is num
//Fix the tail head: Tail: null head: 1 + func (left) + func (right) // or func (left) + func (right)
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);
 }
}

Sum the node data in the tree

//Explicit function function: return sum
//Fixed tail head: Tail: null head: Root - > elem + func (left) + func (right)
int add(tree t)
{
 if (t == NULL)
 return 0;
 else
 return t->elem + add(t->left) + add(t->right);
}

Judge whether there are nodes with num data in the tree

//There are two ways: one is to end when the goal can be achieved, and the other is to complete traversal
//Clear function function: judge whether there is a node with value of num and return 1 or 0
//Fix the tail: the tail: value is num, the head:
int inTree_1(tree t, elemtype num)
{
 if (t->elem == num)
 return TRUE;
 else
 {
 if (t->left != NULL)
 intree(t->left, num); //  Use recursion to pass it to child nodes
 if (t->right != NULL)
 intree(t->right, num);
 }
 return FALSE;
}
//Determine function function: return 0 / non-0 according to the presence or absence of num
//Fixed tail head: Tail: null head: Yes: Return 1 + func (left) + func (right) none: func (left) + func (right)
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;
}

The number of calculated values is num


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;
}

Print trunk

//Clear function function: print trunk
//Fix the tail: null head: the first step is to judge whether this node is a trunk, and then print, then func (left) to print the trunk on the left, and func (right) to print the trunk on the right
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);
}

Judge whether the two trees are the same


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;
}

Find the height of the tree


#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));
}

Prints the number of levels of a value in the tree

//Clear function function: find the number of layers and print
//Determine the tail: // find the node with a specific value and find the null header: If yes, print. If not, go to the left and right subtrees to find layer + +. When the child has no layer after searching--
bool flag = false; // Flag flag can be used to end recursion ahead of time
void getTreeLayer(Node * root, int num, int &layer)
{
 if (root == NULL) return;
 if (flag == true) return;
 if (root->data == num) { 
 The number of layers of cout < < num value < < num < < "is:" < layer < < endl; 
 flag = true; 
 return; 
 }
 layer++;
 getTreeLayer(root->lChild, num);
 getTreeLayer(root->rChild, num);
 layer--;
}

Find the path of the node

vector<int> path;
bool flag = false; // Flag flag can be used to end recursion ahead of time
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();
}

summary

The above is the example code of C / C + + tree operation introduced by Xiaobian. I hope it will be helpful to you!