java多叉树_Java对多叉树的实现,并实现遍历插入

多叉树实现,并进行查询插入

package com.tree;

import java.util.List;

import java.util.ArrayList;

import java.io.Serializable;

public class TreeNode implements Serializable {

private int parentId;

private int selfId;

protected String nodeName;

protected Object obj;

protected TreeNode parentNode;

protected List childList;

public TreeNode() {

initChildList();

}

public TreeNode(TreeNode parentNode) {

this.getParentNode();

initChildList();

}

public boolean isLeaf() {

if (childList == null) {

return true;

} else {

if (childList.isEmpty()) {

return true;

} else {

return false;

}

}

}

/* 插入一个child节点到当前节点中 */

public void addChildNode(TreeNode treeNode) {

initChildList();

childList.add(treeNode);

}

public void initChildList() {

if (childList == null)

childList = new ArrayList();

}

public boolean isValidTree() {

return true;

}

/* 返回当前节点的父辈节点集合 */

public List getElders() {

List elderList = new ArrayList();

TreeNode parentNode = this.getParentNode();

if (parentNode == null) {

return elderList;

} else {

elderList.add(parentNode);

elderList.addAll(parentNode.getElders());

return elderList;

}

}

/* 返回当前节点的晚辈集合 */

public List getJuniors() {

List juniorList = new ArrayList();

List childList = this.getChildList();

if (childList == null) {

return juniorList;

} else {

int childNumber = childList.size();

for (int i = 0; i < childNumber; i++) {

TreeNode junior = childList.get(i);

juniorList.add(junior);

juniorList.addAll(junior.getJuniors());

}

return juniorList;

}

}

/* 返回当前节点的孩子集合 */

public List getChildList() {

return childList;

}

/* 删除节点和它下面的晚辈 */

public void deleteNode() {

TreeNode parentNode = this.getParentNode();

int id = this.getSelfId();

if (parentNode != null) {

parentNode.deleteChildNode(id);

}

}

/* 删除当前节点的某个子节点 */

public void deleteChildNode(int childId) {

List childList = this.getChildList();

int childNumber = childList.size();

for (int i = 0; i < childNumber; i++) {

TreeNode child = childList.get(i);

if (child.getSelfId() == childId) {

childList.remove(i);

return;

}

}

}

/* 动态的插入一个新的节点到当前树中 */

public boolean insertJuniorNode(TreeNode treeNode) {

int juniorParentId = treeNode.getParentId();

if (this.parentId == juniorParentId) {

addChildNode(treeNode);

return true;

} else {

List childList = this.getChildList();

int childNumber = childList.size();

boolean insertFlag;

for (int i = 0; i < childNumber; i++) {

TreeNode childNode = childList.get(i);

insertFlag = childNode.insertJuniorNode(treeNode);

if (insertFlag == true)

return true;

}

return false;

}

}

/* 找到一颗树中某个节点 */

public TreeNode findTreeNodeById(int id) {

if (this.selfId == id)

return this;

if (childList.isEmpty() || childList == null) {

return null;

} else {

int childNumber = childList.size();

for (int i = 0; i < childNumber; i++) {

TreeNode child = childList.get(i);

TreeNode resultNode = child.findTreeNodeById(id);

if (resultNode != null) {

return resultNode;

}

}

return null;

}

}

/* 遍历一棵树,层次遍历 */

public void traverse() {

if (selfId < 0)

return;

print(this.selfId);

if (childList == null || childList.isEmpty())

return;

int childNumber = childList.size();

for (int i = 0; i < childNumber; i++) {

TreeNode child = childList.get(i);

child.traverse();

}

}

public void print(String content) {

System.out.println(content);

}

public void print(int content) {

System.out.println(String.valueOf(content));

}

public void setChildList(List childList) {

this.childList = childList;

}

public int getParentId() {

return parentId;

}

public void setParentId(int parentId) {

this.parentId = parentId;

}

public int getSelfId() {

return selfId;

}

public void setSelfId(int selfId) {

this.selfId = selfId;

}

public TreeNode getParentNode() {

return parentNode;

}

public void setParentNode(TreeNode parentNode) {

this.parentNode = parentNode;

}

public String getNodeName() {

return nodeName;

}

public void setNodeName(String nodeName) {

this.nodeName = nodeName;

}

public Object getObj() {

return obj;

}

public void setObj(Object obj) {

this.obj = obj;

}

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java叉树可以用于构建家谱,以下是一个简单的实现示例: ```java class Person { private String name; private List<Person> children; public Person(String name) { this.name = name; this.children = new ArrayList<>(); } public String getName() { return name; } public void addChild(Person child) { this.children.add(child); } public List<Person> getChildren() { return children; } } public class FamilyTree { private Person root; public FamilyTree(String name) { this.root = new Person(name); } public Person getRoot() { return root; } public void addPerson(Person parent, Person child) { parent.addChild(child); } public static void main(String[] args) { FamilyTree familyTree = new FamilyTree("张三"); Person parent1 = familyTree.getRoot(); Person child1 = new Person("李四"); familyTree.addPerson(parent1, child1); Person parent2 = child1; Person child2 = new Person("王五"); familyTree.addPerson(parent2, child2); System.out.println(familyTree.getRoot().getName()); for (Person child : familyTree.getRoot().getChildren()) { System.out.println(" " + child.getName()); for (Person grandChild : child.getChildren()) { System.out.println(" " + grandChild.getName()); } } } } ``` 在上面的示例中,`Person`类代表一个人,每个人有一个名字和多个子节点,即其子女。`FamilyTree`类代表家谱,其中`root`字段为根节点,表示家谱的起始人物。`addPerson`方法用于向家谱中添加人物。在`main`方法中,先创建家谱的起始人物张三,然后添加李四作为其子女,再添加王五作为李四的子女。最后,通过遍树的方式输出家谱的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值