package tree;
import java.io.BufferedInputStream;
import java.util.Scanner;
/*
6 3
1
2
3
4
5
6
2 is a child of 1
4 is a child of 2
6 is a child of 4
输出:
true
true
false
*/
class Node2{//如果node中的成员变量改为father会更好做,搜一下网上实现的吧,我做的这个只能检测child的,只是为了实现一下二叉树表示多叉树
String name;
Node2 firstchild;
Node2 nextsibling;
public void addson(Node2 node){
if(this.firstchild == null)
this.firstchild = node;
else{
Node2 next = this.firstchild;
while(next.nextsibling!=null){
next = next.nextsibling;
}
next.nextsibling = node;//注意!!!!!!对next赋值没有任何意义,并不能起到连接作用
}
}
}
public class 二叉树表示多叉树 {
public static int space(String s){
int num = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i) == ' ')
num++;
}
return num;
}
public static void reverse(Node2 node){
if(node==null) return;
System.out.print(node.name);
reverse(node.firstchild);
reverse(node.nextsibling);
}
public static boolean findchild(Node2 node,String s){
if(node==null) return false;
if(node.name.equals(s))
return true;
return (findchild(node.firstchild,s)||findchild(node.nextsibling,s));
}
public static void main(String[] args) {
String[] ss = new String[100];//储存第i个人的名字
Node2[] arr = new Node2[100]; //储存第i个人的结点,与前面的对应,好好理解一下这个问题,关键在于剥离出一个编号,当做数组下标
Scanner sc = new Scanner(new BufferedInputStream(System.in));
int n = sc.nextInt();
int m = sc.nextInt();
sc.nextLine();//前面是next后面是nextLine 是要注意回车的吸收,因为next是不吸收回车的
for(int i=0;i<n;i++){
String s = sc.nextLine();
ss[i] = s;
int k=i;
if(k == 0)
{arr[i] = new Node2();arr[i].firstchild = null;arr[i].nextsibling = null;arr[i].name = s;}
else
while(true){
k--;
if(space(s)==space(ss[k])+2){
arr[i] = new Node2();arr[i].firstchild = null;arr[i].nextsibling = null;arr[i].name = s;
arr[k].addson(arr[i]);
break;
}
}
}
//reverse(arr[0]);
String[] ss2 = new String[100];
for(int i=0;i<n;i++){
ss2[i] = ss[i].replace(" ", "");
}
for(int i=0;i<m;i++){
String s1 = sc.next();
int a=-1;
for(int j=0;j<n;j++){
if(ss2[j].equals(s1)){
a=j;
}
}sc.next();sc.next();
String s2 = sc.next();
sc.next();
String s3 = sc.next();
int b=-1;
for(int j=0;j<n;j++){
if(ss2[j].equals(s3)){
b=j;
}
}
if(s2.equals("child")){
System.out.println(findchild(arr[b].firstchild,ss[a]));
}
}
}
}
/*
* 5-27 家谱处理 (30分)
人类学研究对于家族很感兴趣,于是研究人员搜集了一些家族的家谱进行研究。实验中,使用计算机处理家谱。为了实现这个目的,研究人员将家谱转换为文本文件。下面为家谱文本文件的实例:
John
Robert
Frank
Andrew
Nancy
David
家谱文本文件中,每一行包含一个人的名字。第一行中的名字是这个家族最早的祖先。家谱仅包含最早祖先的后代,而他们的丈夫或妻子不出现在家谱中。每个人的子女比父母多缩进2个空格。以上述家谱文本文件为例,John这个家族最早的祖先,他有两个子女Robert和Nancy,Robert有两个子女Frank和Andrew,Nancy只有一个子女David。
在实验中,研究人员还收集了家庭文件,并提取了家谱中有关两个人关系的陈述语句。下面为家谱中关系的陈述语句实例:
John is the parent of Robert
Robert is a sibling of Nancy
David is a descendant of Robert
研究人员需要判断每个陈述语句是真还是假,请编写程序帮助研究人员判断。
输入格式:
输入首先给出2个正整数NN(2\le N\le 1002≤N≤100)和MM(\le 100≤100),其中NN为家谱中名字的数量,MM为家谱中陈述语句的数量,输入的每行不超过70个字符。
名字的字符串由不超过10个英文字母组成。在家谱中的第一行给出的名字前没有缩进空格。家谱中的其他名字至少缩进2个空格,即他们是家谱中最早祖先(第一行给出的名字)的后代,且如果家谱中一个名字前缩进kk个空格,则下一行中名字至多缩进k+2k+2个空格。
在一个家谱中同样的名字不会出现两次,且家谱中没有出现的名字不会出现在陈述语句中。每句陈述语句格式如下,其中X和Y为家谱中的不同名字:
X is a child of Y
X is the parent of Y
X is a sibling of Y
X is a descendant of Y
X is an ancestor of Y
输出格式:
对于测试用例中的每句陈述语句,在一行中输出True,如果陈述为真,或False,如果陈述为假。
输入样例:
6 5
John
Robert
Frank
Andrew
Nancy
David
Robert is a child of John
Robert is an ancestor of Andrew
Robert is a sibling of Nancy
Nancy is the parent of Frank
John is a descendant of Andrew
输出样例:
True
True
True
False
False
*/
二叉树表示多叉树.java
最新推荐文章于 2022-01-05 08:29:46 发布
