多叉树的构建及遍历

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

一、多叉树节点

package com.wbf.tree;

import java.util.ArrayList;
import java.util.List;

//多叉树的节点
public class ManyTreeNode {
	// 节点的内容
	private NodeBean data;
	// 节点列表
	private List childList;

	// 构造函数
	public ManyTreeNode() {
		data = new NodeBean();
		childList = new ArrayList();
	}

	// 构造函数 可以指定key的值
	public ManyTreeNode(int key) {
		data = new NodeBean();
		data.setKey(key);
		childList = new ArrayList();
	}

	public NodeBean getData() {
		return data;
	}

	public void setData(NodeBean data) {
		this.data = data;
	}

	public List getChildList() {
		return childList;
	}

	public void setChildList(List childList) {
		this.childList = childList;
	}

}

二、节点数据

package com.wbf.tree;

//节点数据
public class NodeBean {
    
    private int key;
    private String nodeName;
 
    public String getNodeName() {
        return nodeName;
    }
 
    public void setNodeName(String nodeName) {
        this.nodeName = nodeName;
    }
 
    public int getKey() {
        return key;
    }
 
    public void setKey(int key) {
        this.key = key;
    }
}

三、多叉树创建与递归遍历

package com.wbf.tree;

//多叉树
public class ManyNodeTree {
	// 树根
	private ManyTreeNode root;

	// 构造函数
	public ManyNodeTree() {
		root = new ManyTreeNode();
		root.getData().setNodeName("root");
	}

	// 构造函数
	public ManyNodeTree(int key) {
		root = new ManyTreeNode();
		root.getData().setKey(key);
		root.getData().setNodeName("root");
	}

	public ManyTreeNode getRoot() {
		return root;
	}

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

	// 遍历多叉树
	public String iteratorTree(ManyTreeNode treeNode) {

		StringBuilder sb = new StringBuilder();

		if (treeNode != null) {

			if ("root".equals(treeNode.getData().getNodeName())) {
				sb.append(treeNode.getData().getKey() + ",");
			}

			for (ManyTreeNode index : treeNode.getChildList()) {

				sb.append(index.getData().getKey() + ",");

				if (index.getChildList() != null && index.getChildList().size() > 0) {

					sb.append(iteratorTree(index));

				}
			}
		}

		return sb.toString();
	}

	// 构造多叉树
	public static ManyNodeTree createTree() {

		// 用构造函数指定根节点的值
		ManyNodeTree tree = new ManyNodeTree(10);

		// 第一层的节点
		ManyTreeNode node20 = new ManyTreeNode(20);
		ManyTreeNode node30 = new ManyTreeNode(30);
		ManyTreeNode node40 = new ManyTreeNode(40);

		tree.getRoot().getChildList().add(0, node20);
		tree.getRoot().getChildList().add(1, node30);
		tree.getRoot().getChildList().add(2, node40);

		// 第二层的节点
		ManyTreeNode node50 = new ManyTreeNode(50);
		ManyTreeNode node60 = new ManyTreeNode(60);
		ManyTreeNode node70 = new ManyTreeNode(70);
		ManyTreeNode node80 = new ManyTreeNode(80);
		ManyTreeNode node90 = new ManyTreeNode(90);
		ManyTreeNode node100 = new ManyTreeNode(100);

		tree.getRoot().getChildList().get(0).getChildList().add(0, node50);
		tree.getRoot().getChildList().get(0).getChildList().add(1, node60);
		tree.getRoot().getChildList().get(0).getChildList().add(2, node70);

		tree.getRoot().getChildList().get(1).getChildList().add(0, node80);
		tree.getRoot().getChildList().get(1).getChildList().add(1, node90);

		tree.getRoot().getChildList().get(2).getChildList().add(0, node100);

		// 第三层的节点
		ManyTreeNode node110 = new ManyTreeNode(110);
		ManyTreeNode node120 = new ManyTreeNode(120);
		ManyTreeNode node130 = new ManyTreeNode(130);
		ManyTreeNode node140 = new ManyTreeNode(140);

		tree.getRoot().getChildList().get(0).getChildList().get(0).getChildList().add(0, node110);
		tree.getRoot().getChildList().get(0).getChildList().get(0).getChildList().add(1, node120);
		tree.getRoot().getChildList().get(0).getChildList().get(0).getChildList().add(2, node130);

		tree.getRoot().getChildList().get(0).getChildList().get(2).getChildList().add(0, node140);

		return tree;

	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		ManyNodeTree testTree = ManyNodeTree.createTree();
		String result = testTree.iteratorTree(testTree.getRoot());
		System.out.println(result);
	}

}

构造出来的树如下图所示:

多叉树的构建及遍历_第1张图片

四、运行结果

10,20,50,110,120,130,60,70,140,30,80,90,40,100,

转载于:https://my.oschina.net/wangbaofeng/blog/686114

你可能感兴趣的:(多叉树的构建及遍历)