目录
介绍:
本篇文章仅仅介绍c语言搜索二叉树各种功能代码 不提供讲解 各位见谅
代码:
写代码前提:
写代码之前先看一看 完成c语言搜索二叉树各种功能 所需要的头文件和结构体
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* left;
struct node* right;
}node;
向二叉树中插入数据代码:
node* insert_data(node* tree_root, int data)
{
node* newnode = (node*)malloc(sizeof(node));
node* m = tree_root;
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
if (tree_root == NULL)
{
tree_root = newnode;
return tree_root;
}
while (m != NULL)
{
if (data < m->data)
{
if (m->left == NULL)
{
m->left = newnode;
break;
}
else
{
m = m->left;
}
}
else
{
if (m->right == NULL)
{
m->right = newnode;
break;
}
else
{
m = m->right;
}
}
}
return tree_root;
}
获取二叉树的高度代码:
int tree_high(node* tree_root)
{
if (tree_root == NULL)
{
return 0;
}
else
{
int left_high = tree_high(tree_root->left);
int right_high = tree_high(tree_root->right);
int high = left_high;
if (right_high > high)
{
high = right_high;
}
return high + 1;
}
}
获取二叉树中最大的值代码:
int tree_maximum(node* tree_root)
{
if (tree_root == NULL)
{
return -1;
}
else
{
int left_max = tree_maximum(tree_root->left);
int right_max = tree_maximum(tree_root->right);
int maximum = tree_root->data;
if (maximum < left_max)
{
maximum = left_max;
}
if (maximum < right_max)
{
maximum = right_max;
}
return maximum;
}
}
获取二叉树中最小的值代码:
int tree_minimum(node* tree_root)
{
if (tree_root == NULL)
{
return 10000;
}
else
{
int left_min = tree_minimum(tree_root->left);
int right_min = tree_minimum(tree_root->right);
int minimum = tree_root->data;
if (minimum > left_min)
{
minimum = left_min;
}
if (minimum > right_min)
{
minimum = right_min;
}
return minimum;
}
}
删除二叉树中的节点代码:
node* delete_node(node* tree_root, int data)
{
if (tree_root == NULL)
{
printf("The tree is NULL\n");
return NULL;
}
node* p = tree_root;
node* fp = NULL;
while (1)
{
fp = p;
if (data > p->data)
{
if (p->right == NULL)
{
printf("The tree haven't the number\n");
return tree_root;
}
p = p->right;
}
else if (data < p->data)
{
if (p->left == NULL)
{
printf("The tree haven't the number\n");
return tree_root;
}
p = p->left;
}
if (p->data == data)
{
break;
}
}
if (p->left != NULL && p->right != NULL)
{
node* q = p;
fp = p;
p = p->right;
while (p->left != NULL)
{
fp = p;
p = p->left;
}
q->data = p->data;
if (fp->right == p)
{
fp->right = p->right;
free(p);
p = NULL;
}
else if (fp->left == p)
{
fp->left = p->right;
free(p);
p = NULL;
}
return tree_root;
}
else if (p->left == NULL && p->right == NULL)
{
if (p == tree_root)
{
free(p);
p = NULL;
return NULL;
}
else if (fp->left == p)
{
fp->left = NULL;
free(p);
p = NULL;
}
else if (fp->right == p)
{
fp->right = NULL;
free(p);
p = NULL;
}
return tree_root;
}
else if (p->left != NULL && p->right == NULL)
{
if (p == tree_root)
{
tree_root = p->left;
p->left = NULL;
free(p);
return tree_root;
}
else if (fp->left == p)
{
fp->left = p->left;
p->left = NULL;
free(p);
p = NULL;
}
else if (fp->right == p)
{
fp->right = p->left;
p->left = NULL;
free(p);
p = NULL;
}
return tree_root;
}
else if (p->left == NULL && p->right != NULL)
{
if (p == tree_root)
{
tree_root = p->right;
p->right = NULL;
free(p);
return tree_root;
}
if (fp->left == p)
{
fp->left = p->right;
p->right = NULL;
free(p);
p = NULL;
}
if (fp->right == p)
{
fp->right = p->right;
p->right = NULL;
free(p);
p = NULL;
}
return tree_root;
}
}
判断二叉树是否为空代码:
int empty(node* tree_root)
{
if (tree_root == NULL)
{
printf("The tree is NULL\n");
return 0;
}
else
{
return 1;
}
}
二叉树前序遍历代码:
void preorder(node* tree_root)
{
if (tree_root != NULL)
{
printf("%d ", tree_root->data);
preorder(tree_root->left);
preorder(tree_root->right);
}
}
二叉树中序遍历代码:
void inorder(node* tree_root)
{
if (tree_root != NULL)
{
inorder(tree_root->left);
printf("%d ", tree_root->data);
inorder(tree_root->right);
}
}
二叉树后序遍历代码:
void taorder(node* tree_root)
{
if (tree_root != NULL)
{
taorder(tree_root->left);
taorder(tree_root->right);
printf("%d ", tree_root->data);
}
}
全部代码:
这个全部代码包括小编自己训练的部分 大家应该有用:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node* left;
struct node* right;
}node;
node* insert_data(node* tree_root, int data);
void preorder(node* tree_root);
void inorder(node* tree_root);
void taorder(node* tree_root);
int tree_high(node* tree_root);
int tree_maximum(node* tree_root);
int tree_minimum(node* tree_root);
node* delete_node(node* tree_root, int data);
int empty(node* tree_root);
int main(void)
{
int a[16] = { 7,6,4,3,16,15,13,5,25,30,24,32,23 ,38,36,41 };
int tree_high1;
int tree_maximum1;
int tree_minimum1;
node* tree_root = NULL;
for (int i = 0; i < 16; i++)
{
tree_root = insert_data(tree_root, a[i]);
}
if (empty(tree_root))
{
printf("The tree's preorder is : ");
preorder(tree_root);
putchar('\n');
}
if (empty(tree_root))
{
printf("The tree's inorder is : ");
inorder(tree_root);
putchar('\n');
}
if (empty(tree_root))
{
printf("The tree's taorder is : ");
taorder(tree_root);
putchar('\n');
}
if (empty(tree_root))
{
tree_high1 = tree_high(tree_root);
printf("The tree's high = %d", tree_high1);
putchar('\n');
}
if (empty(tree_root))
{
tree_maximum1 = tree_maximum(tree_root);
printf("The tree's maximum = %d", tree_maximum1);
putchar('\n');
}
if (empty(tree_root))
{
tree_minimum1 = tree_minimum(tree_root);
printf("The tree's minimum = %d", tree_minimum1);
putchar('\n');
}
for (int i = 0; i < 16; i++)
{
tree_root = delete_node(tree_root, a[i]);
}
if (empty(tree_root))
{
printf("The delete' tree is : ");
inorder(tree_root);
putchar('\n');
}
return 0;
}
node* insert_data(node* tree_root, int data)
{
node* newnode = (node*)malloc(sizeof(node));
node* m = tree_root;
newnode->data = data;
newnode->left = NULL;
newnode->right = NULL;
if (tree_root == NULL)
{
tree_root = newnode;
return tree_root;
}
while (m != NULL)
{
if (data < m->data)
{
if (m->left == NULL)
{
m->left = newnode;
break;
}
else
{
m = m->left;
}
}
else
{
if (m->right == NULL)
{
m->right = newnode;
break;
}
else
{
m = m->right;
}
}
}
return tree_root;
}
void preorder(node* tree_root)
{
if (tree_root != NULL)
{
printf("%d ", tree_root->data);
preorder(tree_root->left);
preorder(tree_root->right);
}
}
void inorder(node* tree_root)
{
if (tree_root != NULL)
{
inorder(tree_root->left);
printf("%d ", tree_root->data);
inorder(tree_root->right);
}
}
void taorder(node* tree_root)
{
if (tree_root != NULL)
{
taorder(tree_root->left);
taorder(tree_root->right);
printf("%d ", tree_root->data);
}
}
int tree_high(node* tree_root)
{
if (tree_root == NULL)
{
return 0;
}
else
{
int left_high = tree_high(tree_root->left);
int right_high = tree_high(tree_root->right);
int high = left_high;
if (right_high > high)
{
high = right_high;
}
return high + 1;
}
}
int tree_maximum(node* tree_root)
{
if (tree_root == NULL)
{
return -1;
}
else
{
int left_max = tree_maximum(tree_root->left);
int right_max = tree_maximum(tree_root->right);
int maximum = tree_root->data;
if (maximum < left_max)
{
maximum = left_max;
}
if (maximum < right_max)
{
maximum = right_max;
}
return maximum;
}
}
int tree_minimum(node* tree_root)
{
if (tree_root == NULL)
{
return 10000;
}
else
{
int left_min = tree_minimum(tree_root->left);
int right_min = tree_minimum(tree_root->right);
int minimum = tree_root->data;
if (minimum > left_min)
{
minimum = left_min;
}
if (minimum > right_min)
{
minimum = right_min;
}
return minimum;
}
}
node* delete_node(node* tree_root, int data)
{
if (tree_root == NULL)
{
printf("The tree is NULL\n");
return NULL;
}
node* p = tree_root;
node* fp = NULL;
while (1)
{
fp = p;
if (data > p->data)
{
if (p->right == NULL)
{
printf("The tree haven't the number\n");
return tree_root;
}
p = p->right;
}
else if (data < p->data)
{
if (p->left == NULL)
{
printf("The tree haven't the number\n");
return tree_root;
}
p = p->left;
}
if (p->data == data)
{
break;
}
}
if (p->left != NULL && p->right != NULL)
{
node* q = p;
fp = p;
p = p->right;
while (p->left != NULL)
{
fp = p;
p = p->left;
}
q->data = p->data;
if (fp->right == p)
{
fp->right = p->right;
free(p);
p = NULL;
}
else if (fp->left == p)
{
fp->left = p->right;
free(p);
p = NULL;
}
return tree_root;
}
else if (p->left == NULL && p->right == NULL)
{
if (p == tree_root)
{
free(p);
p = NULL;
return NULL;
}
else if (fp->left == p)
{
fp->left = NULL;
free(p);
p = NULL;
}
else if (fp->right == p)
{
fp->right = NULL;
free(p);
p = NULL;
}
return tree_root;
}
else if (p->left != NULL && p->right == NULL)
{
if (p == tree_root)
{
tree_root = p->left;
p->left = NULL;
free(p);
return tree_root;
}
else if (fp->left == p)
{
fp->left = p->left;
p->left = NULL;
free(p);
p = NULL;
}
else if (fp->right == p)
{
fp->right = p->left;
p->left = NULL;
free(p);
p = NULL;
}
return tree_root;
}
else if (p->left == NULL && p->right != NULL)
{
if (p == tree_root)
{
tree_root = p->right;
p->right = NULL;
free(p);
return tree_root;
}
if (fp->left == p)
{
fp->left = p->right;
p->right = NULL;
free(p);
p = NULL;
}
if (fp->right == p)
{
fp->right = p->right;
p->right = NULL;
free(p);
p = NULL;
}
return tree_root;
}
}
int empty(node* tree_root)
{
if (tree_root == NULL)
{
printf("The tree is NULL\n");
return 0;
}
else
{
return 1;
}
}
结尾:
还是那句话 大家不要直接粘贴复制 要自己动手敲一下 动动脑子思考一下
大家加油吧
散会
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!