当前位置:网站首页>Binary search tree to solve the fallen leaves problem
Binary search tree to solve the fallen leaves problem
2022-08-03 22:46:00 【Chengqiuming】
A link to the original question
Falling Leaves - POJ 1577 - Virtual Judgehttps://vjudge.net/problem/POJ-1577
Two Inputs and Outputs
1 input
The input contains multiple test cases.Each test case is a sequence of one or more uppercase letters, each giving the leaves removed from the binary search tree as described above, each giving letters in ascending alphabetical order.Separate the test cases with a line that contains only an asterisk, and give a line after the last test case that contains only a dollar sign.There are no spaces or blank lines in the input.
2 output
For each test case, there is a unique binary search tree, and a single line outputs the preorder traversal of the tree.
Three input and output samples
1 Input example
BDHPY
CM
GQ
K
*
AC
B
$
2 Sample output
KGCBDHQMPY
BAC
Four Algorithms Design
The last letter must be the root of the tree. The letter entered first is in the deep layer of the tree, and the tree can be built in reverse order.After reading the sequence of letters, store them as strings, and then create a binary search tree in reverse order, inserting small letters into the left subtree and inserting larger letters into the right subtree.Output the preorder traversal of the tree: root, left subtree, right subtree.
Five codes
package poj1577;import java.util.Scanner;public class Poj1577 {static int cnt = 1;static data tree[] = new data[110];static void insert(int t, char ch) {if (tree[t].c == 0) {tree[t].c = ch;return;}if (ch < tree[t].c) {if (tree[t].l == 0) {tree[++cnt].c = ch;tree[t].l = cnt;} elseinsert(tree[t].l, ch);}if (ch > tree[t].c) {if (tree[t].r == 0) {tree[++cnt].c = ch;tree[t].r = cnt;} elseinsert(tree[t].r, ch);}}static void preorder(int t) {if (tree[t].c == 0)return;System.out.print(tree[t].c);preorder(tree[t].l);preorder(tree[t].r);}public static void main(String[] args) {String s1, s;while (true) {s = "";for (int i = 0; i < tree.length; i++) {tree[i] = new data();}Scanner scanner = new Scanner(System.in);while (true) {s1 = scanner.next();if (s1.charAt(0) != '*' && s1.charAt(0) != '$') {s += s1;} else {break;}}for (int i = s.length() - 1; i >= 0; i--)insert(1, s.charAt(i));preorder(1);System.out.println();if (s1.charAt(0) == '$')break;}}}class data {int l;int r;char c;}Six Tests
Green is input and white is output.

边栏推荐
- UVa 1025 - A Spy in the Metro (White Book)
- The development status of cloud computing at home and abroad
- 嵌入式系统:GPIO
- Embedded systems: overview
- encapsulation, package, access modifier, static variable
- Flutter 桌面探索 | 自定义可拖拽导航栏
- LabVIEW code generation error 61056
- JPA Native Query(本地查询)及查询结果转换
- 静态文件快速建站
- 中国企业构建边缘计算解决方案的最佳实践
猜你喜欢
![navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication](/img/09/a579c60e07cdc145175e72673409f7.png)
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication

嵌入式系统:时钟

for循环练习题

October 2019 Twice SQL Injection

如何设计 DAO 的 PoW 评判标准 并平衡不可能三角

Bytebase数据库 Schema 变更管理工具

113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节

noip初赛

override learning (parent and child)

113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
随机推荐
[b01lers2020]Life on Mars
如何创建一个Web项目
Golang Chapter 1: Getting Started
October 2019 Twice SQL Injection
for循环练习题
老板:公司系统太多,能不能实现账号互通?
软测人每个阶段的薪资待遇,快来康康你能拿多少?
Causes of Mysql Disk Holes and Several Ways to Rebuild Tables
Internet user account information management regulations come into effect today: must crack down on account trading and gray products
【day1】
举一个 web worker 的例子
websocket多线程发送消息报错TEXT_PARTIAL_WRITING--自旋锁替换synchronized独占锁的使用案例
LabVIEW code generation error 61056
Kotlin - 扩展函数和运算符重载
node连接mysql数据库报错:Client does not support authentication protocol requested by server
【MySQL进阶】数据库与表的创建和管理
log4j-slf4j-impl cannot be present with log4j-to-slf4j
获国际权威认可 | 云扩科技入选《RPA全球市场格局报告,Q3 2022》
Makefile
UVa 437 - The Tower of Babylon (White Book)