SDUST数据结构 - chap6 树与二叉树
判断题:
选择题:
函数题:
6-1 求二叉树高度:
裁判测试程序样例:
- #include <stdio.h>
- #include <stdlib.h>
- typedef char ElementType;
- typedef struct TNode *Position;
- typedef Position BinTree;
- struct TNode{
- ElementType Data;
- BinTree Left;
- BinTree Right;
- };
- BinTree CreatBinTree(); /* 实现细节忽略 */
- int GetHeight( BinTree BT );
- int main()
- {
- BinTree BT = CreatBinTree();
- printf("%d\n", GetHeight(BT));
- return 0;
- }
- /* 你的代码将被嵌在这里 */
代码:


- int GetHeight(BinTree BT)
- {
- if(BT == NULL)//判断是否为空
- return 0;
- int l,r;
- l=GetHeight(BT->Left);//递归
- r=GetHeight(BT->Right);
- return l>=r?l+1:r+1;
- }
6-2 二叉树的遍历:
裁判测试程序样例:
- #include <stdio.h>
- #include <stdlib.h>
- typedef char ElementType;
- typedef struct TNode *Position;
- typedef Position BinTree;
- struct TNode{
- ElementType Data;
- BinTree Left;
- BinTree Right;
- };
- BinTree CreatBinTree(); /* 实现细节忽略 */
- void InorderTraversal( BinTree BT );
- void PreorderTraversal( BinTree BT );
- void PostorderTraversal( BinTree BT );
- void LevelorderTraversal( BinTree BT );
- int main()
- {
- BinTree BT = CreatBinTree();
- printf("Inorder:"); InorderTraversal(BT); printf("\n");
- printf("Preorder:"); PreorderTraversal(BT); printf("\n");
- printf("Postorder:"); PostorderTraversal(BT); printf("\n");
- printf("Levelorder:"); LevelorderTraversal(BT); printf("\n");
- return 0;
- }
- /* 你的代码将被嵌在这里 */
代码:


- void InorderTraversal( BinTree BT )//前三个依次递归,只需注意每一次的输出位置即可
- {
- if(BT)
- {
- if(BT->Left)
- InorderTraversal(BT->Left);
- printf(" %c",BT->Data);
- if(BT->Right)
- InorderTraversal(BT->Right);
- }
- }
- void PreorderTraversal( BinTree BT )
- {
- if(BT)
- {
- printf(" %c",BT->Data);
- if(BT->Left)
- PreorderTraversal(BT->Left);
- if(BT->Right)
- PreorderTraversal(BT->Right);
- }
- }
- void PostorderTraversal( BinTree BT )
- {
- if (BT)
- {
- if (BT->Left)
- PostorderTraversal(BT->Left);
- if (BT->Right)
- PostorderTraversal(BT->Right);
- printf(" %c", BT->Data);
- }
- }
- void LevelorderTraversal( BinTree BT )
- {
- BinTree bt[10001],root;
- int h=0,t=0;
- if(BT)
- {
- bt[t++]=BT;
- while(h!=t)
- {
- root=bt[h++];
- printf(" %c", root->Data);
- if(root->Left)
- bt[t++] = root->Left;
- if(root->Right)
- bt[t++] = root->Right;
- }
- }
- }
6-3 先序输出叶结点:
裁判测试程序样例:
- #include <stdio.h>
- #include <stdlib.h>
- typedef char ElementType;
- typedef struct TNode *Position;
- typedef Position BinTree;
- struct TNode{
- ElementType Data;
- BinTree Left;
- BinTree Right;
- };
- BinTree CreatBinTree(); /* 实现细节忽略 */
- void PreorderPrintLeaves( BinTree BT );
- int main()
- {
- BinTree BT = CreatBinTree();
- printf("Leaf nodes are:");
- PreorderPrintLeaves(BT);
- printf("\n");
- return 0;
- }
- /* 你的代码将被嵌在这里 */
代码:


- void PreorderPrintLeaves( BinTree BT )
- {
- if(BT==NULL)//若为空,只退回当前函数
- return ;
- PreorderPrintLeaves(BT->Left);//递归调用
- PreorderPrintLeaves(BT->Right);
- if(BT->Left==NULL&&BT->Right==NULL)
- printf(" %c",BT->Data);
- //PreorderPrintLeaves(BT->Left);//递归调用
- //PreorderPrintLeaves(BT->Right);
- }
7-1 根据后序和中序遍历输出先序遍历:
输入样例:
- 7
- 2 3 1 5 7 6 4
- 1 2 3 4 5 6 7
输出样例:
- Preorder: 4 1 3 2 6 5 7
代码:


- #include<bits/stdc++.h>
- using namespace std;
- typedef struct BiTNode
- {
- int Data;
- struct BiTNode *lchild;
- struct BiTNode *rchild;
- }BiTNode, *BiTree;
- BiTree PlusTree(int *l, int *r, int n)
- {
- if(n==0)
- return NULL;
- else
- {
- BiTree t=new BiTNode;
- t->Data=r[n-1];
- int i=0;
- for(i=0;i<n;i++)
- {
- if(r[n-1]==l[i])
- break;
- }
- t->lchild = PlusTree(l, r, i);
- t->rchild = PlusTree(l+i+1, r+i, n-i-1);
- return t;
- }
- }
- void PreorderTraversal(BiTree BT)
- {
- if(BT == NULL)
- return;
- printf(" %d", BT->Data);
- PreorderTraversal(BT->lchild);
- PreorderTraversal(BT->rchild);
- }
- int main()
- {
- int n;
- scanf("%d",&n);
- int a[35],b[35];
- for(int i=0;i<n;i++)
- cin>>b[i];
- for(int j=0;j<n;j++)
- cin>>a[j];
- BiTree tree;
- tree = PlusTree(a, b, n);
- printf("Preorder:");
- PreorderTraversal(tree);
- printf("\n");
- return 0;
- }
7-2 树的同构:
输入样例1:
- 8
- A 1 2
- B 3 4
- C 5 -
- D - -
- E 6 -
- G 7 -
- F - -
- H - -
- 8
- G - 4
- B 7 6
- F - -
- A 5 1
- H - -
- C 0 -
- D - -
- E 2 -
输出样例:
- Yes
输入样例:
- 8
- B 5 7
- F - -
- A 0 3
- C 6 -
- H - -
- D - -
- G 4 -
- E 1 -
- 8
- D 6 -
- B 5 -
- E - -
- H - -
- C 0 2
- G - 3
- F - -
- A 1 4
输出样例:
- No
代码:


- #include<cstdio>
- typedef int Tree;
- struct TNode{
- Tree left, right;
- char data;
- int flag;
- }T1[15], T2[15];
- int flag[15];
- Tree BuildTree( struct TNode T[]){
- Tree R=-1, cl, cr;
- int n;
- scanf("%d\n", &n);
- for(int i=0; i<n; i++)flag[i]=0;
- for(int i=0; i<n; i++){
- scanf("%c %c %c\n", &T[i].data, &cl, &cr);
- if(cl!='-'){
- T[i].left=cl - '0';
- flag[T[i].left]=1;
- }else{
- T[i].left=-1;
- }
- if(cr!='-'){
- T[i].right= cr-'0';
- flag[T[i].right]=1;
- }else{
- T[i].right=-1;
- }
- }
- for(int i=0; i<n; i++)
- if(flag[i]==0){
- R=i;
- break;
- }
- return R;
- }
- int PanDuan(Tree R1, Tree R2){
- if(R1==-1 && R2==-1)return 1;
- if((R1==-1 && R2!=-1) || (R2==-1 && R1!=-1))return 0;
- if(T1[R1].data!=T2[R2].data)return 0;
- if(T1[R1].left==-1 && T2[R2].left==-1)
- return PanDuan(T1[R1].right, T2[R2].right);
- if((T1[R1].left!=-1&&T2[R2].left!=-1)&&(T1[T1[R1].left].data==T2[T2[R2].left].data)){
- return(PanDuan(T1[R1].left, T2[R2].left)&&PanDuan(T1[R1].right, T2[R2].right));
- }else{
- return(PanDuan(T1[R1].right, T2[R2].left)&&PanDuan(T1[R1].left, T2[R2].right));
- }
- }
- int main(){
- Tree a, b;
- a=BuildTree(T1);
- b=BuildTree(T2);
- if(PanDuan(a, b)){
- printf("Yes");
- }else{
- printf("No");
- }
- return 0;
- }
7-3 修理牧场:
输入样例:
- 8
- 4 5 1 2 1 3 1 1
输出样例:
- 49
代码:


- #include<bits/stdc++.h>
- using namespace std;
- int main()
- {
- int n;
- scanf("%d",&n);
- priority_queue<int, vector<int>, greater<int> > w;
- int a[n];
- int i=0;
- int sum=0; //待求最小值
- for(;i<n;i++)
- {
- scanf("%d",&a[i]);
- w.push(a[i]);
- }
- int x;//存取现小顶堆
- int y;//存取新小顶堆
- while(!w.empty()) //最短的木头二合一
- {
- x=w.top();
- w.pop(); //用完移除
- if(w.empty()) //移除x后没有元素了,说明x是木头总长,不再循环
- {
- break;
- }
- y=w.top();
- w.pop();
- x+=y;
- sum+=x;
- w.push(x); //新木头放入堆中
- }
- printf("%d",sum);
- }
7-4 完全二叉搜索树:
输入样例:
- 10
- 1 2 3 4 5 6 7 8 9 0
输出样例:
- 6 3 8 1 5 7 9 0 2 4
代码:


- #include<bits/stdc++.h>
- using namespace std;
- int n, flag;
- int a[1001];
- int b[1001];
- void DFS(int aa)
- {
- if(aa>n)
- return ;
- DFS(2*aa);
- b[aa]=++flag;
- DFS(2*aa+1);
- }
- int main()
- {
- scanf("%d",&n);
- for(int i=1;i<=n;i++)
- {
- scanf("%d",&a[i]);
- }
- sort(a+1, a+n+1);
- DFS(1);
- for(int i=1;i<=n;i++)
- {
- if(i==1)
- printf("%d",a[b[i]]);
- else
- printf(" %d",a[b[i]]);
- }
- }
SDUST数据结构 - chap6 树与二叉树的更多相关文章
- python数据结构之树和二叉树(先序遍历、中序遍历和后序遍历)
python数据结构之树和二叉树(先序遍历.中序遍历和后序遍历) 树 树是\(n\)(\(n\ge 0\))个结点的有限集.在任意一棵非空树中,有且只有一个根结点. 二叉树是有限个元素的集合,该集合或 ...
- Java数据结构之树和二叉树(2)
从这里始将要继续进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来 ...
- Java数据结构之树和二叉树
从这里开始将要进行Java数据结构的相关讲解,Are you ready?Let's go~~ Java中的数据结构模型可以分为一下几部分: 1.线性结构 2.树形结构 3.图形或者网状结构 接下来的 ...
- 【PHP数据结构】树和二叉树
树的概念其实非常地广泛,也非常地常见,大家见到这个词千万不要惊慌,因为真的每天你都能见到树结构在我们生活中的应用.比如说公司的组织结构: 另外像我们家里的族谱,或者说是我们的家庭结构,也是一个典型的树 ...
- 【Java】 大话数据结构(9) 树(二叉树、线索二叉树)
本文根据<大话数据结构>一书,对Java版的二叉树.线索二叉树进行了一定程度的实现. 另: 二叉排序树(二叉搜索树) 平衡二叉树(AVL树) 二叉树的性质 性质1:二叉树第i层上的结点数目 ...
- python数据结构之树(二叉树的遍历)
树是数据结构中非常重要的一种,主要的用途是用来提高查找效率,对于要重复查找的情况效果更佳,如二叉排序树.FP-树. 本篇学习笔记来自:二叉树及其七种遍历方式.python遍历与非遍历方式实现二叉树 介 ...
- javascript实现数据结构: 树和二叉树的应用--最优二叉树(赫夫曼树),回溯法与树的遍历--求集合幂集及八皇后问题
赫夫曼树及其应用 赫夫曼(Huffman)树又称最优树,是一类带权路径长度最短的树,有着广泛的应用. 最优二叉树(Huffman树) 1 基本概念 ① 结点路径:从树中一个结点到另一个结点的之间的分支 ...
- javascript实现数据结构: 树和二叉树,二叉树的遍历和基本操作
树型结构是一类非常重要的非线性结构.直观地,树型结构是以分支关系定义的层次结构. 树在计算机领域中也有着广泛的应用,例如在编译程序中,用树来表示源程序的语法结构:在数据库系统中,可用树来组织信息:在分 ...
- 数据结构与算法系列研究五——树、二叉树、三叉树、平衡排序二叉树AVL
树.二叉树.三叉树.平衡排序二叉树AVL 一.树的定义 树是计算机算法最重要的非线性结构.树中每个数据元素至多有一个直接前驱,但可以有多个直接后继.树是一种以分支关系定义的层次结构. a.树是n ...
随机推荐
- base64 基本使用 和os模块使用
1 base64 的基本使用 import base64 with open('../static/upload/63bc620d1594779d6a98c53a3a8db1e5.png','rb' ...
- mysql多实例启动、关闭
启动(指定参数文件): [root@mysql01 ~]# mysqld_safe --defaults-file=/data/3306/my.cnf & [root@mysql01 ~]# ...
- Kubernetes【K8S】(一):Kubernetes组件
什么是Kubernetes Kubernetes 是一个可移植的.可扩展的开源平台,用于管理容器化的工作负载和服务,可促进声明式配置和自动化.Kubernetes拥有一个庞大且快速增长的生态系统. ...
- Android studio使用OKGO的POST请求访问http失败的解决方法
前几天在旧手机(版本是Android7)上调试一个app,用OKGO的post请求连接服务器登录一直很正常.今天旧手机不在身边,用自己的手机调试,就出现网络请求失败的问题,弹onError()里自己写 ...
- .NET生态系统掠影
如果你是一名开发人员,想要进入到.NET的世界,你需要知道都有哪些可能.由于.NET Framework是..NET生态系统中最流行的技术,你可以用它来构建各种各样的应用程序,但是最近,出现了一些新的 ...
- HTC Vive使用WebVR的方法以及启用后头显无画面的解决方法
1.下载支持WebVR的浏览器. 笔者使用的是HTC Vive,故下载了Firefox的Nightly版本[下载地址]. 2.Nightly 开启WebVR的步骤[引用自Mozilla VR] 1.从 ...
- CCNP之OSPF实验报告
OSPF实验报告 一.实验要求 1.R4为ISP,其上只能配置IP地址:R4与其它所有直连设备间使用公有IP2.R3--R5/6/7为MGRE环境,R3为中心站点3.整个OSPF环境IP地址为172. ...
- LINQ to Entities 不识别方法“System.String ToString(“yyyy-MM-dd”)”
将Queryable转化为IEnumerable或者直接Tolist()
- 【命令】ln命令
这是linux中一个非常重要命令,请大家一定要熟悉.它的功能是为某一个文件或目录在另外一个位置建立一个同步的链接,默认是链接是硬链接,常用参数是 "-s" . 对于ln命令,这里 ...
- 3.自定义view-TextView变色
1.效果 2.实现原理 自定义Textview,重写onDraw方法,将画布分成两部分,用不同颜色的画笔画 核心代码: @Override protected void onDraw(Canvas c ...