70,021
社区成员




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXDEPTH 255 //输入串最大长度
int instance_count = 0; //解的个数
int times_malloc = 0;
int times_free = 0;
int state[MAXDEPTH + 1];
#define MALLOC(sz) (++times_malloc, malloc(sz))
#define FREE(ptr) {++times_free; free((ptr)); (ptr) = NULL;}
#define BLANK " "
#define VERTICAL "│"
#define HORIZON "─"
#define BRANCH "├"
#define BRANCHEND "└"
#define EMPTY_TREE "(null)"
#define BLANK_FLAG 0
#define VERTICAL_FLAG 1
#define HORIZON_FLAG 2
#define BRANCH_FLAG 3
#define BRANCHEND_FLAG 4
#define PRINT(SYMBOL) printf("%s", SYMBOL)
//树节点
typedef struct tagTreeNode
{
struct tagTreeNode *left;
struct tagTreeNode *right;
char c;
} TreeNode;
TreeNode* new_node(char ch);
void printInstance(TreeNode *root);
void printTree(TreeNode *root, int depth, int last);
void FindAndTryInsertItem(TreeNode **pCurrent, int *found, char *input_str, int index, int length, TreeNode **ppRoot);
int main()
{
char input_str[MAXDEPTH];
int length, found;
TreeNode *pRoot = NULL;
scanf("%s", input_str);
length = strlen(input_str) - 1;
if (length != 0) //树非空
{
found = 0;
pRoot = new_node(input_str[0]);
FindAndTryInsertItem(&pRoot, &found, input_str, 1, length, &pRoot);
FREE(pRoot);
}
else
instance_count = 1; //empty tree
printf("%d instances found\n", instance_count);
printf("malloc times = %d, free times = %d\n", times_malloc, times_free);
return 0;
}
//新开节点
TreeNode* new_node(char ch)
{
TreeNode *pNewNode = (TreeNode*)MALLOC(sizeof(TreeNode));
pNewNode->left = NULL;
pNewNode->right = NULL;
pNewNode->c = ch;
return pNewNode;
}
//InsertItem
void FindAndTryInsertItem(TreeNode **pCurrent, int *found, char *input_str, int index, int length, TreeNode **ppRoot)
{
if (index == length)
{
printInstance(*ppRoot);
return;
}
if (*pCurrent == NULL)
{
if (*found)
{
int fnd = 0;
*pCurrent = new_node(input_str[index]);
FindAndTryInsertItem(ppRoot, &fnd, input_str, index + 1, length, ppRoot);
FREE(*pCurrent);
*pCurrent = NULL;
}
return;
}
if ((*pCurrent)->c == input_str[index - 1]) *found = 1;
FindAndTryInsertItem(&((*pCurrent)->left), found, input_str, index, length, ppRoot);
FindAndTryInsertItem(&((*pCurrent)->right), found, input_str, index, length, ppRoot);
}
// 打印实例
void printInstance(TreeNode *root)
{
++instance_count;
printf("Instance %d:\n", instance_count);
printTree(root, 0, 0);
printf("\n");
}
//打印一棵二叉树到屏幕
void printTree(TreeNode *root, int depth, int last)
{
int i, tmp;
int newlast;
for (i = 0; i < depth; ++i)
{
switch (state[i])
{
case BLANK_FLAG: PRINT(BLANK); break;
case VERTICAL_FLAG: PRINT(VERTICAL); break;
case BRANCH_FLAG: PRINT(BRANCH); break;
case BRANCHEND_FLAG: PRINT(BRANCHEND); break;
case HORIZON_FLAG: PRINT(HORIZON); break;
default: printf("error at line %d\n", __LINE__);
}
if (i < depth - 1)
PRINT(BLANK);
else PRINT(HORIZON);
}
char *info = EMPTY_TREE;
if (root == NULL)
{
printf("%s\n", info);
return;
}
printf("%c\n", root->c);
if (depth > 0 && last) state[depth - 1] = BLANK_FLAG;
if (root->left == NULL && root->right == NULL) return; //没有子节点
newlast = 0;
for (i = 0; i < 2; ++i)
{
if (i == 1)
{
newlast = 1;
state[depth] = BRANCHEND_FLAG;
}
else
state[depth] = BRANCH_FLAG;
if (depth > 0)
{
tmp = state[depth - 1];
if (state[depth - 1] != BLANK_FLAG && state[depth - 1] != VERTICAL_FLAG)
state[depth - 1] = VERTICAL_FLAG;
}
if (i == 0)
printTree(root->right, depth + 1, newlast);
else
printTree(root->left, depth + 1, newlast);
if (depth > 0)
state[depth - 1] = tmp;
}
}