当前位置:网站首页>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.
边栏推荐
猜你喜欢
AOSP CameraLatencyHistogram的原理与使用
Adobe是什么?
Cloud platform construction solutions
[MySQL Advanced] Creation and Management of Databases and Tables
PowerMockup 4.3.4::::Crack
授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节试读版
Quickly build a website with static files
中国企业构建边缘计算解决方案的最佳实践
斩获双奖|易知微荣获“2021中国数字孪生解决方案优秀供应商”“中国智能制造优秀推荐产品”双奖项!
【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。
随机推荐
老板:公司系统太多,能不能实现账号互通?
如何创建一个Web项目
Take an example of a web worker
【day1】
First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
L2-029 特立独行的幸福
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
override学习(父类和子类)
utlis thread pool
ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
node连接mysql数据库报错:Client does not support authentication protocol requested by server
utils timer
utlis 线程池
软件测试内卷严重,如何提升自己的竞争力呢?
HDU 5655 CA Loves Stick
mysql如何将表结构导出到excel
易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
Republish the lab report
What is memoization and what is it good for?
《数字经济全景白皮书》金融数字用户篇 重磅发布!