INPUT: (A,B) (A,C) (B,G) (C,H) (E,F) (B,D) (C,E)
OUTPUT: (A(B(D)(G))(C(E(F))(H)))
(parent(child)(child))
Error Code Type of error
E1 More than 2 children
E2 Duplicate Edges - (A,B) (A,B)
E3 Cycle present
E4 Multiple roots
E5 Any other error
就是给一个图,判断是不是合法的二叉树,是的话,输出二叉树的字符串表达式,不是的话,输出错误码。
public class GraphEdgeTree { public static class Edge { char from, to; public Edge(String exp) { String[] nodes = exp.replaceAll("[\\(\\)\\s]", "").split(","); from = nodes[0].charAt(0); to = nodes[1].charAt(0); } } private Map<Character, Character> map = new HashMap<>(); private Map<Character, Set<Character>> childMap = new HashMap<>(); private List<Edge> edges = new ArrayList<>(); public GraphEdgeTree(String exp) { String[] arr = exp.split("\\s"); for(String str:arr) { Edge edge = new Edge(str); edges.add(edge); map.put(edge.from, edge.from); map.put(edge.to, edge.to); Set<Character> children = childMap.get(edge.from); if(children == null) { children = new HashSet<Character>(); } children.add(edge.to); childMap.put(edge.from, children); } } public Character find(char x) { Character parent = map.get(x); if(parent == null || parent == x) { return parent; } return find(parent); } public void union(char x, char y) { Character xp = find(x); Character yp = find(y); map.put(xp, yp); } public boolean isEdgeExisted(Edge edge) { Character p = map.get(edge.to); return p != null && p == edge.from; } public String buildTree() { for(Character key:childMap.keySet()) { Set<Character> set = childMap.get(key); if(set != null && set.size()>2) { return "E1"; } } for(Edge edge:edges) { if(isEdgeExisted(edge)) { return "E2"; } char xp = find(edge.from); char yp = find(edge.to); if(xp == yp) { return "E3"; } union(edge.to, edge.from); } if(rootNum() > 1) { return "E4"; } return null; } private int rootNum() { int cnt = 0; for(char key:map.keySet()) { if(find(key) == key) { cnt++; } } return cnt; } public String treeString(char root) { Set<Character> children = childMap.get(root); if(children == null || children.size() == 0) { return "("+root+")"; } StringBuilder result = new StringBuilder("("+root); for(char child:children) { result.append(",").append(treeString(child)); } result.append(")"); return result.toString(); } public char getRoot() { return find(edges.get(0).from); } public static void main(String[] args) { String input = "(A,B) (A,C) (B,G) (C,H) (E,F) (B,D) (C,E)"; GraphEdgeTree gt = new GraphEdgeTree(input); String error = gt.buildTree(); if(error != null) { System.out.println(error); } else { String result = gt.treeString(gt.getRoot()); System.out.println(result); } } }
相关推荐
"Determine if a Directory Exists"这个主题就涉及到了如何检查计算机系统中是否存在特定的目录。这是一个基础但至关重要的功能,因为它广泛应用于文件管理、数据备份、安装程序验证等多个场景。 在Windows操作系统...
这个题目名为"Given-a-string-containing-just-the-characters-and-determine-if-the-input",实际上是在询问我们如何编写一个函数或程序来检查一个字符串是否只包含特定的一组字符。 首先,让我们明确一下问题的...
"Determine if a File Exists" 这个主题涵盖了文件系统操作的核心概念,它在各种应用中都有广泛的应用,比如数据验证、文件处理流程或者用户交互。下面将详细阐述如何在不同的编程语言和环境下检查文件是否存在。 1...
The FTP protocol is specified in RFC 959. The library has been tested on linux, OpenVMS and Windows NT. It should also work without major modification on other POSIX systems. All programs using the ...
Enter a three-digit number to determine whether the number is a daffodil number or not, and output t.zip
If you have a partition that is incompatible with MS-DOS 5.0, you must delete it from your hard disk. Incompatible partitions are listed as "Non-DOS" partitions in FDISK. See the procedure for ...
determine if it has a cycle in it. Follow up:Can you solve it without using extra space? 解决思路 声明一个慢指针和一个快指针 慢指针一次后移一个节点,快指针一次后移两个节点 不断后移至指针指空,或两指针...
据世界卫生组织称,在艾滋病高流行地区,涂片阴性和肺外肺结核的发病率正在增加。 结核病诊断的延迟会导致大量的死亡率过高。 如果我们要降低死亡率,特别是在多哥等低收入国家的艾滋病毒感染人群中由于结核病的合并...
4、DFS.inc - a small include file written by Brad Stowers used to determine the Delphi or BCB version you are trying to compile with With these four files it is extremely easy to install the trees. ...
Delphi's THandle type when a HWND is passed to a function. E.g.: if (DragDetectPlus(THandle(MyControl->Handle), Point(X, Y))) { ... } * Virtual File Stream formats can only be pasted from the ...
id -a root 显示用户所在组的所有组名(如root用户,是所有组的组员) df 查看文件系统,查看数据区 用法 df [-F FSType] [-abeghklntVvZ] [-o FSType 特定选项] [目录 | 块设备 | 资源] df -k 以kbytes显示文件...
在C++编程中,"determine" 这个任务通常涉及到对文件内容的读取以及字符串处理。在这个场景中,我们需要实现一个功能:检查文件中的字符串是否包含特定的符号,并根据找到的符号输出对应的值(正1或负1)。下面我们...
Topics Covered The topics covered in this book are An overview of decision... Gini Criteria & Entropy Criteria - how to tell which split on a decision tree is best among many possible choices And More
if (isNumeric && isInteger && number >= minValue && number ) { validNumbers.Add(number); } ``` 最后,关于源码方面,这段代码可以在按钮点击事件(Button_Click)或者其他合适的事件处理器中执行,以确保在...
% ISRGB uses these criteria to determine if A is an RGB image: % % - If A is of class double, all values must be in the range % [0,1], and A must be M-by-N-by-3. % % - If A is of class uint8 or uint16...
performance and stability.” The architectural guidelines help determine whether a problem that someone wants to be solved is within the scope of the project Chapter 2 Definitions 2.1. Activity An...
1.5 二叉树是否平衡Given a binary tree, determine if it is height-balanced.For this prob
ARMv8-A Processing element (PE), and this manual includes descriptions of: • The two Execution states, AArch64 and AArch32. • The instruction sets: — In AArch32 state, the A32 and T32 instruction ...
- 0000769: UniDBGrid: Row position is ignored if row is immediately changed after a call to Open() - 0000673: UniDBGrid: OnDrawColumnCell event - 0000768: Better "ext\" folder translation - 0000766:...
S170009 - Add the capability to determine if a specific point corresponds to a tile control's title using HitTest information Resolved Issues VCL Subscription Q434991 - Data-aware container controls...