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 ...
随机推荐
- vue 表单基本 表单修饰符
表单的基础 利用v-model进行双向数据绑定: 1.在下拉列表中,将v-model写在select中 2.单选框和复选框需要每个按钮都需要写上v-model 3.v-model在输入框中获取得是输入 ...
- [日常摸鱼]poj1509Glass Beads-SAM
QAQ学了好几天了-(我太傻啦) #include<cstdio> #include<cstring> #define rep(i,n) for(register int i= ...
- Javascript之Firefox与IE
IE其实相对来讲并不是规范的遵循者,错怪firefox了. 2020注:IE看来要退出市场了,这些也逐渐成为历史了.:) 1firefox不支持iframe.document, 而IE支持,所以对fi ...
- sqlserver varchar和Nvarchar区别
sql server中的varchar和Nvarchar有什么区别? 答:varchar(n)长度为 n 个字节的可变长度且非 Unicode 的字符数据.n 必须是一个介于 1 和 8,000 ...
- ProGuard使用文档
介绍 是一个对于Java字节码的免费的压缩器,优化器,混淆器和审核器: l 它检测并删除未使用的类,字段,方法和属性. l 它优化字节码并删除未使用的指令. l 它重命名其余类.字段和方法使用短 ...
- 为什么MySQL不推荐使用uuid作为主键?
前言 在mysql中设计表的时候,mysql官方推荐不要使用uuid或者不连续不重复的雪花id(long形且唯一,单机递增),而是推荐连续自增的主键id,官方的推荐是auto_increment,那么 ...
- Tomcat启动web项目静态页面中文乱码问题解决
1 首先查看静态页面在编辑器中是否正常, 如果是eclipse ,需要设置一下项目编码格式为utf-8, 如果是idea , 一般会自动识别, 也可以自己手动检查一下, 检查html上面是否有 ...
- [leetcode]TwoSum系列问题
1.普通数组找两个数,哈希表建立数值和下标的映射,遍历时一边判断一边添加 /* 哇,LeetCode的第一题...啧啧 */ public int [] twoSum(int[] nums, int ...
- 【探索之路】机器人篇(3)-给mwRobot建立模型
在创建一个mwRobot_description程序包那一节中,我们添加了依赖roscpp rospy std_msgs 和 urdf , 现在我们再添加一个xacro依赖. 如何添加依赖? 打开程 ...
- 使用JWT+RSA完成SSO单点登录
无状态登录原理 1.1.什么是有状态? 有状态服务,即服务端需要记录每次会话的客户端信息,从而识别客户端身份,根据用户身份进行请求的处理,典型的设计如tomcat中的session. 例如登录:用户登 ...