二叉树相关操作流程图.doc

非递归先序遍历二叉树 P P=root 访问节点P,并将P进栈 P=P-LChild P为空? N 栈为空? 出栈—P, P=P-RChild 结束 Y N Y 非递归先序遍历二叉树 用扩展先序遍历序列建立二叉树 读入字符 读入字符?ch ch=’.’? 建立根结点root,并将根结点进栈;pre=root P=P-LChild 读入字符—ch ch=’.’? 建立结点P,P进栈; pre-LChild=P;pre=p Pre-LChild=NULL 出栈结点?pre 读入字符—ch ch=’.’? 建立结点P,P进栈; pre-RChild=P;pre=P Pre-RChild=NULL 栈空? 出栈结点?pre 结束 Y N N Y N Y 根据扩展先序遍历序列建立二叉树 Root=NULL,结素 Void CreateBiTree(BiTree *root) { int CreateBiTree (BiTree Root) { char ch; char *x; InitStack(S); BitNode *p,*pre; ch=getchar(); if (ch==.) { Root=NULL; return ; } Root=(BiTree)malloc(sizeof(BiTNode)); Root-data=ch; pre=root; ch=getchar(); while (1) { while(ch!=.) { p=(BiTree)malloc(sizeof(BiTNode)); p-data=ch; PushStack(S,p); p=pre-LChild; pre=p; ch=getchar(); }; pre-LChild=NULL; PopStack(S,x); pre=x; while (ch==.) { pre-RChild=NULL; if (!IsEmptyStack(S)) { PopStack(S,x); pre=x; } else return; } } } }

文档评论(0)

1亿VIP精品文档

相关文档