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 ...
随机推荐
- python中字符串的常见操作(一)
如有字符串: str1 = '192.168.1.1' str2 = 'asdfghjk' str3 = 'Asd fg hj ki' str4 = ' ' str5 = '' 以下是常见操作:# i ...
- springcloud微服务基于redis集群的单点登录
springcloud微服务基于redis集群的单点登录 yls 2019-9-23 简介 本文介绍微服务架构中如何实现单点登录功能 创建三个服务: 操作redis集群的服务,用于多个服务之间共享数据 ...
- T-SQL Part VII: CROSS JOIN
虽然不能确定是不是只有个SQL Server提供了Cross Join的功能,貌似W3School的SQL教程中是没有的 SQL教程.而Wikipedia中倒是有,也是最新的SQL:2011SQL:2 ...
- git push后出现错误 ![rejected] master -> master(non-fast-forward) error:failed to push some refs to 'XXX'
本地创建了一个project并在GitHub上创建了一个仓库,想要将本地的仓库链接到远程仓库我用的是如下方法:git init //初始化本地仓库git remote add origin XX ...
- diff算法
diff算法的作用计算出Virtual DOM中真正变化的部分,并只针对该部分进行原生DOM操作,而非重新渲染整个页面. 传统diff算法 通过循环递归对节点进行依次对比,算法复杂度达到 O(n^3) ...
- MySQL 5.7 安装教程(Win 10)
MySQL5.7 下载 官网下载(不推荐使用):https://dev.mysql.com/downloads/mysql/ 清华镜像站下载(推荐):https://mirrors.tuna.tsin ...
- js-程序结构
程序结构: 1.顺序结构(主体结构):自上而下,逐行实行: 2.分支(选择)结构:if语句,if…else, if…else if…else,switch; 3.循环结构:重复某些代码: 分支 ...
- 使用shiro做权限管理的学习笔记整理
Shiro权限管理 参考:https://www.cnblogs.com/jpfss/p/8352031.html Shiro解决的问题 授权和鉴别的问题:Authenrization(授权) Aut ...
- 玩转网络(一)用TTL(Time To Live)排查网络问题
先大概介绍一下TTL(Time To Live)吧! TTL翻译过来就是网络生存时间,说的是一个网络数据包,它在网络设备中转发的跳数(网络设备这里一般指的是路由器),默认值为64,也有很多设置为了12 ...
- Code Helper占用大量CPU和内存
项目架构: React+TS+DVA 设备及软件: 设备:Mac 软件:VSCode 场景: 在Mac中使用VSCode运行时发现项目编译非常卡顿,时间长达五六分钟以上,并且项目启动后访问页面,页面也 ...