100层时就不能用递归了
现在最高支持到24层^_^
很多地方都没有注释,希望你能看懂^_^
源.c#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "Tree.h"
#define LAYER 24 // 24层是最大了 ^_^
#define ITEM 9999999
int tmp[LAYER + 1];
unsigned long n = 0;
static void set_array(Tree **T, int(*((*path)[ITEM]))[LAYER + 1], int c)
{
if((*T)->lchild == NULL)
{
tmp[c++] = (*T)->data;
c = 1;
(*path)[n++] = malloc(sizeof(int) * (LAYER + 1)); // 数组第0个元素存储总和
(*path)[n] = NULL;
memcpy(*(*path)[n - 1], tmp, (LAYER + 1) * sizeof(int));
return;
}
tmp[c++] = (*T)->data;
set_array(&(*T)->lchild, path, c);
set_array(&(*T)->rchild, path, c);
}
static unsigned long get_item(int(*((*path)[ITEM]))[LAYER + 1])
{
unsigned long ret = 0;
unsigned long c = 0;
while((*path)[c] != NULL)
{
c++;
ret++;
}
return ret;
}
static void free_array(int(*((*path)[ITEM]))[LAYER + 1])
{
for(unsigned long i = 0; i < ITEM; i++)
{
if((*path)[i] == NULL)
{
break;
}
free((*path)[i]);
}
}
int euler(Tree **T, int (**ret_path)[LAYER + 1])
{
int(*((*path)[ITEM]))[LAYER + 1];
path = malloc(sizeof(int) * ITEM);
set_array(T, path, 1); // 数组第0个元素存储总和
// 计算每条路径的和
unsigned long item = get_item(path);
for(unsigned long i = 0; i < item; i++)
{
(*(*path)[i])[0] = 0;
for(int j = 1; j <= LAYER; j++)
{
(*(*path)[i])[0] += (*(*path)[i])[j];
}
}
// 查找最大
int max = (*(*path)[0])[0];
int index = 0;
for(unsigned long i = 0; i < item; i++)
{
if(max < (*(*path)[i])[0])
{
index = i;
max = (*(*path)[i])[0];
}
}
memcpy(*ret_path, (*path)[index], sizeof(int) * (LAYER + 1));
free_array(path);
free(path);
return max;
}
int main(void)
{
int(*ret_path)[LAYER + 1] = malloc(sizeof(int) * (LAYER + 1));
Tree *T;
InitTree(&T);
CreateTree(&T, LAYER);
PrintTree(&T);
putchar('\n');
printf("最大和是:%d\n", euler(&T, &ret_path));
printf("输出路径:\n");
for(int i = 1; i <= LAYER; i++)
{
printf("%.3d -> ", (*ret_path)[i]);
}
// 退掉最后一个箭头
printf("\b\b\b\b");
printf(" ");
putchar('\n');
free(ret_path);
DestroyTree(&T);
return 0;
}
Tree.c#include "Tree.h"
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
static int get_rand()
{
return 1 + (int)(999.0*rand() / (RAND_MAX + 0.0));
}
void InitTree(Tree **T)
{
(*T) = malloc(sizeof(Tree));
(*T)->data = get_rand();
(*T)->lchild = NULL;
(*T)->rchild = NULL;
(*T)->degree = NULL;
srand((unsigned)time(NULL)); // 初始化一下随机数
}
static Tree *get_lchild(Tree **T)
{
return (*T)->lchild->rchild;
}
static void set_left_degree(Tree **T, Tree *rchild)
{
(*T)->lchild->degree = rchild;
}
static void create_left_child(Tree **T, int layer)
{
if(layer == 1)
{
return ;
}
(*T)->lchild = malloc(sizeof(Tree));
(*T)->lchild->data = get_rand();
(*T)->lchild->lchild = NULL;
(*T)->lchild->rchild = NULL;
(*T)->lchild->degree = NULL;
create_left_child(&(*T)->lchild, layer - 1);
}
static void create_right_child2(Tree **T, int layer)
{
if(layer == 1)
{
return ;
}
(*T)->rchild = malloc(sizeof(Tree));
(*T)->rchild->data = get_rand();
(*T)->rchild->lchild = get_lchild(T);
(*T)->rchild->rchild = NULL;
(*T)->rchild->degree = NULL;
set_left_degree(T, (*T)->rchild);
create_right_child2(&(*T)->rchild, layer - 1);
}
static void create_right_child(Tree **T, int layer)
{
if((*T)->lchild == NULL)
{
return ;
}
create_right_child(&(*T)->lchild, layer - 1);
create_right_child2(T, layer);
}
void CreateTree(Tree **T, int layer)
{
create_left_child(T, layer);
create_right_child(T, layer);
}
void DestroyTree(Tree **T)
{
Tree *pp = *T;
Tree *p = pp->lchild;
Tree *tmp;
while(p != NULL)
{
p = pp->lchild;
while(pp != NULL)
{
tmp = pp;
pp = pp->degree;
free(tmp);
}
pp = p;
}
*T = NULL;
}
static void print_space(int max_degree, int i)
{
int n = (max_degree * 4 / 2 - 2) - (i - 1) * 4 / 2;
while(n--)
{
printf(" ");
}
}
static int get_degree(Tree **T)
{
int ret = 0;
Tree *p = *T;
while(p != NULL)
{
ret++;
p = p->degree;
}
return ret;
}
static int get_max_degree(Tree **T)
{
int ret = 0;
Tree *p = *T;
while(p->lchild != NULL)
{
p = p->lchild;
}
while(p != NULL)
{
ret++;
p = p->degree;
}
return ret;
}
void PrintTree(Tree **T)
{
Tree *p = *T;
Tree *pp = p;
int max_degree = get_max_degree(T);
while(p != NULL)
{
pp = p;
print_space(max_degree, get_degree(&p));
while(pp != NULL)
{
printf("%.3d ", pp->data);
pp = pp->degree;
}
putchar('\n');
p = p->lchild;
}
}
Tree.h#ifndef _TREE_H
#define _TREE_H
#define OK 1
#define ERROR 0
#define TRUE 1
#define FALSE 0
//typedef int Status; /* Status是函数的类型,其值是函数结果状态代码,如OK等 */
typedef int ElemType;
typedef struct Tree
{
ElemType data;
struct Tree *lchild;
struct Tree *rchild;
struct Tree *degree;
} Tree;
void InitTree(Tree **T);
void CreateTree(Tree **T, int layer);
void PrintTree(Tree **T);
void DestroyTree(Tree **T);
#endif
|