首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-03-13 
C语言编写的二叉树问题~~C/C++ code/*************************************************Copyright (C), 2

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

C/C++ code
/*************************************************Copyright (C), 2000-2004, ******File name:      ALL_TREE.cppAuthor:         ZHJVersion:        1.0Date:            2009-9-15Description:    根据层次构造二叉树*************************************************/#include        <stdio.h>#include        <stdlib.h>#include        <string.h>/*************************************************Copyright (C), 2000-2004, ******File name:      ALL_TREE.cppAuthor:         ZHJVersion:        1.0Date:            2009-9-16Description:    构造顺序栈服务于二叉树*************************************************/#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;} 



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

[解决办法]
问题1:

C/C++ code
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;}
[解决办法]
void lay_CreateTree(BinaryTree_1 *T) 
可以改为void lay_CreateTree(BinaryTree_1 *&T) 

热点排行