格式化显示二叉树
来源:互联网 发布:看建筑图纸软件 编辑:程序博客网 时间:2024/05/16 11:44
#include <stdio.h>
#include <string.h>
#include <windows.h>
struct BTree {
int data;
struct BTree * left;
struct BTree * right;
};//end struct BTree
BTree * CreateLeaf(int number)
{
BTree * l = new BTree;
l->data = number;
l->left = 0;
l->right = 0;
return l;
}//end CreateLeaf
void append(BTree ** root, int number)
{
if (!root) return ;
BTree * t = *root;
if (!t) {
*root = CreateLeaf(number);
return ;
}//end if
while(t) {
if(number == t->data ) return ; //ignore duplicated elements.
if(number < t->data ) {
if(!t->left ) {
t->left = CreateLeaf(number);
return ;
}//end if
t = t->left ;
}else{
if(!t->right ) {
t->right = CreateLeaf(number);
return ;
}//end if
t = t->right ;
}//end if
}//end while
}//end append
void PrintLeaf(const BTree * root)
{
if (!root) return ;
if (root->left ) PrintLeaf(root->left );
printf("%d/t", root->data);
if (root->right ) PrintLeaf(root->right );
}//end printLeaf
void PrintTree (const BTree * root)
{
printf("[/t");
PrintLeaf(root);
printf("]/n");
}//end printTree
BTree * global_root = 0;
int Offsets[32];
int LevelHeight = 50;
void GDIShowTree(HDC h, BTree * root, int level, int center)
{
if (!root || !h) return ;
char number[32] = ""; sprintf(number, "%d", root->data );
int x0 = center; int off = Offsets[level + 1]; int x1 = 0;
int y0 = level * LevelHeight; int y1 = y0 + LevelHeight;
if (root->left ) {
x1 = x0 - off;
GDIShowTree(h, root->left , level + 1, x1);
MoveToEx(h, x0, y0, 0);
LineTo(h, x1, y1);
}//end if
if (root->right) {
x1 = x0 + off;
GDIShowTree(h, root->right, level + 1, x1);
MoveToEx(h, x0, y0, 0);
LineTo(h, x1, y1);
}//end if
TextOut(h, x0, y0, number, strlen(number));
}//end GDIShowTree
LRESULT CALLBACK MsgProc(
HWND hwnd, // handle to window
UINT uMsg, // message identifier
WPARAM wParam, // first message parameter
LPARAM lParam) // second message parameter
{
HDC hDC = 0; int i = 0; RECT r = {0, 0, 0, 0}; int width = 0;
PAINTSTRUCT ps;
switch(uMsg){
case WM_PAINT:
hDC=BeginPaint(hwnd,&ps);
GetClientRect(hwnd, &r);
width = r.right ;
for(i = 0; i < 32; i++) {
width /= 2;
Offsets[i] = width;
}//next i
GDIShowTree(hDC, global_root, 0, r.right / 2);
EndPaint(hwnd,&ps);
break;
case WM_CLOSE:
exit(0);
default:
return DefWindowProc(hwnd,uMsg,wParam,lParam);
}//end case
return 0;
}//end MsgProc
int main(void)
{
int x = 0;
printf("Enter some numbers ended with 0:");
do {
scanf("%d", &x);
append(&global_root, x);
}while(x);
PrintTree(global_root);
WNDCLASS wc;
wc.cbClsExtra=0;
wc.cbWndExtra=0;
wc.hbrBackground=(HBRUSH)GetStockObject(WHITE_BRUSH);
wc.hCursor=LoadCursor(NULL,IDC_CROSS);
wc.hIcon=LoadIcon(NULL,IDI_ERROR);
wc.hInstance=0;
wc.lpfnWndProc=MsgProc;
wc.lpszClassName="Binary Tree Demo";
wc.lpszMenuName=NULL;
wc.style=CS_HREDRAW | CS_VREDRAW;
RegisterClass(&wc);
HWND hwnd;
hwnd=CreateWindow("Binary Tree Demo","Enoch WILLS 2010",WS_OVERLAPPEDWINDOW,
0,0,640,480,0,0,0,0);
ShowWindow(hwnd,SW_MAXIMIZE);
UpdateWindow(hwnd);
MSG msg;
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}//end while
return 0;
}//end main
- 格式化显示二叉树
- 树状显示二叉树
- 显示二叉树
- 【蓝桥杯】显示二叉树
- 蓝桥杯 显示二叉树
- 格式化显示
- 格式化显示
- 显示二叉树的算法
- 二叉树的图形显示
- 二叉树图形显示(VC)
- 蓝桥杯决赛-----显示二叉树
- 二叉树的横向显示
- 显示二叉树图 Panel
- ReportingService格式化和显示格式化
- 格式化显示时间函数
- 日期字符串格式化显示
- C#显示格式化
- 格式化命令行日期显示
- panel刷新问题
- 第四节--构造函数和析构函数
- 用php实现简单的滑动菜单
- 一个非常全面获取图象信息的PHP函数
- Note
- 格式化显示二叉树
- 安装 nginx
- extjs继承panel实现图片缩放、翻页功能
- ExtJs的html布局
- vc++ ADO数据库
- 第九章 子窗口控件(静态类别,滚动条类别)
- 不使用临时表,仅使用select实现查询出多行常数
- 第二十章 多任务和多线程(线程同步)
- 第二十章 多任务和多线程(多任务的各种模式4)