0% found this document useful (0 votes)
10 views5 pages

Bai Tap Tree

The document contains C code for managing a binary tree, including functions for inserting nodes, printing values in different orders, and counting various types of nodes. It also includes functionality to find specific values, determine the depth of the tree, and print nodes at a specific level. Additionally, there are functions to count nodes with one child and leaf nodes.

Uploaded by

Đại Phát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views5 pages

Bai Tap Tree

The document contains C code for managing a binary tree, including functions for inserting nodes, printing values in different orders, and counting various types of nodes. It also includes functionality to find specific values, determine the depth of the tree, and print nodes at a specific level. Additionally, there are functions to count nodes with one child and leaf nodes.

Uploaded by

Đại Phát
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

typedef struct node{

int data;
struct node* left;
struct node* right;
}node;

int insert_node(node* &tree, int data){


if(tree != NULL){
if(tree->data == data)
return 0;
else{
if(data > tree->data)
insert_node(tree->right, data);
else
insert_node(tree->left, data);
}
}
else{
tree = (node*)malloc(sizeof(node));
if(tree == NULL)
return -1;
tree->data = data;
tree->left = tree->right = NULL;
return 1;
}
return 1;
}

void LNR_print(node* tree){


if(tree != NULL){
LNR_print(tree->left);
printf("%d ", tree->data);
LNR_print(tree->right);
}
}

void NLR_print(node* tree){


if(tree != NULL){
printf("%d ", tree->data);
NLR_print(tree->left);
NLR_print(tree->right);
}
}

void input_tree(node* &tree){


int check = 1, data;
do{
printf("Nhap gia tri node: ");
scanf("%d", &data); getchar();
check = insert_node(tree, data);
}while(check == 1);
}

void LNR_print_even_value(node* tree){


if(tree != NULL){
LNR_print_even_value(tree->left);
if(tree->data % 2 == 0){
printf("%d ", tree->data);
}
LNR_print_even_value(tree->right);
}
}

void count_node(node* tree, int &count){


if(tree != NULL){
count_node(tree->left, count);
count++;
count_node(tree->right, count);
}
}

void find_x(node* tree, int x){


if(tree == NULL)
printf("Khong tim thay x");
else{
if(x == tree->data){
printf("X co trong cay");
}
else if(x > tree->data)
find_x(tree->right, x);
else
find_x(tree->left, x);
}
}

void find_min_max(node* tree, int &min, int &max){


if(tree != NULL){
if(min > tree->data){
min = tree->data;
}
if(max < tree->data){
max = tree->data;
}
find_min_max(tree->left, min, max);
find_min_max(tree->right, min, max);
}
}

int max_depth(node* tree){


if(tree == NULL)
return -1;
else{
if(max_depth(tree->left) > max_depth(tree-
>right))
return max_depth(tree->left) + 1;
else
return max_depth(tree->right) + 1;
}
}

void leaf_node_count(node* tree, int &count){


if(tree != NULL){
if(tree->left == NULL && tree->right == NULL)
count ++;
leaf_node_count(tree->left, count);
leaf_node_count(tree->right, count);
}
}

void rank1_node_count(node* tree, int &count){


if(tree != NULL){
if(tree->left != NULL && tree->right == NULL ||
tree->left == NULL && tree->right != NULL)
count++;
rank1_node_count(tree->left, count);
rank1_node_count(tree->right, count);
}
}

void rank1_node_count_right(node* tree, int &count){


if(tree != NULL){
if(tree->left == NULL && tree->right != NULL)
count++;
rank1_node_count(tree->left, count);
rank1_node_count(tree->right, count);
}
}

void rank1_node_count_left(node* tree, int &count){


if(tree != NULL){
if(tree->left != NULL && tree->right == NULL)
count++;
rank1_node_count(tree->left, count);
rank1_node_count(tree->right, count);
}
}

void rank2_node_count(node* tree, int &count){


if(tree != NULL){
if(tree->left != NULL && tree->right != NULL)
count++;
rank1_node_count(tree->left, count);
rank1_node_count(tree->right, count);
}
}

void specific_level_node_print(node* tree, int level){


if(tree != NULL){
if(level == 0)
printf("%d ", tree->data);
level--;
specific_level_node_print(tree->left, level);
specific_level_node_print(tree->right, level);
}
}

void lenght_root_to_x(node* tree, int x, int length){


if(tree != NULL){
if(x == tree->data)
printf("%d ", length);
else if(x < tree->data){
length++;
lenght_root_to_x(tree ->left,x, length);
}
else{
length++;
lenght_root_to_x(tree -> right, x, length);
}
}
}

You might also like