数据结构二叉树的建立与应用源程序

附录:

//*********************************************************************** // 头文件 //*********************************************************************** #include <iostream>

#include <cstring>

#include <iomanip>

using namespace std;

#include <windows.h>

//*********************************************************************** // 全局数据 //*********************************************************************** #define MAX_CHARS 50

struct Node

{

char data;

Node* lchild;

Node* rchild;

};

typedef Node* PNode;

char *pre_order=new char[MAX_CHARS];

char *in_order=new char[MAX_CHARS];

PNode root = NULL;

int Offsets[32];

int LevelHeight = 50;

//************************************************************************ // 函数声明 //************************************************************************ inline int get_left_len(int rootpos,int in_begin,int in_end);

void create(PNode * pnode,int pre_begin,int pre_end,int in_begin,int in_end);

void preOrder(PNode pnode);

void inOrder(PNode pnode);

void postOrder(PNode pnode);

void printMenu();

void dealChoice();

void createBinaryTree();

void outPut();

void delSubTree();

void del(PNode &pnode,char ch);

void addNewNode();

void add(PNode &pnode,char ch);

LRESULT CALLBACK MsgProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam); void GDIShowTree(HDC h, PNode root, int level, int center);

void ShowUseWindow();

//************************************************************************

// 主函数

//************************************************************************

int main()

{

printMenu();

dealChoice();

return 0;

}

inline int get_left_len(int rootpos,int in_begin,int in_end)

{

for(int i = in_begin; i <= in_end; i++)

{

} if(in_order[i] == pre_order[rootpos]) { } return i-in_begin;

return -1;

}

//************************************************************************

// (先序+中序)建立二叉树 //************************************************************************

void create(PNode * pnode,int pre_begin,int pre_end,int in_begin,int in_end)

{

*pnode = new Node;

PNode temp = *pnode;

temp->data = pre_order[pre_begin];

temp->lchild = NULL;

temp->rchild = NULL;

if(pre_begin == pre_end)

{

}

int left_len = get_left_len(pre_begin,in_begin,in_end);

if(left_len > 0)

{

}

if(left_len < (in_end - in_begin))

{

create(&temp->rchild,pre_begin+left_len+1,pre_end, create(&temp->lchild,pre_begin+1,pre_begin+left_len, in_begin,in_begin+left_len-1); return;

}

} in_begin+left_len+1,in_end);

//************************************************************************ // 二叉树的遍历 //************************************************************************ void preOrder(PNode pnode)

{

if(pnode == NULL)

{

}

cout<<pnode->data;

preOrder(pnode->lchild);

preOrder(pnode->rchild);

}

void inOrder(PNode pnode)

{

if(pnode == NULL)

{

}

inOrder(pnode->lchild);

cout<<pnode->data;

inOrder(pnode->rchild);

}

void postOrder(PNode pnode)

{ return; return;

if(pnode == NULL)

{

}

postOrder(pnode->lchild);

postOrder(pnode->rchild);

cout<<pnode->data;

}

//************************************************************************ // 显示菜单 //************************************************************************ void printMenu()

{

cout<<setw(50)<<setfill('*')<<left<<"*"<<endl;

cout<<setw(40)<<setfill(' ')<<" 0 退出系统"<<endl;

cout<<setw(40)<<setfill(' ')<<" 1 根据先序+中序序列创建二叉树"<<endl; cout<<setw(40)<<setfill(' ')<<" 2 显示二叉树遍历序列"<<endl;

cout<<setw(40)<<setfill(' ')<<" 3 显示二叉树"<<endl;

cout<<setw(40)<<setfill(' ')<<" 4 删除二叉树的子树"<<endl;

cout<<setw(40)<<setfill(' ')<<" 5为二叉树添加结点"<<endl;

cout<<setw(50)<<setfill('*')<<left<<"*"<<endl;

cout<<"请输入你的选择:";

}

//************************************************************************ // 处理选择 //************************************************************************ void dealChoice()

{

char choice;

cin>>choice; return;

switch(choice)

{

case '0':exit(0);break;

case '1':createBinaryTree();break;

case '2':outPut();break;

case '3':ShowUseWindow();break;

case '4':delSubTree();break;

case '5':addNewNode();break;

default:cout<<"选择有误!"<<endl;break;

}

system("cls");

printMenu();

dealChoice();

}

//************************************************************************ // 建立二叉树 //************************************************************************ void createBinaryTree()

{

cout<<"请输入先序序列:"<<endl;

cin>>pre_order;

cout<<"请输入中序序列:"<<endl;

cin>>in_order;

create(&root,0,strlen(pre_order)-1,0,strlen(in_order)-1);

cout<<"树已成功建立!"<<endl;

system("pause");

}

//************************************************************************ // 输出遍历序列 //************************************************************************

void outPut()

{

cout<<"先序序列: ";

preOrder(root);

cout<<endl;

cout<<"中序序列: ";

inOrder(root);

cout<<endl;

cout<<"后序序列: ";

postOrder(root);

cout<<endl;

system("pause");

}

//************************************************************************ // 删除子树 //************************************************************************ void delSubTree()

{

cout<<"请输入要删除的子树父结点:";

char ch=0;

cin>>ch;

del(root,ch);

}

void del(PNode &pnode,char ch)

{

if(pnode == NULL)

{

}

del(pnode->lchild,ch); return;

if (pnode->data==ch)

{

}

del(pnode->rchild,ch);

}

//************************************************************************ // 显示树

//************************************************************************ void GDIShowTree(HDC h, PNode root, int level, int center)

{

if (!root || !h) return ;

char number[32] = ""; sprintf(number, "%c", root->data );

int x0 = center;

int x1 = 0;

int y0 = level * LevelHeight;

int y1 = y0 + LevelHeight;

int off = Offsets[level + 1];

if (root->lchild ) {

x1 = x0 - off; GDIShowTree(h, root->lchild , level + 1, x1); MoveToEx(h, x0, y0, 0); LineTo(h, x1, y1); pnode=NULL; cout<<"删除成功!"<<endl; return;

}//end if

if (root->rchild) {

x1 = x0 + off; GDIShowTree(h, root->rchild, 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(

{

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; HWND hwnd, // handle to window UINT uMsg, // message identifier WPARAM wParam, // first message parameter LPARAM lParam) // second message parameter }//next i GDIShowTree(hDC, root, 0, r.right / 2); EndPaint(hwnd,&ps); break;

case WM_CLOSE:

//exit(0); cout<<"操作回到控制台界面!"<<endl;printMenu(); dealChoice();exit(0); break; default:

return DefWindowProc(hwnd,uMsg,wParam,lParam);

}//end case

return 0;

}//end MsgProc

void ShowUseWindow()

{

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_WINLOGO);

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","二叉树直观显示界面",WS_OVERLAPPEDWINDOW,

ShowWindow(hwnd,SW_NORMAL);

UpdateWindow(hwnd);

MSG msg;

while(GetMessage(&msg,NULL,0,0))

{

TranslateMessage(&msg); 0,0,640,480,0,0,0,0);

}

} DispatchMessage(&msg);

//************************************************************************ // 增加新结点 //************************************************************************ void addNewNode()

{

char ch;

cout<<"请输入叶子结点:"<<endl;

cin>>ch;

add(root,ch);

}

void add(PNode &pnode,char ch)

{

if(pnode == NULL)

{

}

add(pnode->lchild,ch);

if (pnode->data==ch)

{

if(pnode->lchild==NULL&&pnode->rchild==NULL) { char nd; cout<<"请输入添加元素:"<<endl; cin>>nd; PNode temp = new Node; return;

temp->lchild=temp->rchild=NULL; temp->data=nd; cout<<"请输入 6:加在右边 7:加在左边"<<endl; int choice; cin>>choice; if (choice==6) { } if (choice==7) { } else { } cout<<"添加成功!"<<endl; cout<<"你的输入有错!"<<endl; pnode->lchild=temp; pnode->rchild=temp; return ;

}

add(pnode->rchild,ch);

}

} else { } return; cout<<"这不是叶子结点!"<<endl;

相关推荐