CSU1005-Binary Search Tree analog-模拟
2017-06-27 00:44
501 查看
A: 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 isT, 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
Nintegers 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
用输入序列直接建立一棵以第一个数为根节点的二叉搜索树,然后输出该树的三种DFS遍历序列直接放代码吧
#include <bits/stdc++.h> #define N 10100 #define INF 0x3f3f3f3f #define LL long long #define mem(a,n) memset(a,n,sizeof(a)) #define fread freopen("in.txt","r",stdin) #define fwrite freopen("out.txt","w",stdout) using namespace std; struct node{ int val; node *left,*right; node():val(0),left(nullptr),right(nullptr){} node(int n):val(n),left(nullptr),right(nullptr){} }; void insert(int n,node *root); void del(node *root); void PreTra(node *root); void InTra(node *root); void PosTra(node *root); int pre,in,pos; int main() { ios::sync_with_stdio(false); int n,t,temp; node *root; cin>>t; while(t--){ cin>>n; in=pos=pre=0; root=new node 9f85 ; cin>>root->val; for(int i=1;i<n;++i){ cin>>temp; insert(temp,root); } PreTra(root);cout<<endl; InTra(root);cout<<endl; PosTra(root);cout<<endl; del(root); cout<<endl; } return 0; } void insert(int n,node *root) { if(n<root->val){ if(root->left){ insert(n,root->left); }else{ root->left=new node; root->left->val=n; } }else{ if(root->right){ insert(n,root->right); }else{ root->right=new node; root->right->val=n; } } } void del(node *root) { if(root==nullptr){ return; } del(root->left); del(root->right); delete root; } void PreTra(node *root) { if(root==nullptr){ return; }if(pre==0){ cout<<root->val; ++pre; }else{ cout<<' '<<root->val; } PreTra(root->left); PreTra(root->right); } void InTra(node *root) { if(root==nullptr){ return; } InTra(root->left); if(in==0){ cout<<root->val; ++in; }else{ cout<<' '<<root->val; } InTra(root->right); } void PosTra(node *root) { if(root==nullptr){ return; } PosTra(root->left); PosTra(root->right); if(pos==0){ cout<<root->val; ++pos; }else{ cout<<' '<<root->val; } }
相关文章推荐
- CSUOJ 1005 Binary Search Tree analog(二叉搜索树模拟)
- csuoj1005: Binary Search Tree analog
- CSUOJ 1005 Binary Search Tree analog
- CSU-ACM 1005: Binary Search Tree analog
- CSU 1005: Binary Search Tree analog
- COJ-1005-Binary Search Tree analog
- Binary Search Tree analog
- COJ1005(Binary Search Tree analog)
- Binary Search Tree analog
- Binary Search Tree analog
- 1064. Complete Binary Search Tree (30)
- Convert Sorted List to Binary Search Tree解题报告
- UVA | Optimal Binary Search Tree
- Leetcode 109 Convert Sorted List to Binary Search Tree 有序单链表构造平衡二插查找树
- 【小熊刷题】Convert Sorted Array to Balanced Binary Search Tree
- HomeWork——Binary Search Tree
- 【PAT】1064. Complete Binary Search Tree
- UVaLive 4847 - Binary Search Tree (与BST有关的计数问题)
- 1043. Is It a Binary Search Tree (25)
- Leetcode:Convert Sorted List to Binary Search Tree