定义一个节点结构体,包括节点的名称、双亲节点、子节点,定义一个根节点,作为族谱的根节点,初始化族谱,添加至少10个节点,提供查询功能,根据输入的指定人物名称,递归查找其双亲节点和所有子孙节点,提供修改功能,根据输入的指定人物名称,将其修改为新的名称,插入新的子孙节点,并更新其子孙节点的双亲节点,用Java代码实现
时间: 2023-12-10 16:03:29 浏览: 86
计算给定二叉树叶子节点的数目
以下是实现代码:
```
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Node {
String name;
Node parent;
List<Node> children;
Node(String name) {
this.name = name;
this.parent = null;
this.children = new ArrayList<>();
}
}
public class FamilyTree {
Node root;
FamilyTree(String name) {
this.root = new Node(name);
}
void addNode(String name, String parentName) {
Node node = new Node(name);
Node parent = searchNode(this.root, parentName);
if (parent != null) {
node.parent = parent;
parent.children.add(node);
} else {
System.out.println("Parent node not found.");
}
}
Node searchNode(Node node, String name) {
if (node.name.equals(name)) {
return node;
} else {
Node result = null;
for (Node child : node.children) {
result = searchNode(child, name);
if (result != null) {
break;
}
}
return result;
}
}
void modifyNode(String name, String newName) {
Node node = searchNode(this.root, name);
if (node != null) {
node.name = newName;
updateChildrenParent(node);
} else {
System.out.println("Node not found.");
}
}
void updateChildrenParent(Node node) {
for (Node child : node.children) {
child.parent = node;
updateChildrenParent(child);
}
}
void printSubtree(Node node) {
System.out.println(node.name);
for (Node child : node.children) {
printSubtree(child);
}
}
public static void main(String[] args) {
FamilyTree familyTree = new FamilyTree("Adam");
familyTree.addNode("Bob", "Adam");
familyTree.addNode("Charlie", "Adam");
familyTree.addNode("David", "Bob");
familyTree.addNode("Eva", "Bob");
familyTree.addNode("Frank", "Charlie");
familyTree.addNode("Grace", "Charlie");
familyTree.addNode("Helen", "David");
familyTree.addNode("Ivy", "David");
familyTree.addNode("Jack", "Eva");
familyTree.addNode("Kate", "Eva");
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a name to search for:");
String name = scanner.nextLine();
Node node = familyTree.searchNode(familyTree.root, name);
if (node != null) {
System.out.println("Subtree:");
familyTree.printSubtree(node);
} else {
System.out.println("Node not found.");
}
System.out.println("Enter a name to modify:");
name = scanner.nextLine();
System.out.println("Enter a new name:");
String newName = scanner.nextLine();
familyTree.modifyNode(name, newName);
System.out.println("Enter a name to search for:");
name = scanner.nextLine();
node = familyTree.searchNode(familyTree.root, name);
if (node != null) {
System.out.println("Subtree:");
familyTree.printSubtree(node);
} else {
System.out.println("Node not found.");
}
}
}
```
在这个实现中,我们定义了一个节点结构体 `Node`,包括节点的名称、双亲节点、子节点。我们还定义了一个族谱的类 `FamilyTree`,包括族谱的根节点和一些方法,如添加节点、查找节点、修改节点和打印子树。
在 `main` 方法中,我们初始化了一个族谱,并添加了至少10个节点。然后,我们先输入一个要查找的人物名称,并递归查找其双亲节点和所有子孙节点。接下来,我们输入一个要修改的人物名称和新的名称,并将其修改为新的名称,插入新的子孙节点,并更新其子孙节点的双亲节点。最后,我们再次输入一个要查找的人物名称,并递归查找其双亲节点和所有子孙节点,以验证修改是否成功。
阅读全文