延时摄影软件:二叉树遍历问题

来源:百度文库 编辑:中科新闻网 时间:2024/04/27 16:16:21
# include <malloc.h>
# include <iostream.h>
# include <conio.h>
# include <stdlib.h>

# define OK 1
# define ERROR 0
# define OVERFLOW 0
# define STACK_INIT_SIZE 100//MAXSIZE // 存储空间初始分配量
# define STACKINCREMENT 10
typedef char TElemType;
typedef struct BiTNode //define structure BiTree
{ TElemType data;
struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;

typedef BiTNode SElemType;
typedef struct SqStack //define structure SqStack
{ BiTree * base;
BiTree * top;
int stacksize;
}SqStack;

CreateBiTree(BiTree &T) //createBiTree() 子函数
{
TElemType ch;
cin>>ch;
if(ch=='/')
T=NULL;
else
{
if(!(T=(BiTNode *)malloc(sizeof(BiTNode))))
exit(OVERFLOW);
T->data=ch;
CreateBiTree(T->lchild); //create lchild
CreateBiTree(T->rchild); //create rchild
}
}
int InitStack(SqStack &S) //InitStack() sub-function
{ S.base=(SElemType* *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
if(!S.base)
{ cout<<endl<<"Allocate space failure !";
return (ERROR);
}
S.top=S.base;
S.stacksize=STACK_INIT_SIZE;
return (OK);
} //InitStack() end

int Push(SqStack &S,BiTree e) //e前面为什么不加& ???
{ if(S.top-S.base>S.stacksize)
{ S.base=(SElemType* *)realloc(S.base,(S.stacksize+
STACKINCREMENT*sizeof(SElemType)));
if(!S.base)
{ cout<<endl<<"Overflow!";
return (ERROR);
}
S.top=S.base+S.stacksize;
S.stacksize+=STACKINCREMENT;
}
* (S.top)=e;
S.top++;
return (OK);
} //Push() end

int Pop(SqStack &S,BiTree &e) //e前面为什么加& ???
{ if(S.top==S.base)
{ cout<<endl<<"It's a empty SqStack!";
return (ERROR);
}
S.top--;
e=*(S.top);

return (OK);
} //Pop() end

int StackEmpty(SqStack S) //StackEmpty() sub-function
{ if(S.top==S.base)
return (OK);
else
return (ERROR);
} //StackEmpty() end

void preorder1(BiTree T)
{ SqStack S; BiTree P=T;
InitStack(S);
Push(S,NULL);
while (P)
{ cout<<P->data<<"->";
if (P->rchild)
Push(S,P->rchild);
if (P->lchild)
P=P->lchild;
else Pop(S,P);
}
}
void main() //main() function
{ BiTree T;

cout<<endl<<"Please input data to create BiTree:"<<endl;
CreateBiTree(T); //call CreateBiTree()
cout<<endl<<"InOrder :"<<endl<<endl<<"Begin->";
preorder1(T); //Call InOrderTraverse()
cout<<"End !"<<endl<<endl<<"...OK!...";
getch();
} //main() end

第一个地方:因为只需要把节点的地址传进函数就可以了。不需要函数改变外界参数的值。
第二个地方:外界参数是要得到节点的地址的值,它需要函数来改变它。这样它不把自己的地址给人家,人家怎么改啊。
你好好理解。。不理解的话QQ我。。115088917

你想问什么?