云题海 - 专业文章范例文档资料分享平台

当前位置:首页 > 数据结构小课堂(6.7讲师版)

数据结构小课堂(6.7讲师版)

  • 62 次阅读
  • 3 次下载
  • 2025/12/3 0:07:39

45 45 40 22 48 80 22 40 48 80

78 四、编程题

1.根据函数的定义,利用递归完成计算二叉树大小和高度的函数。 (1)intBinaryTree :: Size ( constBinTreeNode *t ) const { if ( t == NULL ) return 0; else

return 1 + Size ( t→leftChild )

+ Size ( t→rightChild );

}

(2)intBinaryTree :: Depth ( constBinTreeNode *t ) const { if ( t == NULL ) return -1;

else return 1 + Max ( Depth ( t→leftChild ),

Depth ( t→rightChild ) ); }

2.实现在链表上进行插入操作的功能函数。 intList::Insert ( constintx, constinti ) {

// Insert a node with data equal to x after the i-1’th ListNode*p = first; intk = 0; while ( p != NULL && k

{ p = p→link; k++; } // Locate i-1’th element if ( i<0 || p == NULL&&i!=0) { cout<< “无效的插入位置!\\n”; return 0; }

ListNode *newnode= new ListNode(x, NULL); // Allocate memory for the new node if (i == 0 ) { //Insert in the front of list newnode→link = first; first = newnode; }

else { //else newnode→link = p→link; p→link = newnode;

}

if(newnode->link==NULL) last=newnode; return 1; }

3.借助栈,实现二叉树的中序遍历。

template void BinaryTree :: InOrderTraverse () { stack S; BiTreeNode * p;

S.makeEmpty( ); p = root; //初始化 do{

while ( p ) { S.push(p); p = p→leftChild; }

if ( !S.empty( ) ) { //栈非空

p = S.top( ); S.pop( ); //退栈 cout<

p = p→rightChild; //向右链走 }

} while ( p || !S.empty( ) ); }

搜索更多关于: 数据结构小课堂(6.7讲师版) 的文档
  • 收藏
  • 违规举报
  • 版权认领
下载文档10.00 元 加入VIP免费下载
推荐下载
本文作者:...

共分享92篇相关文档

文档简介:

45 45 40 22 48 80 22 40 48 80 78 四、编程题 1.根据函数的定义,利用递归完成计算二叉树大小和高度的函数。 (1)intBinaryTree :: Size ( constBinTreeNode *t ) const { if ( t == NULL ) return 0; else return 1 + Size ( t→leftChild ) + Size ( t→rightChild ); } (2)intBinaryTree :: Depth ( constBinTreeNode *t ) const { i

× 游客快捷下载通道(下载后可以自由复制和排版)
单篇付费下载
限时特价:10 元/份 原价:20元
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能“只有目录或者内容不全”等情况,请下载之前注意辨别,如果您已付费且无法下载或内容有问题,请联系我们协助你处理。
微信:fanwen365 QQ:370150219
Copyright © 云题海 All Rights Reserved. 苏ICP备16052595号-3 网站地图 客服QQ:370150219 邮箱:370150219@qq.com