家谱

13 篇文章 0 订阅

数据结构:树(孩子兄弟表示法)

数据集:data.txt

A(A1,A2)
A1(A11,A12,A13)




import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;


public class FMessage {
	
	private PersonMessage root;
	
	public FMessage() {
		super();
		this.root = new PersonMessage();
	}



	public PersonMessage getRoot() {
		return root;
	}



	public void setRoot(PersonMessage root) {
		this.root = root;
	}


	public void createFMessage(String path) throws IOException{
		
		
		BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
		String len;
		while((len =br.readLine()) != null) {
			String[] datas = len.split("[,()]");
			createData(datas);
		}
		
		br.close();
	
	}
	

	public PersonMessage findNode(String name,PersonMessage node) {
		if(node==null)
			return null;
		
		if(node.getName().equals(name))
			return node;
		else {
			
			PersonMessage t1 = findNode(name, node.getBrother());
			PersonMessage t2 = null;
			if(t1==null) {
				t2 = findNode(name, node.getKid());
				if(t2==null)
					return null;
				else
					return t2;
			}
			else 
				return t1;
		}
			
	}
	
	
	
	
	private void createData(String[] datas) {
		if(this.getRoot().getKid() == null) {
			PersonMessage node = new PersonMessage(datas[0],"male",null,null);
			this.getRoot().setKid(node);
			
			PersonMessage bigKid = new PersonMessage(datas[1],"male",null,null);
			node.setKid(bigKid);
			node = bigKid;
			for(int i=2; i<datas.length;i++){
				PersonMessage tnode = new PersonMessage(datas[i],"male",null,null);
				node.setBrother(tnode);
				node = tnode;
			}
		}
		else {
			String par = datas[0];
			PersonMessage parNode = findNode(par,this.getRoot().getKid());
			if(parNode == null){
				System.out.println("家族中并未找到名为:"+par+",顾其孩子不能加入到该家谱!");
				return;
			}
			else {
				
				if(parNode.getKid() == null) {
					PersonMessage bigKid = new PersonMessage(datas[1],"male",null,null);
					parNode.setKid(bigKid);
					for(int i=2; i<datas.length;i++){
						PersonMessage tnode = new PersonMessage(datas[i],"male",null,null);
						bigKid.setBrother(tnode);
						bigKid = tnode;
					}
				}
				else {
					PersonMessage lastKid = parNode.getKid();
					while(lastKid.getBrother()!=null)
						lastKid = lastKid.getBrother();
					for(int i=1; i<datas.length;i++){
						PersonMessage tnode = new PersonMessage(datas[i],"male",null,null);
						lastKid.setBrother(tnode);
						lastKid = tnode;
					}
				}
					
			}
				
		}
			
		
	}



	public void levelTraverse(PersonMessage pm){
		ArrayList<PersonMessage> al = new ArrayList<PersonMessage>();
		al.add(pm);
		int i = 0;
		int len = 1;
		while(i != len) {
			PersonMessage data = al.get(i++);
			System.out.print(data.getName()+" : ");
			
			if(data.getKid()!=null) {
				PersonMessage kid = data.getKid();
				while(kid!=null){
					al.add(kid);
					System.out.print(kid.getName()+"  ");
					kid = kid.getBrother();
					len++;
				}
			}
			else
				System.out.print("none");
			System.out.println();
			
			
		}
		
	}

	
	
	public PersonMessage findParent(PersonMessage data,PersonMessage pm) {
		if(pm.getKid()!=null) {
			PersonMessage tmpKid = pm.getKid();
			if(tmpKid == data)
				return pm;
			else {
				PersonMessage dataT1 = findParent(data,tmpKid);
				if(dataT1 == null){
					PersonMessage tmpKBro = tmpKid.getBrother();
					if(tmpKBro == data)
						return pm;
					else{
						return findParent(data,tmpKBro);
					}
				}
				else
					return dataT1;
			}
			
		}
		else
			return null;
	}
	
	public PersonMessage findParentByName(String name,PersonMessage pm) {
		
		PersonMessage data = findNode(name, pm);
		if(data !=null) {
		
			if(name.equals(this.getRoot().getKid().getName())) {
				System.out.println("此人为祖宗,没有其父亲的记载!");
				return null;
			}
			else{
				
				return findParent(data,pm);	
			}
		}
		else{
			System.out.println("家谱里不存在该人!!!");
			return null;
		}

		
	}
	
	public boolean deletePersonByName(String name) {
		
		PersonMessage r = findParentByName(name, getRoot().getKid());
		
		if(r != null) {
			PersonMessage kid = r.getKid();
			if(kid.getName().equals(name)) {
				if(kid.getBrother() == null) 
					r.setKid(null);
				else
					r.setKid(kid.getBrother());
			}
			else{
				PersonMessage br = kid.getBrother();
				while(!br.getName().equals(name)) {
					kid = br;
					br = br.getBrother();
				}
				if(br.getBrother() == null)
					kid.setBrother(null);
				else
					kid.setBrother(br.getBrother());
			}
			return true;
		}
		else
			return false;
	}
	
	public void  traverseAllKidByName(String name) {
		PersonMessage data = findNode(name,this.getRoot().getKid());
		if(data!=null)
			levelTraverse(data);
		else
			System.out.println("家谱里不存在该人!!!");
		
	}
	

	
	public boolean addPersonByName(String data) {
		
		String[] datas = data.split("[,()]");
		PersonMessage par = this.findNode(datas[0], getRoot().getKid());
		if(par == null) {
			System.out.println("输入有无,在家谱中不能查找相应到父亲的记录!!");
			return false;
		}
		else {
			PersonMessage kid = par.getKid();
			if(kid != null){		
				while(kid.getBrother()!=null)
					kid = kid.getBrother();
				for(int i=1;i<datas.length;i++) {
					PersonMessage d = new PersonMessage(datas[i], "male", null, null);
					kid.setBrother(d);
					kid = d;
				}
			}
			else {
				PersonMessage bigKid = new PersonMessage(datas[1],"male",null,null);
				par.setKid(bigKid);
				for(int i=2; i<datas.length;i++){
					PersonMessage tnode = new PersonMessage(datas[i],"male",null,null);
					bigKid.setBrother(tnode);
					bigKid = tnode;
				}
			}
			return true;
		}
	}
	
	
	
	
	public static void main(String[] args) throws IOException {

		FMessage fm = new FMessage();
		fm.createFMessage("./data.txt");
//		fm.levelTraverse(fm.getRoot().getKid());
//		fm.traverseAllKidByName("A");
		
		PersonMessage r = fm.findParentByName("A", fm.getRoot().getKid());
		if(r != null)
			System.out.println(r.getName());
			
		boolean s = fm.deletePersonByName("A1");
		if(s)
			System.out.println("删除成功!");
		fm.levelTraverse(fm.getRoot().getKid());
		
		System.out.println("========================================");
//		System.out.println("格式如下: 父亲(孩子1,孩子2,....,孩子n)");
		
		String d = "A(A3,A4)";
		fm.addPersonByName(d);
		fm.levelTraverse(fm.getRoot().getKid());
		
		System.out.println("========================================");
		
		String d1 = "A3(A31,A32)";	
		fm.addPersonByName(d1);
		fm.levelTraverse(fm.getRoot().getKid());
		
	}
	
	
	

}

节点的代码:

public class PersonMessage {
	private String name;
	private String sex;
	private PersonMessage brother;
	private PersonMessage kid;
	
	public PersonMessage() {
		this.name = null;
		this.sex = null;
		this.brother = null;
		this.kid = null;		
	}	
	
	public PersonMessage(String name, String sex, PersonMessage brother,
			PersonMessage kid) {
		this.name = name;
		this.sex = sex;
		this.brother = brother;
		this.kid = kid;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public PersonMessage getBrother() {
		return brother;
	}
	public void setBrother(PersonMessage brother) {
		this.brother = brother;
	}
	public PersonMessage getKid() {
		return kid;
	}
	public void setKid(PersonMessage kid) {
		this.kid = kid;
	}
	
	
}


  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值