(6.3.1)--ch6-3二叉树的应用.pdf
《(6.3.1)--ch6-3二叉树的应用.pdf》由会员分享,可在线阅读,更多相关《(6.3.1)--ch6-3二叉树的应用.pdf(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 Data Structures and AlgorithmsApplication of Binary TreesData Structures and Algorithms Data Structures and Algorithms Trees and Binary TreesTrees and Binary TreesApplication of traversal algorithm1)Output binary tree nodes2)Output binary tree leaf nodes3)Count the number of leaf nodes 4)Calculate
2、the height of binary tree5)Output the path from leaf node to root node6)Print binary tree by tree6.3 Binary Tree TraversalData Structures and Algorithms Data Structures and Algorithms Trees and Binary TreesTrees and Binary TreesApplication of traversal algorithmABCDEFCDA BFE3)count the number of lea
3、f nodes12LeafCountData Structures and Algorithms Data Structures and Algorithms Trees and Binary TreesTrees and Binary Treesint LeafCount=0;void LeafNum(BiTree root)if(root!=NULL)LeafCount+;LeafNum(root-lchild);LeafNum(root-rchild);Method 1:Set LeafCount as a global variable,and initialize to 0.if(r
4、oot-lchild=NULL&root-rchild=NULL)3)Application of traversal algorithmData Structures and Algorithms Data Structures and Algorithms Trees and Binary TreesTrees and Binary TreesApplication of traversal algorithmABCDEF3)count the number of leaf nodesIf its an empty tree,return 0If there are no left and
5、 right children,return 1Otherwise,it is the sum of the number of leaf nodes of the left and right subtreesData Structures and Algorithms Data Structures and Algorithms Trees and Binary TreesTrees and Binary Treesint leaf(BiTree root)int LeafCount;if(root=NULL)LeafCount=0;else if(root-lchild=NULL)&(r
6、oot-rchild=NULL)LeafCount=1;else LeafCount=leaf(root-lchild)+leaf(root-rchild);return LeafCount;Method 2:use function return valueApplication of traversal algorithm3)Data Structures and Algorithms Data Structures and Algorithms Trees and Binary TreesTrees and Binary TreesOther applicationsApplicatio
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 6.3 ch6 二叉 应用
限制150内