当前位置:网站首页>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.

边栏推荐
猜你喜欢
随机推荐
Nine ways to teach you to read the file path in the resources directory
What is memoization and what is it good for?
UVa 1025 - A Spy in the Metro(白书)
UVa 10003 - Cutting Sticks (White Book, Interval DP)
2022-08-02 mysql/stonedb慢SQL-Q18-内存使用暴涨分析
RPA power business automation super order!
为什么我们需要回调
举一个 web worker 的例子
L2-029 特立独行的幸福
图的基础概念
Why do we need callbacks
AOSP CameraLatencyHistogram的原理与使用
电商秒杀系统
Conditional Statements for Shell Programming
start with connect by implements recursive query
HDU 5655 CA Loves Stick
Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
嵌入式系统:GPIO
直播预告 | 构建业务智联,快速拥抱财务数字化转型









