C and C++ implementation tree operation instance code

  • 2020-07-21 09:29:58
  • OfStack

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


// Specifies what the function does: returns the number of nodes passed into the tree 
// Fixed tail head: tail: when passing in the node tail NULL when   Head: 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 with num node data


// Specify function function: return node data is num Number of nodes 
// Set the tail Head: Tail: NULL  Head: 1 + func( On the left ) + func (right).  //  or  func( On the 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);
 }
}

Find the sum of the node data in the tree


// What this function does: Returns the sum 
// Set the tail Head: Tail: NULL  Head: root-> elem + func( On the left ) + func (right). 
int add(tree t)
{
 if (t == NULL)
 return 0;
 else
 return t->elem + add(t->left) + add(t->right);
}

Determine if there are nodes in the tree with data of num


// Two ways: 1 The seed is the end that can accomplish its purpose, 1 The species needs to be traversed completely before it ends 
// Define function function: determine whether the value is num Node return of 1 or 0
// Fixed tail head: 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: according to num With or without, return 0/ non 0
// Set the tail Head: Tail: NULL  Head: :return 1 + func( On the left )+func( right )  No: func( On the 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;
}

Calculate the number of num values


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


// Explicit function function: print trunk
// Going to end   Tail: NULL  Head: the first 1 The step is to determine if the node is a tree trunk and then print it func (left) to print the left trunk  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);
}

Determine if 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));
}

Print the number of layers of a value in the tree


// Specify function functions: Find the number of layers into the number and print 
// To determine the tail: // Find a node with a specific value   find NULL  Head: If so, print. If not, go to the left and right subtrees layer++ ", when the child had finished searching layer--
bool flag = false; //flag The tag can be used to prematurely end recursion 
void getTreeLayer(Node * root, int num, int &layer)
{
 if (root == NULL) return;
 if (flag == true) return;
 if (root->data == num) { 
 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


// Specifies what the function does: returns the number of nodes passed into the tree 
// Fixed tail head: tail: when passing in the node tail NULL when   Head: 1 + count(t->left) + count(t->right)
int count(tree t)
{
 if (t == NULL) return 0;
 return 1 + count(t->left) + count(t->right);
}
0

conclusion

Above is the site to introduce C/C++ tree operation example code, I hope to help you!


Related articles: