题目内容
请完成下列二叉树的递归遍历算法#include #include #define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define OVERFLOW -2typedef int status;/*二叉树的二叉链表存储表示*/typedef struct BiNode{char Data;struct BiNode* lChild;struct BiNode* rChild; //左右孩子指针}BiNode,*pBiNode;BiNode *pRoot=NULL;status Visit(char Data){printf("%c",Data);return OK;}/*建立二叉链表*/status CreateTree(BiNode** pTree){//按先序次序输入二叉树中结点值,空格表示空树//构造二叉链表表示的二叉树char ch;scanf("%c",&ch);if(ch==' '){(*pTree)=NULL;}else{(*pTree)=(BiNode*)malloc(sizeof(BiNode));(*pTree)->Data=ch;CreateTree(&((*pTree)->lChild));CreateTree(&((*pTree)->rChild));}return OK;}/*先序遍历二叉树,递归算法*/status PreOrderTraval(BiNode* pTree){if(pTree){if(Visit(pTree->Data)){if(PreOrderTraval(pTree->lChild)){if(PreOrderTraval(pTree->rChild)){return OK;}}}return ERROR;}else{return OK;}}/*中序遍历二叉树,递归算法*/status InOrderTraval(BiNode* pTree){if(pTree){if(InOrderTraval(1)){if(Visit(2)){if(InOrderTraval(3)){return OK;}}}return ERROR;}else{return OK;}}/*后序遍历二叉树,递归算法*/status PostOrderTraval(BiNode* pTree){if(pTree){if(4){if(PostOrderTraval(pTree->rChild)){if(Visit(pTree->Data)){return OK;}}}return ERROR;}else{return OK;}}main(){system("cls");CreateTree(&pRoot);printf("\nPreOrder:");PreOrderTraval(pRoot);printf("\n");printf("\nInOrder:");InOrderTraval(pRoot);printf("\n");printf("\nPostOrder:");PostOrderTraval(pRoot);printf("\n");getch();}
查看答案
搜索结果不匹配?点我反馈
更多问题