二叉查找树的简单创建和3种遍历
2016-04-02 03:23
232 查看
创建一棵二叉查找树并分别用先序遍历中序遍历后序遍历输出
其实根据二叉搜索树的特点,中序遍历的结果就是从小到大的顺序输出,但为了练习树我还是用递归输出的
题目:
Description
Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following property:
1. each node has a Key value, which can be used to compare with each other.
2. For every node in the tree, every Key value in its left subtree is smaller than its own Key value.
3. 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
l To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:
l Visit the root.
l Traverse the left subtree.
l Traverse the right subtree.
l To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:
l Traverse the left subtree.
l Visit the root.
l Traverse the right subtree.
l To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:
l Traverse the left subtree.
l Traverse the right subtree.
l 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 figure:
-
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
Sample Output
Hint
代码如下:
其实根据二叉搜索树的特点,中序遍历的结果就是从小到大的顺序输出,但为了练习树我还是用递归输出的
题目:
Description
Binary Search Tree, abbreviated as BST, is a kind of binary tree maintains the following property:
1. each node has a Key value, which can be used to compare with each other.
2. For every node in the tree, every Key value in its left subtree is smaller than its own Key value.
3. 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
l To traverse a non-empty binary tree in preorder, perform the following operations recursively at each node, starting with the root node:
l Visit the root.
l Traverse the left subtree.
l Traverse the right subtree.
l To traverse a non-empty binary tree in inorder (symmetric), perform the following operations recursively at each node:
l Traverse the left subtree.
l Visit the root.
l Traverse the right subtree.
l To traverse a non-empty binary tree in postorder, perform the following operations recursively at each node:
l Traverse the left subtree.
l Traverse the right subtree.
l 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 figure:
-
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> #include<algorithm> using namespace std; /* 题意:给出 N 个数,按照顺序依次插入到二叉搜索树, 然后按照前序,中序,后序依次输出 If the inserted value is larger than or equal to the value of the root's value, insert it to the right subtree. ╮(╯▽╰)╭ 二叉查找树: 左子树<根<右子树 先序遍历:根-左-右 中序遍历:左-根-右 后序遍历:左-右-根 */ typedef long long LL; struct Node { int val; Node *l,*r; } ; int a[1005]; int n; void Insert(Node * &p,int x)//往树里面插入一个元素x { if (p == NULL)//空树 { p = new Node; p->val = x; p->l = p->r = NULL; return; } if (x < p->val)//如果插入的值小于根的值,插入到左子树 { Insert(p->l,x); } else//否则插入右子树 PS: =-= 这个题说了等于根的时候插入右子树 { Insert(p->r,x); } } void buildBST(Node * &T) { T = NULL; for (int i = 0;i < n;++i) Insert(T,a[i]); } bool first; void PreOder(Node * T) { if (T) { if (first) { cout<<T->val; first = 0; } else { cout<<" "<<T->val; } PreOder(T->l); PreOder(T->r); } } void InOder(Node * T)//中序遍历二叉查找树的话就是顺序输出,这样的话...... { if (T) { InOder(T->l); if (first) { cout<<T->val; first = 0; } else cout<<" "<<T->val; InOder(T->r); } } void AfterOder(Node * T) { if (T) { AfterOder(T->l); AfterOder(T->r); if (first) { cout<<T->val; first = 0; } else cout<<" "<<T->val; } } int main() { int T_T; cin>>T_T; while (T_T--) { cin>>n; for (int i = 0;i < n;++i) scanf("%d",a+i); Node * T; buildBST(T); first = 1; PreOder(T); cout<<endl; first = 1; InOder(T); cout<<endl; first = 1; AfterOder(T); cout<<endl; cout<<endl; } return 0; }
相关文章推荐
- Quick-Cocos2dx 快速了解
- ListView 设置点击项目背景色
- 如何高效的开发Android程序
- 许单单肩负的担当:做错了的认,考虑不周也得认
- linux 安装Python 包
- MySQL-QueryCache查询缓存
- nginx+tocmat 负载均衡时,一台tomcat宕机时的问题
- Linux用户和用户组管理
- centos编译安装php5.6.20+nginx1.8.1+mysql5.6.17
- python开发者打包发布工具Distutils | setuptools
- 17. Letter Combinations of a Phone Number
- mysql主主互备+原来mysql主从架构
- Android Binder机制,跨进程机制深入理解精华总结
- SGA内存的优化
- Nginx 下 安装nginx_upstream_jvm_route模块
- JQuery $("#id").val(value); 给select选值不出现问题
- 如何实现函数来处理可变参数
- 扩展Python__在python脚本中使用其他语言(c/c++/java/c#)编写的模块
- C#的UDP服务器
- 从root后的Android手机中取出数据库数据