二叉树基本操作
#include "stdio.h"
#include "locale.h"
#include "string.h"
typedef struct _Node{
char szName[64];
struct _Node *pChild, *pSibling;
}NODE, *NODE_PTR;
typedef struct {
NODE_PTR *elem;
int front,rear;
int queuesize;
}XhQueue;
void InitQueue(XhQueue &Q,int maxsize){
Q.elem=new NODE_PTR[maxsize];
if(Q.elem!=NULL){
Q.front=Q.rear=0;
Q.queuesize=maxsize;
}
}
void EnQueue(XhQueue &Q,NODE_PTR p){
if((Q.rear+1)%Q.queuesize!=Q.front){
Q.elem[Q.rear]=p;
Q.rear=(Q.rear+1)%Q.queuesize;
}
}
void DeQueue(XhQueue &Q,NODE_PTR &p){
if(Q.front!=Q.rear){
p=Q.elem[Q.front];
Q.front=(Q.front+1)%Q.queuesize;
}
}
void GetHead(XhQueue &Q,NODE_PTR &p){
if(Q.front!=Q.rear){
p=Q.elem[Q.front];
}
}
int EmQueue(XhQueue Q){
if(Q.front!=Q.rear)return 0;
else return 1;
}
//输入先序字符串创建的方式
void Create(NODE_PTR &pTree)
{
char szInput[64];
gets(szInput);
if(szInput[0] == '^')
pTree = NULL;
else{
pTree = new NODE;
strcpy(pTree->szName, szInput);
Create(pTree->pChild);
Create(pTree->pSibling);
}
}
//输入边创建的方式
void CreateTree( NODE_PTR &T ) {
char fa[30],ch[30];
NODE_PTR p,s,r;
XhQueue Q;
InitQueue(Q,50);
T = NULL;
printf("请输入父结点和子结点(用空格隔开,根结点的双亲为#)\n");
scanf("%s%s",fa, ch);
for( ; strcmp(ch,"end"); ) {
p =new NODE;
strcpy(p->szName,ch);
p->pChild=p->pSibling=NULL;
EnQueue(Q, p);
if (strcmp(fa,"#")==0) T = p;
else {
GetHead(Q,s);
while (strcmp(s->szName,fa)) {
DeQueue(Q,s); GetHead(Q,s);
}
if (!(s->pChild)){
s->pChild = p;
r = p;
}
else {
r->pSibling = p; r = p;
}
}
printf("请输入父结点和子结点(用空格隔开,结束时输入end end)\n");
scanf("%s%s",fa,ch);
}
}
void Destroy(NODE_PTR &pTree)
{
if(pTree == NULL) return;
if(pTree->pChild)
Destroy(pTree->pChild);
if(pTree->pSibling)
Destroy(pTree->pSibling);
delete pTree;
pTree = NULL;
}
void Print(NODE_PTR pTree, int indent)
{
if (pTree == NULL)
return;
for(int i = 0; i < indent; i++)
printf(" ");
printf("%s\n", pTree->szName);
Print(pTree->pChild, indent + 1);
Print(pTree->pSibling, indent);
}
void Find(NODE_PTR pTree, char *szName, NODE_PTR &p)
{
if(pTree == NULL) return;
if(strcmp(pTree->szName, szName) == 0)
p = pTree;
else{
Find(pTree->pChild, szName, p);
Find(pTree->pSibling, szName, p);
}
}
void Find(NODE_PTR pTree, char *szName, NODE_PTR &pLast, NODE_PTR &p1, NODE_PTR &p2)
{
if(pTree == NULL) return;
if(strcmp(pTree->szName, szName) == 0){
p2 = pTree;
p1 = pLast;
}
else{
pLast = pTree;
Find(pTree->pChild, szName, pLast, p1, p2);
pLast = pTree;
Find(pTree->pSibling, szName, pLast, p1, p2);
}
}
void Insert(NODE_PTR pTree, char *szParent, char *szNew)
{
NODE_PTR pFind = NULL;
NODE_PTR pChild = NULL;
Find(pTree, szParent, pFind);
if(pFind){
Find(pFind->pChild, szNew, pChild);
if(pChild){
printf("子节点已存在!\n");
return;
}
NODE_PTR pNew = new NODE;
strcpy(pNew->szName, szNew);
if(pFind->pChild){
pNew->pSibling = pFind->pChild;
pNew->pChild = NULL;
}else{
pNew->pChild = NULL;
pNew->pSibling = NULL;
}
pFind->pChild = pNew;
}else
printf("父节点不存在!\n");
}
void Delete(NODE_PTR &pTree, char *szName){
NODE_PTR pFind = NULL, pParent = NULL, p = NULL;
Find(pTree, szName, p, pParent, pFind);
if(pFind){
if(pParent == NULL){
Destroy(pTree);
pTree = NULL;
}else{
if(pParent->pChild == pFind)
pParent->pChild = pFind->pSibling;
else
pParent->pSibling = pFind->pSibling;
pFind->pSibling = NULL;
Destroy(pFind);
}
}else
printf("指定删除节点不存在!\n");
}
void Path(NODE_PTR pTree, char *szName, char *psz, char *szPath)
{
NODE_PTR p;
char sz[256];
if(pTree == NULL) return;
if(strcmp(pTree->szName, szName) == 0){
strcpy(szPath, psz);
return;
}else{
if(pTree->pChild){
strcpy(sz, psz);
if(strlen(psz) != 0)
strcat(psz,"\\");
strcat(psz,pTree->szName);
Path(pTree->pChild, szName, psz, szPath);
strcpy(psz, sz);
}
p = pTree->pSibling;
while(p!=NULL){
if(strcmp(p->szName, szName) == 0){
strcpy(szPath, psz);
return;
}else{
strcpy(sz, psz);
if(strlen(psz) != 0)
strcat(psz,"\\");
strcat(psz,p->szName);
Path(p->pChild, szName, psz, szPath);
strcpy(psz, sz);
}
p = p->pSibling;
}
}
}
void pre(NODE_PTR pTree)
{
while(pTree)
{
printf("%s",pTree->szName);
pre(pTree->pChild);
pTree=pTree->pSibling;
}
}
void pre1(NODE_PTR pTree)
{
if(pTree)
{
printf("%s",pTree->szName);
pre1(pTree->pChild);
pre1(pTree->pSibling);
}
}
void pre1path(NODE_PTR pTree,int level,char szPathArray[64][256])
{
if(pTree)
{
strcpy(szPathArray[level],pTree->szName);
if(pTree->pChild)
pre1path(pTree->pChild,level+1,szPathArray);
else{
for(int i=1; i<=level;i++)
printf("\\%s",szPathArray[i]);
printf("\n");
}
pre1path(pTree->pSibling,level,szPathArray);
}
}
int main(int argc, char* argv[])
{
int nChoice;
NODE_PTR Tree = NULL;
char szP[64],szN[64],szEat[64],szDel[64],sz[64];
char pszPath[128];
pszPath[0]='\0';
for(; ;){
printf("/******************************************");
printf("\n* 名称:虚拟资源管理器 *");
printf("\n* 作者:DS课程组,2009 *");
printf("\n******************************************/\n");
printf("1----先序字符串创建\n");
printf("2----输入边创建\n");
printf("3----查看\n");
printf("4----插入\n");
printf("5----删除\n");
printf("6----销毁\n");
printf("7----路径\n");
printf("8----先序\n");
printf("9----叶子节点路径\n");
printf("0----退出\n");
printf("请选择功能: ");
scanf("%d", &nChoice);
printf("\n");
switch(nChoice){
case 1:
printf("请输入创建信息:\n");
gets(szEat);
Create(Tree);
break;
case 2:
printf("请输入创建信息:\n");
gets(szEat);
CreateTree(Tree);
break;
case 3:
Print(Tree, 0);
break;
case 4:
printf("请输入父节点名: ");gets(szEat);gets(szP);
printf("请输入新节点名: ");gets(szN);
Insert(Tree, szP, szN);
break;
case 5:
printf("请输入待删节点名: ");gets(szEat);gets(szDel);
Delete(Tree, szDel);
break;
case 6:
Destroy(Tree);
break;
case 7:
printf("请输入节点名: ");gets(szEat);gets(sz);
char szPath[256],szTemp[256];
szPath[0]=0,szTemp[0]=0;
Path(Tree, sz, szTemp, szPath);
printf("\n节点所在路径为:");
printf(szPath);
printf("\n");
break;
case 8:
pre(Tree);
printf("\n");
pre1(Tree);
printf("\n");
break;
case 9:
char szPathArray[64][256];
pre1path(Tree,1,szPathArray);
break;
case 0:
Destroy(Tree);
return 0;
}
}
}