33,007
社区成员
发帖
与我相关
我的任务
分享
/*************************************************
Copyright (C), 2000-2004, ******
File name: ALL_TREE.cpp
Author: ZHJ
Version: 1.0
Date: 2009-9-15
Description: 根据层次构造二叉树
*************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*************************************************
Copyright (C), 2000-2004, ******
File name: ALL_TREE.cpp
Author: ZHJ
Version: 1.0
Date: 2009-9-16
Description: 构造顺序栈服务于二叉树
*************************************************/
#include <malloc.h>
//#include "btstruct.h"
#define MAXALLOCATION 100 //初试分配的空间大小
#define INCREATALLOCATE 10 //空间不足时进行补充
typedef char ElemType;
//构造基本的二叉树
struct binaryTree_1
{
ElemType elem; //节点元素
struct binaryTree_1 *lchild_1,*rchild_1;
};
typedef struct binaryTree_1 BinaryTree_1;
typedef BinaryTree_1 Type;
typedef struct stackmaze
{
int top; //栈顶指针
Type *elems; //存放栈元素的数组
int MaxSize; //栈空间的最大尺寸
}Stack;
//////////////////////////////////初始化栈结构
void Inite_Stack(Stack *S)
{
S->elems = (Type *)malloc(MAXALLOCATION*sizeof(Type));
if(!S->elems)
exit(1);
S->MaxSize = 0;
S->top = 0;
}
/////////////////////////////////当空间不够用的时候,进行再次分配
void AnginAllow(Stack *S)
{
Type *p;
p = (Type *)realloc(S->elems,(S->MaxSize+INCREATALLOCATE)*sizeof(Type));
if(!p)
{
printf("\n空间分配失败\n");
exit(0);
}
S->elems = p; //将元素重新赋值给原数组
S->MaxSize = S->MaxSize+INCREATALLOCATE;
}
//////////////////////////////////判断栈是否为空
int IsStackEmpty(Stack *S)
{
if(S->top == 0)
return 1;
else
return 0;
}
////////////////////////////////////栈的压入操作
Stack * Push_Stack(Stack *S,Type *e,int temp)
{
if(S->MaxSize == MAXALLOCATION)
AnginAllow(S);
else
{
S->top = temp;
S->elems[S->top] = *e;
S->MaxSize++;
}
return S;
}
//////////////////////////////////////栈的弹出操作
Type Pop_Stack(Stack *S)
{
if(!IsStackEmpty(S))
{
return S->elems[--S->top];
}
else
{
printf("\n栈已经为空\n");
exit(0);
}
}
void lay_CreateTree(BinaryTree_1 *T)
{
Stack *S = (Stack *)malloc(sizeof(Stack));
Inite_Stack(S);
char ch = NULL; //树节点元素
int i = 0,j = 0,k = 0;
printf("\nPlease input binary tree's element:\t");
while((ch = getchar())!='\n')
{
if(ch != '#')
{
if(i == 0)
{
T = (BinaryTree_1 *)malloc(sizeof(BinaryTree_1));
Push_Stack(S,T,0);
}
else
{
if(i % 2 == 1)
{
k = (i -1)/2;
S->elems[k].lchild_1 = (BinaryTree_1 *)malloc(sizeof(BinaryTree_1));
Push_Stack(S,S->elems[k].lchild_1,i);
}
else
{
k = (i - 2)/2;
S->elems[k].rchild_1 = (BinaryTree_1 *)malloc(sizeof(BinaryTree_1));
Push_Stack(S,S->elems[k].rchild_1,i);
}
}
S->elems[i].elem = ch;
S->elems[i].lchild_1 = NULL;
S->elems[i].rchild_1 = NULL;
j = 2*i+1;
Push_Stack(S,NULL,j);
j = 2*i+2;
Push_Stack(S,NULL,j);
i++;
}
else
{
Push_Stack(S,NULL,i);
i++;
}
}
}
//遍历
void preTravel(BinaryTree_1 *T)
{
if(T)
{
printf("--> %c",T->elem);
preTravel(T->lchild_1);
preTravel(T->rchild_1);
}
}
int main()
{
BinaryTree_1 *T = NULL;
lay_CreateTree(T);
preTravel(T);
return 0;
}
Stack * Push_Stack(Stack *S,Type *e,int temp)
{
if(S->MaxSize == MAXALLOCATION)
AnginAllow(S);
else
{
S->top = temp;
S->elems[S->top] = *e; //当e为NULL时此操作非法,要改为if (e)S->elems[S->top] = *e; else memset(&S->elems[S->top], 0, sizeof(Type));
S->MaxSize++;
}
return S;
}