主题:[原创]C语言实现的二叉树画法
//刚学二叉树,程序写得很乱,还请见谅!
//本程序在bmp图片上画,几个头文件由于篇幅未能贴出,如有需要再贴出来
#ifndef _DRAW_H
#define _DRAW_H
#include "lqueue.h"
#include "btnode.h"
#include "btree.h"
#include "bmp.h"
int *next_pos;
int *offset;
RGB circle_rgb = { 145, 012, 34 };
RGB line_rgb = {153, 204, 204};
DWORD line_color;
DWORD circle_color;
RGB rgb_bg_color = {0xff, 0xff, 0xff};
DWORD background_color;
FILE* handle;
void transform_cdt(BTREE* tree);
void tree_drawing(const BTREE* tree);
void line_drawing(BTREE_NODE* node);
void circle_drawing(BTREE_NODE* node);
void determing_cmp_cdt(BTREE_NODE* node);
void add_up_offset(BTREE_NODE* node);
void draw(BTREE* tree);
/*============================================================================*/
void draw(BTREE* tree)
{
int height;
int i;
height = tree_height(tree);
next_pos = (int*)malloc(sizeof(int)*height);
offset = (int*)malloc(sizeof(int)*height);
for(i = 0; i < height; i++)
{
next_pos[i] = 1;
offset[i] = 0;
}
post_order(tree->root, determing_cmp_cdt);
pre_order(tree->root, add_up_offset);
tree_drawing(tree);
free(next_pos);
free(offset);
}
/*============================================================================*/
void determing_cmp_cdt(BTREE_NODE* node)
{
int pos;
if(is_leaf(node))
{
pos = next_pos[node->height];
}
else if(only_left_child(node))
{
pos = node->left_child->cdt.x + 1;
}
else if(only_right_child(node))
{
pos = node->right_child->cdt.x - 1;
}
else
{
pos = (node->left_child->cdt.x + node->right_child->cdt.x) / 2;
}
offset[node->height] = max(offset[node->height],
next_pos[node->height]-pos);
if(is_leaf(node))
{
node->cdt.x = pos;
}
else
{
node->cdt.x = pos + offset[node->height];
}
node->cdt.y = node->height + 1;
next_pos[node->height] = node->cdt.x + 2;
node->offset = offset[node->height];
}
/*============================================================================*/
void add_up_offset(BTREE_NODE* node)
{
if(has_left_child(node))
{
node->left_child->offset += node->offset;
node->left_child->cdt.x += node->offset;
}
if(has_right_child(node))
{
node->right_child->offset += node->offset;
node->right_child->cdt.x += node->offset;
}
}
/*============================================================================*/
void tree_drawing(const BTREE* tree)
{
line_color = map_rgb(&line_rgb);
circle_color = map_rgb(&circle_rgb);
bg_color = map_rgb(&rgb_bg_color);
handle = create_bmp_file("tree.bmp", 1024, 768, 3, bg_color);
if(handle == NULL)
{
perror("errors happened!\n");
return;
}
pre_order(tree->root, line_drawing);
pre_order(tree->root, circle_drawing);
close_bmp_file(handle);
}
/*============================================================================*/
void line_drawing(BTREE_NODE* node)
{
if(has_left_child(node))
{
line_bres(handle, node->cdt.x * 50, node->cdt.y * 80,
node->left_child->cdt.x * 50, node->left_child->cdt.y * 80, line_color);
}
if(has_right_child(node))
{
line_bres(handle, node->cdt.x * 50, node->cdt.y * 80,
node->right_child->cdt.x * 50, node->right_child->cdt.y * 80, line_color);
}
}
/*============================================================================*/
void circle_drawing(BTREE_NODE* node)
{
if(node == NULL)
return;
circle_bres(handle, node->cdt.x * 50, node->cdt.y *80, 20, circle_color, 1);
}
#endif
//本程序在bmp图片上画,几个头文件由于篇幅未能贴出,如有需要再贴出来
#ifndef _DRAW_H
#define _DRAW_H
#include "lqueue.h"
#include "btnode.h"
#include "btree.h"
#include "bmp.h"
int *next_pos;
int *offset;
RGB circle_rgb = { 145, 012, 34 };
RGB line_rgb = {153, 204, 204};
DWORD line_color;
DWORD circle_color;
RGB rgb_bg_color = {0xff, 0xff, 0xff};
DWORD background_color;
FILE* handle;
void transform_cdt(BTREE* tree);
void tree_drawing(const BTREE* tree);
void line_drawing(BTREE_NODE* node);
void circle_drawing(BTREE_NODE* node);
void determing_cmp_cdt(BTREE_NODE* node);
void add_up_offset(BTREE_NODE* node);
void draw(BTREE* tree);
/*============================================================================*/
void draw(BTREE* tree)
{
int height;
int i;
height = tree_height(tree);
next_pos = (int*)malloc(sizeof(int)*height);
offset = (int*)malloc(sizeof(int)*height);
for(i = 0; i < height; i++)
{
next_pos[i] = 1;
offset[i] = 0;
}
post_order(tree->root, determing_cmp_cdt);
pre_order(tree->root, add_up_offset);
tree_drawing(tree);
free(next_pos);
free(offset);
}
/*============================================================================*/
void determing_cmp_cdt(BTREE_NODE* node)
{
int pos;
if(is_leaf(node))
{
pos = next_pos[node->height];
}
else if(only_left_child(node))
{
pos = node->left_child->cdt.x + 1;
}
else if(only_right_child(node))
{
pos = node->right_child->cdt.x - 1;
}
else
{
pos = (node->left_child->cdt.x + node->right_child->cdt.x) / 2;
}
offset[node->height] = max(offset[node->height],
next_pos[node->height]-pos);
if(is_leaf(node))
{
node->cdt.x = pos;
}
else
{
node->cdt.x = pos + offset[node->height];
}
node->cdt.y = node->height + 1;
next_pos[node->height] = node->cdt.x + 2;
node->offset = offset[node->height];
}
/*============================================================================*/
void add_up_offset(BTREE_NODE* node)
{
if(has_left_child(node))
{
node->left_child->offset += node->offset;
node->left_child->cdt.x += node->offset;
}
if(has_right_child(node))
{
node->right_child->offset += node->offset;
node->right_child->cdt.x += node->offset;
}
}
/*============================================================================*/
void tree_drawing(const BTREE* tree)
{
line_color = map_rgb(&line_rgb);
circle_color = map_rgb(&circle_rgb);
bg_color = map_rgb(&rgb_bg_color);
handle = create_bmp_file("tree.bmp", 1024, 768, 3, bg_color);
if(handle == NULL)
{
perror("errors happened!\n");
return;
}
pre_order(tree->root, line_drawing);
pre_order(tree->root, circle_drawing);
close_bmp_file(handle);
}
/*============================================================================*/
void line_drawing(BTREE_NODE* node)
{
if(has_left_child(node))
{
line_bres(handle, node->cdt.x * 50, node->cdt.y * 80,
node->left_child->cdt.x * 50, node->left_child->cdt.y * 80, line_color);
}
if(has_right_child(node))
{
line_bres(handle, node->cdt.x * 50, node->cdt.y * 80,
node->right_child->cdt.x * 50, node->right_child->cdt.y * 80, line_color);
}
}
/*============================================================================*/
void circle_drawing(BTREE_NODE* node)
{
if(node == NULL)
return;
circle_bres(handle, node->cdt.x * 50, node->cdt.y *80, 20, circle_color, 1);
}
#endif