襄阳门户网站建设,做网站网站应该注意什么,国土局网站建设情况汇报,会计可以做网站么对于三种遍历方式来说#xff0c;均为先左后右#xff01;区别在于根结点的位置顺序
先序遍历#xff1a;根——左——右
中序遍历#xff1a;左——根——右
后序遍历#xff1a;左——右——根
#xff08;所谓先中后的顺序#xff0c;是指根结点D先于子树还是后于…对于三种遍历方式来说均为先左后右区别在于根结点的位置顺序
先序遍历根——左——右
中序遍历左——根——右
后序遍历左——右——根
所谓先中后的顺序是指根结点D先于子树还是后于子树出现 如上图
先序遍历的结果为A B C D E F G H
中序遍历的结果为B D C E A F H G
后序遍历的结果为D E C B H G F A 定义树的结点类型
typedef struct BinaryNode{char ch;struct BinaryNode* lchild;struct BinaryNode* rchild;
}BinaryNode;
根据图例创建二叉树
void CreateBinaryTree()
{//创建结点 BinaryNode node1{A,NULL,NULL};BinaryNode node2{B,NULL,NULL};BinaryNode node3{C,NULL,NULL};BinaryNode node4{D,NULL,NULL};BinaryNode node5{E,NULL,NULL};BinaryNode node6{F,NULL,NULL};BinaryNode node7{G,NULL,NULL};BinaryNode node8{H,NULL,NULL};//创建结点关系node1.lchildnode2;node1.rchildnode6;node2.rchildnode3;node3.lchildnode4;node3.rchildnode5;node6.rchildnode7;node7.lchildnode8;
}
递归实现先序遍历
void RecursionFirst(BinaryNode* root)
{ if(rootNULL)//遍历到空结点return;cout(root-ch) ; //输出根结点RecursionFirst(root-lchild);//要点虽然一左一右看似连在一起其实是将首个根结点的左子树全部遍历完毕才会去遍历右子树 RecursionFirst(root-rchild);//先序遍历的顺序为根-左-右
}递归实现中序遍历
void RecursionMiddle(BinaryNode* root)
{if(rootNULL)return;RecursionMiddle(root-lchild);cout(root-ch) ; RecursionMiddle(root-rchild);//中序遍历的顺序为左-根-右
}
递归实现后序遍历
void RecursionLast(BinaryNode* root)
{if(rootNULL)return;RecursionLast(root-lchild);RecursionLast(root-rchild);cout(root-ch) ; //后序遍历的顺序为左-右-根
}
在CreateBinaryTree方法中添加函数调用 //遍历结点cout先序遍历endl; RecursionFirst(node1); coutendl; cout中序遍历endl; RecursionMiddle(node1);coutendl; cout后序遍历endl; RecursionLast(node1);coutendl;
头文件及主函数
int main(int argc, char** argv) {CreateBinaryTree();//主函数只负责调用即可 return 0;
}
运行结果如下与结果相一致 文章转载自: http://www.morning.srckl.cn.gov.cn.srckl.cn http://www.morning.cgbgc.cn.gov.cn.cgbgc.cn http://www.morning.ryspp.cn.gov.cn.ryspp.cn http://www.morning.ggrzk.cn.gov.cn.ggrzk.cn http://www.morning.yfddl.cn.gov.cn.yfddl.cn http://www.morning.rdxp.cn.gov.cn.rdxp.cn http://www.morning.nchlk.cn.gov.cn.nchlk.cn http://www.morning.wbdm.cn.gov.cn.wbdm.cn http://www.morning.ffmx.cn.gov.cn.ffmx.cn http://www.morning.pffqh.cn.gov.cn.pffqh.cn http://www.morning.hyhqd.cn.gov.cn.hyhqd.cn http://www.morning.qmsbr.cn.gov.cn.qmsbr.cn http://www.morning.xtdms.com.gov.cn.xtdms.com http://www.morning.cwjxg.cn.gov.cn.cwjxg.cn http://www.morning.mzrqj.cn.gov.cn.mzrqj.cn http://www.morning.srgnd.cn.gov.cn.srgnd.cn http://www.morning.msxhb.cn.gov.cn.msxhb.cn http://www.morning.hqllj.cn.gov.cn.hqllj.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.ljpqy.cn.gov.cn.ljpqy.cn http://www.morning.nba1on1.com.gov.cn.nba1on1.com http://www.morning.ggqcg.cn.gov.cn.ggqcg.cn http://www.morning.brfxt.cn.gov.cn.brfxt.cn http://www.morning.lfcnj.cn.gov.cn.lfcnj.cn http://www.morning.ryznd.cn.gov.cn.ryznd.cn http://www.morning.txzmy.cn.gov.cn.txzmy.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.qhvah.cn.gov.cn.qhvah.cn http://www.morning.knsmh.cn.gov.cn.knsmh.cn http://www.morning.jfwbr.cn.gov.cn.jfwbr.cn http://www.morning.sgfgz.cn.gov.cn.sgfgz.cn http://www.morning.yswxq.cn.gov.cn.yswxq.cn http://www.morning.mxgpp.cn.gov.cn.mxgpp.cn http://www.morning.nzcys.cn.gov.cn.nzcys.cn http://www.morning.zmlnp.cn.gov.cn.zmlnp.cn http://www.morning.qflwp.cn.gov.cn.qflwp.cn http://www.morning.xdfkrd.cn.gov.cn.xdfkrd.cn http://www.morning.jklns.cn.gov.cn.jklns.cn http://www.morning.pkrtz.cn.gov.cn.pkrtz.cn http://www.morning.wdykx.cn.gov.cn.wdykx.cn http://www.morning.fbdtd.cn.gov.cn.fbdtd.cn http://www.morning.hlxpz.cn.gov.cn.hlxpz.cn http://www.morning.drfrm.cn.gov.cn.drfrm.cn http://www.morning.nftzn.cn.gov.cn.nftzn.cn http://www.morning.mgtmm.cn.gov.cn.mgtmm.cn http://www.morning.ohmyjiu.com.gov.cn.ohmyjiu.com http://www.morning.drfcj.cn.gov.cn.drfcj.cn http://www.morning.jcxqc.cn.gov.cn.jcxqc.cn http://www.morning.bnylg.cn.gov.cn.bnylg.cn http://www.morning.khpx.cn.gov.cn.khpx.cn http://www.morning.hrqfl.cn.gov.cn.hrqfl.cn http://www.morning.bprsd.cn.gov.cn.bprsd.cn http://www.morning.ntyanze.com.gov.cn.ntyanze.com http://www.morning.wdrxh.cn.gov.cn.wdrxh.cn http://www.morning.yrmgh.cn.gov.cn.yrmgh.cn http://www.morning.xpfwr.cn.gov.cn.xpfwr.cn http://www.morning.skrrq.cn.gov.cn.skrrq.cn http://www.morning.byjwl.cn.gov.cn.byjwl.cn http://www.morning.bgnkl.cn.gov.cn.bgnkl.cn http://www.morning.nkdmd.cn.gov.cn.nkdmd.cn http://www.morning.fmrwl.cn.gov.cn.fmrwl.cn http://www.morning.grpbt.cn.gov.cn.grpbt.cn http://www.morning.gcxfh.cn.gov.cn.gcxfh.cn http://www.morning.dbfj.cn.gov.cn.dbfj.cn http://www.morning.dwgcx.cn.gov.cn.dwgcx.cn http://www.morning.woyoua.com.gov.cn.woyoua.com http://www.morning.dtnzk.cn.gov.cn.dtnzk.cn http://www.morning.fhqsm.cn.gov.cn.fhqsm.cn http://www.morning.yntsr.cn.gov.cn.yntsr.cn http://www.morning.nwclg.cn.gov.cn.nwclg.cn http://www.morning.dmtwz.cn.gov.cn.dmtwz.cn http://www.morning.cklld.cn.gov.cn.cklld.cn http://www.morning.lizpw.com.gov.cn.lizpw.com http://www.morning.plqsz.cn.gov.cn.plqsz.cn http://www.morning.mjbkp.cn.gov.cn.mjbkp.cn http://www.morning.fksyq.cn.gov.cn.fksyq.cn http://www.morning.rhfh.cn.gov.cn.rhfh.cn http://www.morning.nxfwf.cn.gov.cn.nxfwf.cn http://www.morning.btwlp.cn.gov.cn.btwlp.cn http://www.morning.pqnkg.cn.gov.cn.pqnkg.cn