Binary Search Tree(二叉搜索树)
2017-05-04 11:42
246 查看
Problem description
A binary search tree is a binary tree that satisfies the following properties:
• The left subtree of a node contains only nodes with keys less than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• Both the left and right subtrees must also be binary search trees.
Pre-order traversal (Root-Left-Right) prints out the nodes key by visiting the root node then traversing the left subtree and then traversing the right subtree. Post-order traversal (Left Right-Root) prints out the left subtree first and then right subtree and finally the root node. For example, the results of pre-order traversal and post-order traversal of the binary tree shown in Figure 1 are as follows: Pre-order: 50 30 24 5 28 45 98 52 60 Post-order: 5 28 24 45 30 60 52 98 50 Given the pre-order traversal of a binary search tree, you task is to find the post-order traversal of this tree.
Input
The keys of all nodes of the input binary search tree are given according to pre-order traversal. Each node has a key value which is a positive integer less than 106. All values are given in separate lines (one integer per line). You can assume that a binary search tree does not contain more than 10,000 nodes and there are no duplicate nodes.
Output
The output contains the result of post-order traversal of the input binary tree. Print out one key per line.
Sample Input
50
30
24
5
28
45
98
52
60
Sample Output
5
28
24
45
30
60
52
98
50
题意
给一个序列,让它以二叉树排序树的形式插入,即:遇到值比他小的往左子树插入,否则往右子树插入,输出他的后续遍历序列。
思路
建议大家能用数组的时候尽量开固定大小的数组,因为用指针去写的话申请内存耗时间,而且有的题目还可能内存超限,情况比较少就是了。这里先给出指针形式再给出数组形式代码。
图一是数组方法,图二是指针方法,比较两者时间,可想而知。
方法一
方法二
练习网址:https://cn.vjudge.net/problem/UVA-12347
A binary search tree is a binary tree that satisfies the following properties:
• The left subtree of a node contains only nodes with keys less than the node’s key.
• The right subtree of a node contains only nodes with keys greater than the node’s key.
• Both the left and right subtrees must also be binary search trees.
Pre-order traversal (Root-Left-Right) prints out the nodes key by visiting the root node then traversing the left subtree and then traversing the right subtree. Post-order traversal (Left Right-Root) prints out the left subtree first and then right subtree and finally the root node. For example, the results of pre-order traversal and post-order traversal of the binary tree shown in Figure 1 are as follows: Pre-order: 50 30 24 5 28 45 98 52 60 Post-order: 5 28 24 45 30 60 52 98 50 Given the pre-order traversal of a binary search tree, you task is to find the post-order traversal of this tree.
Input
The keys of all nodes of the input binary search tree are given according to pre-order traversal. Each node has a key value which is a positive integer less than 106. All values are given in separate lines (one integer per line). You can assume that a binary search tree does not contain more than 10,000 nodes and there are no duplicate nodes.
Output
The output contains the result of post-order traversal of the input binary tree. Print out one key per line.
Sample Input
50
30
24
5
28
45
98
52
60
Sample Output
5
28
24
45
30
60
52
98
50
题意
给一个序列,让它以二叉树排序树的形式插入,即:遇到值比他小的往左子树插入,否则往右子树插入,输出他的后续遍历序列。
思路
建议大家能用数组的时候尽量开固定大小的数组,因为用指针去写的话申请内存耗时间,而且有的题目还可能内存超限,情况比较少就是了。这里先给出指针形式再给出数组形式代码。
图一是数组方法,图二是指针方法,比较两者时间,可想而知。
方法一
#include<iostream> #include<stdio.h> using namespace std; struct tree { int data; tree *l=NULL,*r=NULL; }; void _insert(tree *&T,int e) { if(!T) { tree *S=new tree; S->data=e; T=S; }else if(e<T->data){ _insert(T->l,e); }else{ _insert(T->r,e); } } void _print(tree *&T) { if(T){ _print(T->l); _print(T->r); printf("%d\n",T->data); } } int main() { int e; tree *T=NULL; while(~scanf("%d",&e)){ _insert(T,e); } _print(T); return 0; }
方法二
#include<cstdio> #include<cstring> #include<algorithm> const int maxn = 1e6 + 10; using namespace std; int son[maxn][2]; int n, root = 0; void __insert(int val, int &x) { if(x==0){ x=val; return ; } if(val < x) __insert(val, son[x][0]); else __insert(val, son[x][1]); } void dfs(int o) { if(o==0) return ; dfs(son[o][0]); dfs(son[o][1]); printf("%d\n", o); } int main() { while(scanf("%d", &n) != EOF) __insert(n, root); dfs(root); return 0; }
练习网址:https://cn.vjudge.net/problem/UVA-12347
相关文章推荐
- 二叉搜索树(Binary Search Tree )的定义及分析
- LeetCode | Convert Sorted List to Binary Search Tree(链表转换成二叉搜索树)
- Binary Search Tree(二叉搜索树、二叉查找树、二叉排序树)
- LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)
- [leetcode]Validate Binary Search Tree (判断有效二叉搜索树 C语言实现)
- 第九章 ALDS1_8_B:Binary Search Tree II 二叉搜索树--搜索
- 二叉搜索树 ALDS1_8_C: Binary Search Tree III
- LeetCode OJ 之 Recover Binary Search Tree(恢复二叉搜索树)
- lintcode search-range-in-binary-search-tree 二叉搜索树中搜索区间
- LeetCode | Recover Binary Search Tree(恢复二叉搜索树)
- [CareerCup] 4.3 Create Minimal Binary Search Tree 创建最小二叉搜索树
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)
- 是否是二叉搜索树 check if it is binary search tree
- LeetCode--Validate Binary Search Tree(二叉搜索树)
- BinarySearchTree二叉搜索树的实现
- 把排序数组转换为高度最小的二叉搜索树convert-sorted-array-to-binary-search-tree-with-minimal-height
- [LeetCode] Recover Binary Search Tree 复原二叉搜索树
- [Leetcode] Convert sorted list to binary search tree 将排好的链表转成二叉搜索树
- 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree
- Convert Sorted Array to Binary Search Tree 将排序的数组转换为二叉搜索树