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 ...
随机推荐
- 我用go-zero开发了第一个线上项目
作者:结冰 前言 说在最前面,我是一个外表谦让,内心狂热,外表斯文,内心贪玩的一个普通人.我的职业是程序员,是一个golang语言爱好者,一半是因为golang好用,一半是因为其他语言学不好.我是 ...
- day112:MoFang:种植园使用websocket代替http&服务端基于flask-socketio提供服务&服务端响应信息&种植园页面显示初始化
目录 1.种植园使用websocket代替http 2.服务端基于socket提供服务 3.服务端响应信息 4.种植园页面展示 1.种植园使用websocket代替http 我们需要完成的种植园,是一 ...
- [RoarCTF 2019]Easy Calc
[RoarCTF 2019]Easy Calc 题目 题目打开是这样的 查看源码 .ajax是指通过http请求加载远程数据. 可以发现有一个calc.php,输入的算式会被传入到这个php文件里,尝 ...
- angular8 大地老师学习笔记---第十课
import { Component,Input} from '@angular/core';@Component({ selector: 'app-lifecycle', templateUrl: ...
- vue element ui 上传 请求接口
在页面上 http-request: 覆盖默认的上传行为,可以自定义上传的实现 <el-upload class="avatar-uploader" action=&qu ...
- python线性回归
一.理论基础 1.回归公式 对于单元的线性回归,我们有:f(x) = kx + b 的方程(k代表权重,b代表截距). 对于多元线性回归,我们有: 或者为了简化,干脆将b视为k0·x0,,其中k0为1 ...
- .Net Core 审计日志实现
前言: 近日在项目协同开发过程中出现了问题,数据出现了异常:其他人员怀疑项目数据丢失程序存在问题.于是通过排查程序提供的审计日志最终还原了当时操作及原因. 可见审计日志在排查.定位问题是相当有用的,那 ...
- Flash Player的终章——赠予它的挽歌
本文由葡萄城技术团队原创并首发 转载请注明出处:葡萄城官网,葡萄城为开发者提供专业的开发工具.解决方案和服务,赋能开发者. 12月28日消息,微软已经确认Windows 10在下一次更新时将自动删除F ...
- DVWA Brute Force:暴力破解篇
DVWA Brute Force:暴力破解篇 前言 暴力破解是破解用户名密码的常用手段,主要是利用信息搜集得到有用信息来构造有针对性的弱口令字典,对网站进行爆破,以获取到用户的账号信息,有可能利用其权 ...
- .netcore 微服务快速开发框架 Anno&Viper -分布式锁是个什么鬼
1.什么是锁 锁是为了解决多线程或者多进程资源竞争的问题. 同一进程的多个线程资源竞争可以用lock解决. lock 关键字可确保当一个线程位于代码的临界区时,另一个线程不会进入该临界区. 如果其他线 ...