Topic 1090: print the path. N-tree

Source: Internet
Author: User
Tags java treeset
Description:

Give you a string of paths, such:
A \ B \ c
A \ D \ e
B \ CST
D \
You can draw out the directory structure contained in these paths. The subdirectories are directly listed under the parent directory, which is one cell to the right than the parent directory, just like this:
A
B
C
D
E
B
CST
D
At the same level, they must be arranged alphabetically.

Input:

The first behavior of each test case is a positive integer n (n <= 10), which indicates that there are N paths. When n is 0, the test ends, followed by N rows, each line has a string representing a path with a length less than 50.

Output:

Output directory structure. The output of each test sample follows a blank line.

Sample input:
4a\b\ca\d\eb\cstd\0
Sample output:
a  b    c  d    eb  cstd

I have built a multi-Cross Tree here. Sibling nodes are sorted.

Customize a heel node. Each node has a prefix (substring), representing their path. If they are the same, they are in the same directory.

Java treeset can be used for automatic sorting.

It is explained in the code comment.

Dictionary trees are also used on the Internet. Another simple method is A/B/C, which can be viewed as files a, a/B, A/B/C. In this way, all files are sorted according to custom rules, in the output.

Public class pathprint_1090 {static int N; static node root; // defines a root node public static void main (string [] ARGs) {dynamic partition = new partition (system. in); While (partial. hasnextint () {n = bytes. nextint (); If (n = 0) break; string STR; node root = new node (); root. key = ""; root. substring = ""; root. children = new treeset <node> (); For (INT I = 0; I <n; I ++) {STR = random. next (); root. addchild (new node (Str. SP Substring ("\\\\"), 0, root); // splits the read string into an array of characters to construct a linked list} root. print (); system. out. println () ;}} class node implements comparable <node> {Public String key; // name to be output public string substring; // prefix, public treeset <node> children; // sub-node in the same directory. The number of public node father; // The parent node public int pre; // represents the offset public int compareto (node O) {// defines the comparison function. That is, compare the Prefix A \ B \ = A \ B \, is a directory return this. substring. compareto (O. substring) ;}public node () {}// constructs a node string based on a character array. public node (string STR [], int index, node f) {This. substring = ""; if (Index = Str. length) // return directly at the end; For (INT I = 1; I <= index; I ++) This. substring + = STR [I] + '\'; // obtain the prefix if (Index = 0) This. substring = STR [0]; this. key = STR [Index]; this. pre = f. pre + F. key. length () + 1; // The offset is equal to the offset of the parent node. + Parent node length: This. Father = f; If (index! = Str. length) {If (this. children = NULL) This. children = new treeset <node> (); children. add (new node (STR, index + 1, this) ;}} public void addchild (node N) {// Add a child node // because the child node may already exist, merge if (children. contains (N) {node temp = NULL; For (node temp2: Children) {If (temp2.compareto (n) = 0) temp = temp2;} temp. addchild (N. children. first ();} else {children. add (n) ;}} public void print () {If (this. key = NULL) Re Turn; If (this. Key! = "") {Pre --; // since the first position will contain multiple spaces while (pre --> 0) system. out. print (""); system. out. println (key);} If (this. children! = NULL & Children. Size ()> 0) {for (node N: Children) n. Print ();}}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.