格式化显示二叉树

来源:互联网 发布:看建筑图纸软件 编辑:程序博客网 时间: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