判断题:

选择题:

函数题:

  6-1 求二叉树高度:

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef char ElementType;
  5. typedef struct TNode *Position;
  6. typedef Position BinTree;
  7. struct TNode{
  8. ElementType Data;
  9. BinTree Left;
  10. BinTree Right;
  11. };
  12.  
  13. BinTree CreatBinTree(); /* 实现细节忽略 */
  14. int GetHeight( BinTree BT );
  15.  
  16. int main()
  17. {
  18. BinTree BT = CreatBinTree();
  19. printf("%d\n", GetHeight(BT));
  20. return 0;
  21. }
  22. /* 你的代码将被嵌在这里 */

代码:

  1. int GetHeight(BinTree BT)
  2. {
  3. if(BT == NULL)//判断是否为空
  4. return 0;
  5. int l,r;
  6. l=GetHeight(BT->Left);//递归
  7. r=GetHeight(BT->Right);
  8. return l>=r?l+1:r+1;
  9. }

  6-2 二叉树的遍历:

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef char ElementType;
  5. typedef struct TNode *Position;
  6. typedef Position BinTree;
  7. struct TNode{
  8. ElementType Data;
  9. BinTree Left;
  10. BinTree Right;
  11. };
  12.  
  13. BinTree CreatBinTree(); /* 实现细节忽略 */
  14. void InorderTraversal( BinTree BT );
  15. void PreorderTraversal( BinTree BT );
  16. void PostorderTraversal( BinTree BT );
  17. void LevelorderTraversal( BinTree BT );
  18.  
  19. int main()
  20. {
  21. BinTree BT = CreatBinTree();
  22. printf("Inorder:"); InorderTraversal(BT); printf("\n");
  23. printf("Preorder:"); PreorderTraversal(BT); printf("\n");
  24. printf("Postorder:"); PostorderTraversal(BT); printf("\n");
  25. printf("Levelorder:"); LevelorderTraversal(BT); printf("\n");
  26. return 0;
  27. }
  28. /* 你的代码将被嵌在这里 */

代码:

  1. void InorderTraversal( BinTree BT )//前三个依次递归,只需注意每一次的输出位置即可
  2. {
  3. if(BT)
  4. {
  5. if(BT->Left)
  6. InorderTraversal(BT->Left);
  7. printf(" %c",BT->Data);
  8. if(BT->Right)
  9. InorderTraversal(BT->Right);
  10. }
  11. }
  12. void PreorderTraversal( BinTree BT )
  13. {
  14. if(BT)
  15. {
  16. printf(" %c",BT->Data);
  17. if(BT->Left)
  18. PreorderTraversal(BT->Left);
  19. if(BT->Right)
  20. PreorderTraversal(BT->Right);
  21. }
  22. }
  23. void PostorderTraversal( BinTree BT )
  24. {
  25. if (BT)
  26. {
  27. if (BT->Left)
  28. PostorderTraversal(BT->Left);
  29. if (BT->Right)
  30. PostorderTraversal(BT->Right);
  31. printf(" %c", BT->Data);
  32. }
  33. }
  34. void LevelorderTraversal( BinTree BT )
  35. {
  36. BinTree bt[10001],root;
  37. int h=0,t=0;
  38. if(BT)
  39. {
  40. bt[t++]=BT;
  41. while(h!=t)
  42. {
  43. root=bt[h++];
  44. printf(" %c", root->Data);
  45. if(root->Left)
  46. bt[t++] = root->Left;
  47. if(root->Right)
  48. bt[t++] = root->Right;
  49. }
  50. }
  51. }

  6-3 先序输出叶结点:

裁判测试程序样例:

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef char ElementType;
  5. typedef struct TNode *Position;
  6. typedef Position BinTree;
  7. struct TNode{
  8. ElementType Data;
  9. BinTree Left;
  10. BinTree Right;
  11. };
  12.  
  13. BinTree CreatBinTree(); /* 实现细节忽略 */
  14. void PreorderPrintLeaves( BinTree BT );
  15.  
  16. int main()
  17. {
  18. BinTree BT = CreatBinTree();
  19. printf("Leaf nodes are:");
  20. PreorderPrintLeaves(BT);
  21. printf("\n");
  22.  
  23. return 0;
  24. }
  25. /* 你的代码将被嵌在这里 */

代码:

  1. void PreorderPrintLeaves( BinTree BT )
  2. {
  3. if(BT==NULL)//若为空,只退回当前函数
  4. return ;
  5. PreorderPrintLeaves(BT->Left);//递归调用
  6. PreorderPrintLeaves(BT->Right);
  7. if(BT->Left==NULL&&BT->Right==NULL)
  8. printf(" %c",BT->Data);
  9. //PreorderPrintLeaves(BT->Left);//递归调用
  10. //PreorderPrintLeaves(BT->Right);
  11. }

  7-1 根据后序和中序遍历输出先序遍历:

输入样例:

  1. 7
  2. 2 3 1 5 7 6 4
  3. 1 2 3 4 5 6 7

输出样例:

  1. Preorder: 4 1 3 2 6 5 7

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. typedef struct BiTNode
  4. {
  5. int Data;
  6. struct BiTNode *lchild;
  7. struct BiTNode *rchild;
  8. }BiTNode, *BiTree;
  9. BiTree PlusTree(int *l, int *r, int n)
  10. {
  11. if(n==0)
  12. return NULL;
  13. else
  14. {
  15. BiTree t=new BiTNode;
  16. t->Data=r[n-1];
  17. int i=0;
  18. for(i=0;i<n;i++)
  19. {
  20. if(r[n-1]==l[i])
  21. break;
  22. }
  23. t->lchild = PlusTree(l, r, i);
  24. t->rchild = PlusTree(l+i+1, r+i, n-i-1);
  25. return t;
  26. }
  27. }
  28. void PreorderTraversal(BiTree BT)
  29. {
  30. if(BT == NULL)
  31. return;
  32. printf(" %d", BT->Data);
  33. PreorderTraversal(BT->lchild);
  34. PreorderTraversal(BT->rchild);
  35. }
  36. int main()
  37. {
  38. int n;
  39. scanf("%d",&n);
  40. int a[35],b[35];
  41. for(int i=0;i<n;i++)
  42. cin>>b[i];
  43. for(int j=0;j<n;j++)
  44. cin>>a[j];
  45. BiTree tree;
  46. tree = PlusTree(a, b, n);
  47. printf("Preorder:");
  48. PreorderTraversal(tree);
  49. printf("\n");
  50. return 0;
  51. }

  7-2 树的同构:

输入样例1:

  1. 8
  2. A 1 2
  3. B 3 4
  4. C 5 -
  5. D - -
  6. E 6 -
  7. G 7 -
  8. F - -
  9. H - -
  10. 8
  11. G - 4
  12. B 7 6
  13. F - -
  14. A 5 1
  15. H - -
  16. C 0 -
  17. D - -
  18. E 2 -

输出样例:

  1. Yes

输入样例:

  1. 8
  2. B 5 7
  3. F - -
  4. A 0 3
  5. C 6 -
  6. H - -
  7. D - -
  8. G 4 -
  9. E 1 -
  10. 8
  11. D 6 -
  12. B 5 -
  13. E - -
  14. H - -
  15. C 0 2
  16. G - 3
  17. F - -
  18. A 1 4

输出样例:

  1. No

代码:

  1. #include<cstdio>
  2. typedef int Tree;
  3. struct TNode{
  4. Tree left, right;
  5. char data;
  6. int flag;
  7. }T1[15], T2[15];
  8. int flag[15];
  9. Tree BuildTree( struct TNode T[]){
  10. Tree R=-1, cl, cr;
  11. int n;
  12. scanf("%d\n", &n);
  13. for(int i=0; i<n; i++)flag[i]=0;
  14. for(int i=0; i<n; i++){
  15. scanf("%c %c %c\n", &T[i].data, &cl, &cr);
  16. if(cl!='-'){
  17. T[i].left=cl - '0';
  18. flag[T[i].left]=1;
  19. }else{
  20. T[i].left=-1;
  21. }
  22. if(cr!='-'){
  23. T[i].right= cr-'0';
  24. flag[T[i].right]=1;
  25. }else{
  26. T[i].right=-1;
  27. }
  28. }
  29. for(int i=0; i<n; i++)
  30. if(flag[i]==0){
  31. R=i;
  32. break;
  33. }
  34. return R;
  35. }
  36. int PanDuan(Tree R1, Tree R2){
  37. if(R1==-1 && R2==-1)return 1;
  38. if((R1==-1 && R2!=-1) || (R2==-1 && R1!=-1))return 0;
  39. if(T1[R1].data!=T2[R2].data)return 0;
  40. if(T1[R1].left==-1 && T2[R2].left==-1)
  41. return PanDuan(T1[R1].right, T2[R2].right);
  42. if((T1[R1].left!=-1&&T2[R2].left!=-1)&&(T1[T1[R1].left].data==T2[T2[R2].left].data)){
  43. return(PanDuan(T1[R1].left, T2[R2].left)&&PanDuan(T1[R1].right, T2[R2].right));
  44. }else{
  45. return(PanDuan(T1[R1].right, T2[R2].left)&&PanDuan(T1[R1].left, T2[R2].right));
  46. }
  47. }
  48. int main(){
  49. Tree a, b;
  50. a=BuildTree(T1);
  51. b=BuildTree(T2);
  52. if(PanDuan(a, b)){
  53. printf("Yes");
  54. }else{
  55. printf("No");
  56. }
  57. return 0;
  58. }

  7-3 修理牧场:

输入样例:

  1. 8
  2. 4 5 1 2 1 3 1 1

输出样例:

  1. 49

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int main()
  4. {
  5. int n;
  6. scanf("%d",&n);
  7. priority_queue<int, vector<int>, greater<int> > w;
  8. int a[n];
  9. int i=0;
  10. int sum=0; //待求最小值
  11. for(;i<n;i++)
  12. {
  13. scanf("%d",&a[i]);
  14. w.push(a[i]);
  15. }
  16. int x;//存取现小顶堆
  17. int y;//存取新小顶堆
  18. while(!w.empty()) //最短的木头二合一
  19. {
  20. x=w.top();
  21. w.pop(); //用完移除
  22.  
  23. if(w.empty()) //移除x后没有元素了,说明x是木头总长,不再循环
  24. {
  25. break;
  26. }
  27. y=w.top();
  28. w.pop();
  29. x+=y;
  30. sum+=x;
  31. w.push(x); //新木头放入堆中
  32. }
  33. printf("%d",sum);
  34. }

  7-4 完全二叉搜索树:

输入样例:

  1. 10
  2. 1 2 3 4 5 6 7 8 9 0

输出样例:

  1. 6 3 8 1 5 7 9 0 2 4

代码:

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n, flag;
  4. int a[1001];
  5. int b[1001];
  6. void DFS(int aa)
  7. {
  8. if(aa>n)
  9. return ;
  10. DFS(2*aa);
  11. b[aa]=++flag;
  12. DFS(2*aa+1);
  13. }
  14. int main()
  15. {
  16. scanf("%d",&n);
  17. for(int i=1;i<=n;i++)
  18. {
  19. scanf("%d",&a[i]);
  20. }
  21. sort(a+1, a+n+1);
  22. DFS(1);
  23. for(int i=1;i<=n;i++)
  24. {
  25. if(i==1)
  26. printf("%d",a[b[i]]);
  27. else
  28. printf(" %d",a[b[i]]);
  29. }
  30. }

SDUST数据结构 - chap6 树与二叉树的更多相关文章

  1. python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)

    python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...

  2. Java数据结构之树和二叉树(2)

    从这里始将要继续进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来 ...

  3. Java数据结构之树和二叉树

    从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...

  4. 【PHP数据结构】树和二叉树

    树的概念其实非常地广泛,也非常地常见,大家见到这个词千万不要惊慌,因为真的每天你都能见到树结构在我们生活中的应用.比如说公司的组织结构: 另外像我们家里的族谱,或者说是我们的家庭结构,也是一个典型的树 ...

  5. 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)

    本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...

  6. python数据结构之树(二叉树的遍历)

    树是数据结构中非常重要的一种,主要的用途是用来提高查找效率,对于要重复查找的情况效果更佳,如二叉排序树.FP-树. 本篇学习笔记来自:二叉树及其七种遍历方式.python遍历与非遍历方式实现二叉树 介 ...

  7. javascript实现数据结构: 树和二叉树的应用--最优二叉树(赫夫曼树),回溯法与树的遍历--求集合幂集及八皇后问题

    赫夫曼树及其应用 赫夫曼(Huffman)树又称最优树,是一类带权路径长度最短的树,有着广泛的应用. 最优二叉树(Huffman树) 1 基本概念 ① 结点路径:从树中一个结点到另一个结点的之间的分支 ...

  8. javascript实现数据结构: 树和二叉树,二叉树的遍历和基本操作

    树型结构是一类非常重要的非线性结构.直观地,树型结构是以分支关系定义的层次结构. 树在计算机领域中也有着广泛的应用,例如在编译程序中,用树来表示源程序的语法结构:在数据库系统中,可用树来组织信息:在分 ...

  9. 数据结构与算法系列研究五——树、二叉树、三叉树、平衡排序二叉树AVL

    树.二叉树.三叉树.平衡排序二叉树AVL 一.树的定义 树是计算机算法最重要的非线性结构.树中每个数据元素至多有一个直接前驱,但可以有多个直接后继.树是一种以分支关系定义的层次结构.    a.树是n ...

随机推荐

  1. base64 基本使用 和os模块使用

    1  base64 的基本使用 import base64 with open('../static/upload/63bc620d1594779d6a98c53a3a8db1e5.png','rb' ...

  2. mysql多实例启动、关闭

    启动(指定参数文件): [root@mysql01 ~]# mysqld_safe --defaults-file=/data/3306/my.cnf & [root@mysql01 ~]# ...

  3. Kubernetes【K8S】(一):Kubernetes组件

    什么是Kubernetes ​ Kubernetes 是一个可移植的.可扩展的开源平台,用于管理容器化的工作负载和服务,可促进声明式配置和自动化.Kubernetes拥有一个庞大且快速增长的生态系统. ...

  4. Android studio使用OKGO的POST请求访问http失败的解决方法

    前几天在旧手机(版本是Android7)上调试一个app,用OKGO的post请求连接服务器登录一直很正常.今天旧手机不在身边,用自己的手机调试,就出现网络请求失败的问题,弹onError()里自己写 ...

  5. .NET生态系统掠影

    如果你是一名开发人员,想要进入到.NET的世界,你需要知道都有哪些可能.由于.NET Framework是..NET生态系统中最流行的技术,你可以用它来构建各种各样的应用程序,但是最近,出现了一些新的 ...

  6. HTC Vive使用WebVR的方法以及启用后头显无画面的解决方法

    1.下载支持WebVR的浏览器. 笔者使用的是HTC Vive,故下载了Firefox的Nightly版本[下载地址]. 2.Nightly 开启WebVR的步骤[引用自Mozilla VR] 1.从 ...

  7. CCNP之OSPF实验报告

    OSPF实验报告 一.实验要求 1.R4为ISP,其上只能配置IP地址:R4与其它所有直连设备间使用公有IP2.R3--R5/6/7为MGRE环境,R3为中心站点3.整个OSPF环境IP地址为172. ...

  8. LINQ to Entities 不识别方法“System.String ToString(“yyyy-MM-dd”)”

    将Queryable转化为IEnumerable或者直接Tolist()

  9. 【命令】ln命令

    这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件或目录在另外一个位置建立一个同步的链接,默认是链接是硬链接,常用参数是 "-s"  . 对于ln命令,这里 ...

  10. 3.自定义view-TextView变色

    1.效果 2.实现原理 自定义Textview,重写onDraw方法,将画布分成两部分,用不同颜色的画笔画 核心代码: @Override protected void onDraw(Canvas c ...