Binary Search Tree analog
Description
Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following property:
- each node has a Key value, which can be used to compare with each other.
- For every node in the tree, every Key value in its left subtree is smaller than its own Key value.
- For every node in the tree, every Key value in its right subtree is equal to or larger than its own Key value.
Now we need to analog a BST, we only require one kind of operation: inserting.
First, we have an empty BST. Input is a sequence of numbers. We need to insert them one by one flowing the rules below:
If the inserted value is smaller than the root's value, insert it to the left subtree.
If the inserted value is larger than or equal to the value of the root's value, insert it to the right subtree.
After each input, we need to output the preorder, inorder, postorder traversal sequences.
About tree traversal, the following is from Wikipedia:
Depth-first Traversal
To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:
- Visit the root.
- Traverse the left subtree.
- Traverse the right subtree.
To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:
- Traverse the left subtree.
- Visit the root.
- Traverse the right subtree.
To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:
- Traverse the left subtree.
- Traverse the right subtree.
- Visit the root.
Look at the folowing example:
Intput is a sequence of 5 integers: 3 6 9 5 1
After each integer inserted the structure of the tree is illustrated in the flowing:
3
/ \
1 6
/ \
5 9
Input
The first integer of the input is T, the number of test cases. Each test case has two lines. The first line contain an integer N,(1<=N<=1000), the number of numbers need to be inserted into the BST. The second line contain N integers separated by space, each integer is in the range of [0,230].
Output
Each test case, output must contain three lines: the preorder, inorder and postorder traversal sequence. The numbers in each line should be separated by a single space and you should not output anything at the end of the line! Output a blank line after each case.
Sample Input
1
5
3 6 9 5 1
Sample Output
3 1 6 5 9
1 3 5 6 9
1 5 9 6 3
Hint
#include<iostream>
#include<cstdio>
using namespace std;
struct Node{
int key;
int left;
int right;
}node[1010];
int n;
int cnt;
void insert(int root,int i)
{
if(node[i].key < node[root].key)
{
if(node[root].left == -1) node[root].left = i;
else insert(node[root].left,i);
}
else
{
if(node[root].right == -1) node[root].right = i;
else insert(node[root].right,i);
}
}
void traverse1(int root)
{
if(root != -1)
{
cnt++;
if(cnt < n) cout << node[root].key << " ";
else cout << node[root].key << endl;
traverse1(node[root].left);
traverse1(node[root].right);
}
}
void traverse2(int root)
{
if(root!=-1)
{
traverse2(node[root].left);
cnt++;
if(cnt < n) cout << node[root].key << " ";
else cout << node[root].key << endl;
traverse2(node[root].right);
}
}
void traverse3(int root)
{
if(root!=-1)
{
traverse3(node[root].left);
traverse3(node[root].right);
cnt++;
if(cnt < n) cout << node[root].key << " ";
else cout << node[root].key << endl;
}
}
int main()
{
int t;
cin >> t;
while(t--)
{
cin >> n;
for(int i = 0;i < n;i++)
{
node[i].left = node[i].right = -1;
}
cin >> node[0].key;
for(int i = 1;i < n;i++)
{
cin >> node[i].key;
insert(0,i);
}
cnt = 0;
traverse1(0);
cnt = 0;
traverse2(0);
cnt = 0;
traverse3(0);
cout << endl;
}
return 0;
}
/**********************************************************************
Problem: 1005
User: song_hai_lei
Language: C++
Result: AC
Time:44 ms
Memory:2036 kb
**********************************************************************/
Binary Search Tree analog的更多相关文章
- CSUOJ 1005 Binary Search Tree analog
Description Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following ...
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode: Convert sorted list to binary search tree (No. 109)
Sept. 22, 2015 学一道算法题, 经常回顾一下. 第二次重温, 决定增加一些图片, 帮助自己记忆. 在网上找他人的资料, 不如自己动手. 把从底向上树的算法搞通俗一些. 先做一个例子: 9 ...
- [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二
Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...
- [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值
Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...
- [LeetCode] Verify Preorder Sequence in Binary Search Tree 验证二叉搜索树的先序序列
Given an array of numbers, verify whether it is the correct preorder traversal sequence of a binary ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- 如何对 React 函数式组件进行优化
文章首发个人博客 前言 目的 本文只介绍函数式组件特有的性能优化方式,类组件和函数式组件都有的不介绍,比如 key 的使用.另外本文不详细的介绍 API 的使用,后面也许会写,其实想用好 hooks ...
- suseoj 1209: 独立任务最优调度问题(动态规划)
1209: 独立任务最优调度问题 时间限制: 1 Sec 内存限制: 128 MB提交: 3 解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 用2台处理机A和B处理 ...
- ReadWriteLock: 读写锁
ReadWriteLock: 读写锁 ReadWriteLock: JDK1.5提供的读写分离锁,采用读写锁分离可以有效帮助减少锁竞争. 特点: 1).使用读写锁.当线程只进行读操作时,可以允许多个线 ...
- BloomFilter在Hudi中的应用
Bloom Filter在Hudi中的应用 介绍 Bloom Filter可以用于检索一个元素是否在一个集合中.它的优点是空间效率和查询时间都远远超过一般的算法,主要缺点是存在一定的误判率:当其判断元 ...
- python:利用celery分布任务
Celery是一个功能完备即插即用的任务队列.它使得我们不需要考虑复杂的问题,使用非常简单.celery看起来似乎很庞大.celery适用异步处理问题,当发送邮件.或者文件上传, 图像处理等等一些比较 ...
- ubuntu 16.04上源码编译glog和gflags 编写glog-config.cmake和gflags-config.cmake | compile glog and glags on ubuntu 16.04
本文首发于个人博客https://kezunlin.me/post/977f5125/,欢迎阅读! compile glog and glags on ubuntu 16.04 Series comp ...
- 新闻网页通用抽取器GNEv0.04版更新,支持提取正文图片与源代码
GeneralNewsExtractor以下简称GNE是一个新闻网页通用抽取器,能够在不指定任何抽取规则的情况下,把新闻网站的正文提取出来. 我们来看一下它的基本使用方法. 安装 GNE 使用 pip ...
- JavaScript笔记九
1.数组方法 reverse() - 可以用来反转一个数组,它会对原数组产生影响 concat() - 可以连接两个或多个数组,它不会影响原数组,而是新数组作为返回值返回 join() - 可以将一个 ...
- java中的运算,+-* /% | ^ &
java中运算都是操作符号,那么整形默认为int,双精度默认为都double 整数 看案例: 无法编译通过:操作默认为int,接受结果为int,所以这个地方编译无法通过,所以需要强制类型转换 再看案例 ...
- Linux I/O复用 —— epoll 部分源码剖析
epoll 相关的系统调用有以下三个,这里简述下当调用对应函数后,内核的具体实现 epoll_creat( ) 在内核注册文件系统 eventpollfs,挂载此文件系统 (linux一切皆文件,便于 ...