算法导论 二叉排序树
内容简单,源码走起,若有疑问留下评论,
#include<stdio.h>
#include<malloc.h>
#define N 1000
#define K 100
typedef struct link S_L;
struct link {
int n;
S_L* left;
S_L* right;
};
void tree(S_L* p, int n);//二叉树的根
int tree_link(S_L* p, int n);//二叉树递归
int printf_tree(S_L*p);//中序遍历打印二叉树
void happen_rand(int* p, int n);//生成随机数
int layer=1;
int LAYER[N]={0};
int main()
{
int array[N] = {0}, i;
happen_rand(array,N);
S_L P = {0};
for (i = 0; i < N; i++) {//二叉树插入元素
tree(&P, array[i]);
}
printf_tree(&P);
return 0;
}
void tree(S_L* p, int n) {
if (p->n == 0) {
p->n = n;
p->left = NULL;
p->right = NULL;
}
else
tree_link(p, n);
}
int tree_link(S_L* p, int n) {
if (n < p->n && p->left == NULL) {
p->left = (S_L*)malloc(sizeof(S_L));
p->left->n = n;
p->left->left = NULL;
p->left->right = NULL;
return 0;
}
else if (n >= p->n && p->right == NULL) {
p->right = (S_L*)malloc(sizeof(S_L));
p->right->n = n;
p->right->left = NULL;
p->right->right = NULL;
return 0;
}
else if (n < p->n)
tree_link(p->left,n);
else if (n >= p->n)
tree_link(p->right, n);
}
int printf_tree(S_L*p){
if((p->left==NULL)&&(p->right==NULL))
{
printf("%d\n",p->n);
return 0;
}
else if((p->left!=NULL)&&(p->right==NULL))
{
printf_tree(p->left);
printf("%d\n",p->n);
return 0;
}
else if((p->left==NULL)&&(p->right!=NULL))
{
printf("%d\n",p->n);
printf_tree(p->right);
}
else if((p->left!=NULL)&&(p->right!=NULL))
{
printf_tree(p->left);
printf("%d\n",p->n);
printf_tree(p->right);
}
}
void happen_rand(int* p, int n){
int i;
for (i = 0; i < N; i++) {
p[i] = rand() % K;
}
}
#include<malloc.h>
#define N 1000
#define K 100
typedef struct link S_L;
struct link {
int n;
S_L* left;
S_L* right;
};
void tree(S_L* p, int n);//二叉树的根
int tree_link(S_L* p, int n);//二叉树递归
int printf_tree(S_L*p);//中序遍历打印二叉树
void happen_rand(int* p, int n);//生成随机数
int layer=1;
int LAYER[N]={0};
int main()
{
int array[N] = {0}, i;
happen_rand(array,N);
S_L P = {0};
for (i = 0; i < N; i++) {//二叉树插入元素
tree(&P, array[i]);
}
printf_tree(&P);
return 0;
}
void tree(S_L* p, int n) {
if (p->n == 0) {
p->n = n;
p->left = NULL;
p->right = NULL;
}
else
tree_link(p, n);
}
int tree_link(S_L* p, int n) {
if (n < p->n && p->left == NULL) {
p->left = (S_L*)malloc(sizeof(S_L));
p->left->n = n;
p->left->left = NULL;
p->left->right = NULL;
return 0;
}
else if (n >= p->n && p->right == NULL) {
p->right = (S_L*)malloc(sizeof(S_L));
p->right->n = n;
p->right->left = NULL;
p->right->right = NULL;
return 0;
}
else if (n < p->n)
tree_link(p->left,n);
else if (n >= p->n)
tree_link(p->right, n);
}
int printf_tree(S_L*p){
if((p->left==NULL)&&(p->right==NULL))
{
printf("%d\n",p->n);
return 0;
}
else if((p->left!=NULL)&&(p->right==NULL))
{
printf_tree(p->left);
printf("%d\n",p->n);
return 0;
}
else if((p->left==NULL)&&(p->right!=NULL))
{
printf("%d\n",p->n);
printf_tree(p->right);
}
else if((p->left!=NULL)&&(p->right!=NULL))
{
printf_tree(p->left);
printf("%d\n",p->n);
printf_tree(p->right);
}
}
void happen_rand(int* p, int n){
int i;
for (i = 0; i < N; i++) {
p[i] = rand() % K;
}
}
算法导论 文章被收录于专栏
以小白的角度诠释算法导论,假如有人看不懂,我就面壁思过。