教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 学前教育 >

数据结构单元练习7(6)

来源:网络收集 时间:2026-08-28
导读: if (T!=NULL) { top++; stack[top]=T; while(top>-1) { T=stack[top]; top--; if (T->child!=NULL||T->rchild!=NULL) { // 交换结点的左右指针 temp=T->lchild; T->lchild=T->rchild; T->rchild=temp; } if (T->lch

if (T!=NULL)

{ top++; stack[top]=T; while(top>-1) { T=stack[top]; top--;

if (T->child!=NULL||T->rchild!=NULL)

{ // 交换结点的左右指针

temp=T->lchild; T->lchild=T->rchild; T->rchild=temp; }

if (T->lchild!=NULL) { top++;

stack[top]=T->lchild; }

if (T->rchild!=NULL) { top++;

stack[top]=T->rchild;

}

}

} } main()

{ int I,j,k,l; printf(“\\n”); root=CreateBinTree(); Inorder (root); i=CountNode (root); j=CountLeafs (root); k=Depth (root); l=Width (root);

printf(“\\nThe Node ’s Number:%d”,i); printf(“\\nThe Leafs’s Number:%d”,j); printf(“\\nThe Depth is:%d”,k); printf(“\\nThe width is:%d”,l); Swap(root);

Printf(“\\nThe swapTree is:”); Inorder(root); }

7.解:

int h=-1,lh=1,count=0;charx=’c’; // 赋初值 Level (BinTree T,int h,int lh) // 求X结点在树只的层树 { if (T==Null)

h=0; else

if (T->data==x) { h=lh; count=h;} else { h++;

Level(T->lchild,h,lh); if (h==-1)

Level(T->rchild,h,lh);

} } main()

{ BinTree *(*newroot); Printf(“\\n”); Root=CreateBinTree(); Inorder(root);

Printf(“\\n”); Level(root,h,lh); Printf(“%d”,count); }

模拟考题

一. 读程序,写出运行结果

1.二叉树的结构如图所示,试写出执行下列算法后的输出结果: 。

(用大写的英文字母表示,字母之间不要任何间隔符号,最后一个字母后面也不要间隔符号)

typedef struct BT { datatype data; BT *lchild; BT *rchild; }BT;

void Preorder(BT *T) { if (T!=NULL) { cout<< T->data; Preorder(T->lchild); Preorder(T->rchild); } }

解:ABCEDFG 先序遍历

2.二叉树的结构如图所示,试写出执行下列算法后的输出结果: 。

C E BF A D G

数据结构单元练习7(6).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/591622.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)