C语言编写的二叉树问题~~

golden_softwave_001 golden_softwave_001 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
/*************************************************
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;
}



好像是指针越界问题,请高手调试指点一下~~~~
...全文

相关推荐

评论 2
收藏 已收藏
分享
返回顶部