博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Binary Search Tree analog
阅读量:5133 次
发布时间:2019-06-13

本文共 4177 字,大约阅读时间需要 13 分钟。

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

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

153 6 9 5 1

Sample Output

3 1 6 5 91 3 5 6 91 5 9 6 3

Hint

#include
#include
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**********************************************************************/

转载于:https://www.cnblogs.com/songorz/p/9386563.html

你可能感兴趣的文章
Linux平台下卸载MySQL的方法
查看>>
ubuntu下安装Docker
查看>>
CF1152 F. Neko Rules the Catniverse (dp)
查看>>
全国行政区域数据库
查看>>
8_19 比赛总结 [暑假集训]
查看>>
GET与POST的区别
查看>>
花容月貌
查看>>
Spring Roo 之 Mysql 连接
查看>>
Spring_Mybatis整合实现CRUD操作
查看>>
Android学习第六天---seekbar
查看>>
C++ lambda
查看>>
div中显示页面
查看>>
C向C++改造
查看>>
[洛谷P5367]【模板】康托展开
查看>>
Redis部分数据结构方法小结
查看>>
[NOIP2017 TG D2T2]宝藏(模拟退火)
查看>>
迷宫 广搜
查看>>
merge 与include
查看>>
Javascript 学习笔记 - 函数 - 关于IIFE - 关于函数声明和函数表达式 - 个人总结
查看>>
160508Junit使用
查看>>