6.4二叉树的遍历算法描述.pptVIP

  • 11
  • 0
  • 约3.81千字
  • 约 35页
  • 2018-12-22 发布于湖北
  • 举报
Status Preorderelem (BiTree T, ElemType x, BiTree p) {// 若二叉树中存在和x相同的元素,则p指向该结点并返回true if (T) { if (T-data==x) { p=T; return OK,} else { if (Preorderelem (T-lchild, x, p) return OK; else return(Preorderelem (T-rchild, x, p)) ; }//else }//if else return FALSE; } 算法基本思想: 先序(或中序或后序)遍历二叉树,在遍历过程中查找叶子结点并计数。在遍历算法中增加一个参数用于计数,并将算法中“VISIT()” 的操作改为:若是叶子,则计数器增1。 int TreeDepth (BiTree T ){ // 返回二叉树的深度 if ( !T ) depth = 0; else { depthLeft = TreeDepth( T-lchild ); depthRight= TreeDepth( T-rchild ); depth = 1 + (depthLeft dep

文档评论(0)

1亿VIP精品文档

相关文档