标签:style blog io ar os sp on 2014 log
huangjing
二叉树的的建立方式为前序 二叉树有三种遍历 前序遍历(NLR) 中序遍历(LNR) 后续遍历(LRN)
非递归的算法明天补上
代码为:
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<cstdlib> #include<map> #include<vector> #include<cmath> #include<string> #include<queue> #define eps 1e-9 #define ll long long #define INF 0x3f3f3f3f using namespace std; typedef struct BITNode { char val; struct BITNode *left,*right; }BITNode,*BITtree; void buildtree(BITtree &T) { char ss; scanf("%c",&ss); if(ss=='#') { T=NULL; return; } else { T=(BITtree)malloc(sizeof(BITNode)); T->val=ss; buildtree(T->left); buildtree(T->right); } }//先序建立二叉树 void visit(BITtree T) { if(T->val!='#') printf("%c",T->val); } void pre_visit(BITtree T) { if(T!=NULL) { visit(T); pre_visit(T->left); pre_visit(T->right); } }//遍历方式为NLR void mid_visit(BITtree T) { if(T!=NULL) { mid_visit(T->left); visit(T); mid_visit(T->right); } }//遍历方式为LNR void beh_visit(BITtree T) { if(T!=NULL) { beh_visit(T->left); beh_visit(T->right); visit(T); } }//遍历方式为LRN int main() { BITtree p; buildtree(p); printf("前序遍历为:\n"); pre_visit(p); cout<<endl; printf("中序遍历为:\n"); mid_visit(p); cout<<endl; printf("后序遍历为:\n"); beh_visit(p); } /* ABC##DE#G##F### -+a##*b##-c##d##/e##f## */
标签:style blog io ar os sp on 2014 log
原文地址:http://blog.csdn.net/u014303647/article/details/41053875