OpenJudge4079:二叉搜索树
2015-10-30 17:29
507 查看
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
提交
统计
提示
提问
总时间限制: 1000ms 内存限制: 1024kB
描述
二叉搜索树在动态查表中有特别的用处,一个无序序列可以通过构造一棵二叉搜索树变成一个有序序列,构造树的过程即为对无序序列进行排序的过程。每次插入的新的结点都是二叉搜索树上新的叶子结点,在进行插入操作时,不必移动其它结点,只需改动某个结点的指针,由空变为非空即可。
这里,我们想探究二叉树的建立和序列输出。
输入只有一行,包含若干个数字,中间用空格隔开。(数字可能会有重复)
输出输出一行,对输入数字建立二叉搜索树后进行前序周游的结果。
样例输入
样例输出
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
typedef struct BiNode{ struct BiNode *left; struct BiNode * right; int num; }BiNode,*BiTree; BiTree tt; int b[500];
//建树 void createTree(BiTree* root,int key) { //cout<<key; BiTree t; t = (BiTree)malloc(sizeof(BiNode)); t->left = NULL; t->right = NULL; t->num = key; if(*root == NULL) { *root = t; return; } if((*root)->left==NULL&&(*root)->num>key) { (*root)->left = t; return; } if((*root)->right==NULL&&(*root)->num<key) { (*root)->right = t; return; } if((*root)->num>key) { createTree(&(*root)->left,key); return; } else if((*root)->num<key) { createTree(&(*root)->right,key); return; } else return; }
//前序遍历 void display(BiTree t) { if(t!=NULL){ cout<<t->num<<" "; display(t->left); display(t->right); } } int main() { int i = 0; tt=NULL; int a; while(cin>>a) { b[i] = a; createTree(&tt,b[i]); i++; //if(i==20) break; } //cout<<i; display(tt); //system("pause"); return 0; }
4079:二叉搜索树
查看提交
统计
提示
提问
总时间限制: 1000ms 内存限制: 1024kB
描述
二叉搜索树在动态查表中有特别的用处,一个无序序列可以通过构造一棵二叉搜索树变成一个有序序列,构造树的过程即为对无序序列进行排序的过程。每次插入的新的结点都是二叉搜索树上新的叶子结点,在进行插入操作时,不必移动其它结点,只需改动某个结点的指针,由空变为非空即可。
这里,我们想探究二叉树的建立和序列输出。
输入只有一行,包含若干个数字,中间用空格隔开。(数字可能会有重复)
输出输出一行,对输入数字建立二叉搜索树后进行前序周游的结果。
样例输入
41 467 334 500 169 724 478 358 962 464 705 145 281 827 961 491 995 942 827 436
样例输出
41 467 334 169 145 281 358 464 436 500 478 491 724 705 962 827 961 942 995
相关文章推荐
- 基于tesseract_ocr实现图片中汉字辨识
- 实现windows8与virtual box linux虚拟机之间的文件共享
- ubuntu下安装opensips 最终总给版
- 『笔记』windows与CentOS间文件传输(win下实行)
- layout_marginTop="-3dp"导致内容被遮挡的问题处理
- cocopods 一直报错找不到头文件的问题解决办法
- Linux下使用dosemu编写汇编程序
- 架构设计:系统间通信(10)——RPC的基本概念
- python 常用的模块 optparse与ConfigParser
- centos google-flags/log 编译安装所产生的问题
- 安卓开发必须收藏的网站
- CentOS 6.4下编译安装MySQL 5.6.14
- FATAL,XX000,"highest timeline 14 of the primary is behind recovery timeline 15" rsync 增量重置备库
- Linux目录结构和常用命令
- Linux命令-重定向和软硬链接
- SELinux架构和Flask安全体系结构的一些认识
- linux上传测试脚本
- linux led子系统(二)
- linux命令总结1129
- git Bash常用命令