先序遍历的非递归算法 C语言.docxVIP

  • 34
  • 0
  • 约1.36千字
  • 约 3页
  • 2021-11-06 发布于湖南
  • 举报
先序遍历的非递归算法 C语言 先序遍历的非递归算法 C语言 先序遍历的非递归算法 #include #include #define MS 10 struct BTreeNode char data; struct BTreeNode * left; struct BTreeNode * right; void InitBTree(struct BTreeNode * * BT) * BT=NULL; void CreatBTree(struct BTreeNode * * BT,char * a) struct BTreeNode * p; struct BTreeNode * s[MS]; int top=-1; int i=0; * BT=NULL; while(a[i]) switch(a[i]) case :break; case (: if(top==MS-1) printf(栈空间太小,需增加MS的值!\n); exit(1); top++;s[top]=p;k=1; case ): if(top==-1) printf(二叉树广义表字符串有错!\n); exit(1); top--;break; case , : k=2;break; default : if((a[i]=aa[i]=Aa[i] p=malloc(sizeof(struct BTreeNode)); p-data=a[i];p-left=p-right=NULL; if(* BT==NULL) * BT=p; if(k==1) s[top]-left=p; else s[top]-right=p; else {printf(二叉树广义表字符串有错!\n);exit(1);} } void Preorder(struct BTreeNode * BT) struct BTreeNode * s[10]; int top=-1; struct BTreeNode * p=BT; printf(先序遍历结点的访问序列为:\n); while(top!=-1||p!=NULL) while(p!=NULL) s[top]=p; printf(%c,p-data); p=p-left; if(top!=-1) p=s[top]; p=p-right; void main() struct BTreeNode * p; char * a=A(B(C),D(E(F,G),H(,I))); InitBTree(p); CreatBTree(p,a); Preorder(p);printf(\n);

文档评论(0)

1亿VIP精品文档

相关文档