【九度OJ】题目1201-二叉排序树
建树过程是递归,"递归的思路不是很复杂",经过题目1078的训练,直接开始编码。提交及修改的过程告诉自己,这是一个错觉,对递归的理解还应该再进一步。
自己的实现
#include <stdio.h>
typedef struct BTNode {
BTNode * lchild;
BTNode * rchild;
int data;
}BTNode;
BTNode tree[110];
int loc; //指向tree[]中第一个空节点,建树过程中会移动
//申请一个新节点
BTNode * creat(int x) {
tree[loc].data = x;
tree[loc].lchild = tree[loc].rchild = NULL;
loc++;
return &tree[loc - 1];
}
//构造BST
void build(int x, BTNode *root) {
if (loc == 0) { //如果树空,直接插入
tree[loc].lchild = tree[loc].lchild = NULL;
tree[loc].data = x;
loc++;
}
else if (x <= root->data) { //如果待插入x小于根,插入左子树
if (root->lchild != NULL) {
build(x, root->lchild);
}
else {
root->lchild = creat(x);
}
}
else { //如果待插入x大于根,插入右子树
if (root->rchild != NULL) {
build(x, root->rchild);
}
else {
root->rchild = creat(x);
}
}
}
void preOrder(BTNode * root) {
printf("%d ", root->data);
if (root->lchild != NULL) {
preOrder(root->lchild);
}
if (root->rchild != NULL) {
preOrder(root->rchild);
}
}
void inOrder(BTNode * root) {
if (root->lchild != NULL) {
inOrder(root->lchild);
}
printf("%d ", root->data);
if (root->rchild != NULL) {
inOrder(root->rchild);
}
}
void postOrder(BTNode * root) {
if (root->lchild != NULL) {
postOrder(root->lchild);
}
if (root->rchild != NULL) {
postOrder(root->rchild);
}
printf("%d ", root->data);
}
int main()
{
int n, next;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) {
scanf("%d", &next);
build(next, &tree[0]); //每插入一个,都从根开始找合适的位置
}
preOrder(&tree[0]);
printf("\n");
inOrder(&tree[0]);
printf("\n");
postOrder(&tree[0]);
printf("\n");
loc = 0; //将树清空
}
return 0;
}
直接实现的这个版本超时,慢的原因是什么???不太确定,可能的原因是:
- 当条件判断if嵌套很深时,要简化逻辑,尽量避免太深的if嵌套。无法避免的时候,这个if分支内部的操作也不能太复杂???
- 由对象组成的数组,首先访问数组,再访问对象的成员,再计算并赋值。这个操作只有一行,但它的复杂度应该比O(1)要大???
一个递归的程序超时,必然是递归本身的逻辑写的太复杂了,递归过程没有构思好。分析下面的代码哪里需要改进
//构造BST
void build(int x, BTNode *root) {
/*如果树空,直接插入。这个判断条件只在插入根节点(第一个节点)时用到。
*不是一个好的递归基,导致下面的分支变复杂,每个分支中多出一层判断
*除了全树的根节点,每插入一个节点,都要做此不必要的判断。
*/
if (loc == 0) {
tree[loc].lchild = tree[loc].lchild = NULL;
tree[loc].data = x;
loc++;
}
else if (x <= root->data) { //如果待插入x小于根,插入左子树
if (root->lchild != NULL) { //这层的if判断可以省略
build(x, root->lchild);
}
else {
/*由于递归基没有构思好,多出这么一个分支,这部分工作和在一棵空树种插入新节点(就是递归基)是一样的。
*因为build这个程序的本身,是往树中插入节点,在一棵很高的树中插入节点,是一次build,在这次build的过程中会进入到子树中,
*对子树而言,这也要启用一个新的build过程;
*直到子树成为NULL,这就到达了递归基:往空树中插入节点
*再回头看上面的if(loc==0)的情况,无法往空“子树”中插入节点
*这个else和下面的else,实际上将一个递归基复制,放入两个分支中
*/
root->lchild = creat(x);
}
}
else { //如果待插入x大于根,插入右子树
if (root->rchild != NULL) {
build(x, root->rchild);
}
else {
root->rchild = creat(x);
}
}
}
修改上述代码
- 通过了简单的测试,只是满足了基本的“做到”的要求,这样的代码中一定还有优化的空间。
- 修正一个思路,该思路事关对“递归层次/递归调用栈”的理解
- 不应判断
A->lchild == NULL
,这个想法的着眼点在A->lchild
上,错。 - 应该是每调用一次build,都相当于往一棵新树中插入节点,对于二叉排序树,新节点总是在底层的NULL位置处插入,即总是往一个空树中插入。
- 当前传入的树root是否为空,是这次递归调用
build(x, root)
应该考虑的着眼点,即root == NULL
是否成立。着眼点在本次递归中以root为根的树。
- 不应判断
- 修改递归基为
if(root == NULL)
- 修改了递归函数
build
的返回值,不再是void
,而是返回一个BTNode *
,引起了错误。一个函数的返回值改变,需要改变:- 所有该函数出现的位置处的代码
- 为了使用该函数,提前申请的一些变量。(该函数的上下文)
完整代码
#include <stdio.h>
typedef struct BTNode {
BTNode * lchild;
BTNode * rchild;
int data;
}BTNode;
BTNode tree[110];
int loc; //指向tree[]中第一个空节点,建树过程中会移动
//申请一个新节点
BTNode * creat(int x) {
tree[loc].data = x;
tree[loc].lchild = tree[loc].rchild = NULL;
loc++;
return &tree[loc - 1];
}
//构造BST
BTNode * build(int x, BTNode *root) {
/*
if (loc == 0) { //如果树空,直接插入
tree[loc].lchild = tree[loc].lchild = NULL;
tree[loc].data = x;
loc++;
}
*/
if (root == NULL) {
root = creat(x);
return root;
}
else if (x < root->data) { //如果待插入x小于根,插入左子树dd
root->lchild = build(x, root->lchild);
}
else if(x > root->data) { //如果待插入x大于根,插入右子树
root->rchild = build(x, root->rchild);
}
return root;
}
void preOrder(BTNode * root) {
printf("%d ", root->data);
if (root->lchild != NULL) {
preOrder(root->lchild);
}
if (root->rchild != NULL) {
preOrder(root->rchild);
}
}
void inOrder(BTNode * root) {
if (root->lchild != NULL) {
inOrder(root->lchild);
}
printf("%d ", root->data);
if (root->rchild != NULL) {
inOrder(root->rchild);
}
}
void postOrder(BTNode * root) {
if (root->lchild != NULL) {
postOrder(root->lchild);
}
if (root->rchild != NULL) {
postOrder(root->rchild);
}
printf("%d ", root->data);
}
int main()
{
int n, next;
while (scanf("%d", &n) != EOF) {
loc = 0;
BTNode * root = NULL;
for (int i = 0; i < n; i++) {
scanf("%d", &next);
root = build(next, root); //每插入一个,都从根开始找合适的位置
}
preOrder(&tree[0]);
printf("\n");
inOrder(&tree[0]);
printf("\n");
postOrder(&tree[0]);
printf("\n");
}
return 0;
}
【九度OJ】题目1201-二叉排序树的更多相关文章
- 九度oj 题目1201:二叉排序树
题目描述: 输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历. 输入: 输入第一行包括一个整数n(1<=n<=100). 接下来的一行包括n个整数. 输出: 可能有多组测试 ...
- 九度OJ 题目1384:二维数组中的查找
/********************************* * 日期:2013-10-11 * 作者:SJF0115 * 题号: 九度OJ 题目1384:二维数组中的查找 * 来源:http ...
- hdu 1284 关于钱币兑换的一系列问题 九度oj 题目1408:吃豆机器人
钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- 九度oj题目&吉大考研11年机试题全解
九度oj题目(吉大考研11年机试题全解) 吉大考研机试2011年题目: 题目一(jobdu1105:字符串的反码). http://ac.jobdu.com/problem.php?pid=11 ...
- 九度oj 题目1007:奥运排序问题
九度oj 题目1007:奥运排序问题 恢复 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号 ...
- 九度oj 题目1087:约数的个数
题目链接:http://ac.jobdu.com/problem.php?pid=1087 题目描述: 输入n个整数,依次输出每个数的约数的个数 输入: 输入的第一行为N,即数组的个数(N<=1 ...
- 九度OJ题目1105:字符串的反码
tips:scanf,cin输入字符串遇到空格就停止,所以想输入一行字符并保留最后的"\0"还是用gets()函数比较好,九度OJ真操蛋,true?没有这个关键字,还是用1吧,还是 ...
- 九度oj题目1009:二叉搜索树
题目描述: 判断两序列是否为同一二叉搜索树序列 输入: 开始一个数n,(1<=n<=20) 表示有n个需要判断,n= 0 的时候输入结束. 接 ...
- 九度oj题目1002:Grading
//不是说C语言就是C++的子集么,为毛printf在九度OJ上不能通过编译,abs还不支持参数为整型的abs()重载 //C++比较正确的做法是#include<cmath.h>,cou ...
- 九度OJ题目1003:A+B
while(cin>>str1>>str2)就行了,多简单,不得不吐槽,九度的OJ真奇葩 题目描述: 给定两个整数A和B,其表示形式是:从个位开始,每三位数用逗号", ...
随机推荐
- BZOJ 3203 sdoi 2013 保护出题人
由于样例解释很清晰,所以很容易得到以下结论: 1.每一关都是独立的,且僵尸的相对位置不会变 2.每一关的攻击力=Max(sum(i)/dis(i)) 其实sum(i)是僵尸攻击力的前缀和,dis(i) ...
- C#操作.csv文件Demo
1.使用OleDB操作.csv文件,比较费时 public static DataTable GetDataTableFromCsv(string path,bool isFirstRowHeader ...
- 在PowerDesigner中设计物理模型3——视图、存储过程和函数
原文:在PowerDesigner中设计物理模型3--视图.存储过程和函数 视图 在SQL Server中视图定义了一个SQL查询,一个查询中可以查询一个表也可以查询多个表,在PD中定义视图与在SQL ...
- Tomcat启动后访问首页报错 显示JSP 空指针异常
HTTP Status 500 - type Exception report message description The server encountered an internal error ...
- 1401 - Remember the Word
注意到单词的长度最长100,其实最糟糕复杂度应该能到O(300005*100),需要注意的是在字典树上匹配单词时,一旦不匹配,则后面的就不会匹配,需要break出来(这个害我TLE查了半天,日!),还 ...
- Sqlmap基础(一)
(1)选项:-r REQUESTFILE Load HTTP request from a file (2)选项:--current-db Retrieve DBMS curr ...
- gdb 基本命令
backtrace(或bt) 查看各级函数调用及参数 finish 连续运行到当前函数返回为止,然后停下来等待命令 frame(或f) 帧编号 选择栈帧 info(或i) locals 查看当前栈帧局 ...
- BZOJ 3143 游走(高斯消元)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3143 题意:一个无向连通图,顶点从1编号到n,边从1编号到m.小Z在该图上进行随机游走, ...
- /usr/lib/libstdc++.so.6: version `GLIBCXX_3.4.15' not found错误的解决
升级cmake时,提示"Error when bootstrapping CMake:Problem while running initial CMake",第二次运行./boo ...
- [UVA796]Critical Links(割边, 桥)
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem ...