#include <stdio.h>
#include <malloc.h>
#define MaxSize 1000
typedef char ElemType
typedef struct lnode
{
int tag
union
{
ElemType data
struct lnode *sublist
} val
struct lnode *link
} GLNode
//建立广义表
GLNode *CreateGL(char *&s)
{
GLNode *h
char ch
ch=*s
s++
if(ch!='\0')
{
h=(GLNode *)malloc(sizeof(GLNode))
if(ch=='(')
{
h->tag=1
h->val.sublist=CreateGL(s)
}
else if(ch==')') h=NULL
else
{
h->tag=0
h->val.data=ch
}
}
else h=NULL
ch=*s
s++
if(h!=NULL)
{
if(ch==',') h->link=CreateGL(s)
else h->link=NULL
}
return h
}
//输出广义表
void DispGL(GLNode *g)
{
if(g!=NULL)
{
if(g->tag==1)
{
printf("(")
if(g->val.sublist==NULL) printf("")
else DispGL(g->val.sublist)
}
else printf("%c",g->val.data)
if(g->tag==1) printf(")")
if(g->link!=NULL)
{
printf(",")
DispGL(g->link)
}
}
}
//广义表的复制
GLNode *GLCopy(GLNode *p)
{
GLNode *q
if(p==NULL) return NULL
q=(GLNode *)malloc(sizeof(GLNode))
q->tag=p->tag
if(p->tag==1) q->val.sublist=GLCopy(p->val.sublist)
else q->val.data=p->val.data
q->link=GLCopy(p->link)
return q
}
/*下面的程序编译运行后,有个输入框,输入你的数据,以输入0按回车键结束,然后就会d出一个窗口,画出一颗二叉树*/#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 = centerint off = Offsets[level + 1]int x1 = 0
int y0 = level * LevelHeightint 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 = 0int i = 0RECT 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 = 0i <32i++) {
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
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)