Tree树结构java实现


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

/**
 * 树状结构
 * 
 *
 * @param <T> 树中数据的类型
 */
public class Tree<T>
{
	private T data;
	
	/**
	 * 子树(节点)
	 */
	private List<Tree<T>> childs;
	
	/**
	 * 父节点
	 */
	private Tree<T> parent;
	private Long parentId;
	public Tree()
	{
		data = null;
		childs = new ArrayList<>();
		childs.clear();
	}
	
	public Tree(T data)
	{
		this.data = data;
		childs = new ArrayList<>();
		childs.clear();
	}
	
	/**
	 * 添加子树
	 * 
	 * @param tree 子树
	 */
	public void addNode(Tree<T> tree)
	{
		childs.add(tree);
		tree.setParent(this);
	}
	
	public Long getParentId()
	{
		return parentId;
	}

	public void setParentId(Long parentId)
	{
		this.parentId = parentId;
	}

	/**
	 * 置空树
	 */
	public void clearTree()
	{
		data = null;
		parent = null;
		childs.clear();
	}
	
	/**
	 * 求树的深度
	 * 
	 * @return 树的深度
	 */
	public int dept()
	{
		return dept(this);
	}
	
	/**
	 * 求树的深度
	 * 
	 * @param tree
	 * @return
	 */
	private int dept(Tree<T> tree)
	{
		if (tree.isEmpty())
		{
			return 0;
		}
		else if (tree.isLeaf())
		{
			return 1;
		}
		else
		{
			int n = childs.size();
			int[] a = new int[n];
			for (int i = 0; i < n; i++)
			{
				if (childs.get(i).isEmpty())
				{
					a[i] = 0 + 1;
				}
				else
				{
					a[i] = dept(childs.get(i)) + 1;
				}
			}
			Arrays.sort(a);
			return a[n - 1];
		}
	}
	
	/**
	 * 获取父树(节点)
	 * 
	 * @return
	 */
	public Tree<T> getParent()
	{
		return parent;
	}
	
	public void setParent(Tree<T> parent)
	{
		this.parent = parent;
	}
	
	public boolean hasParent()
	{
		return parent != null;
	}
	
	/**
	 * 返回第i个子树
	 * 
	 * @param i
	 * @return
	 */
	public Tree<T> getChild(int i)
	{
		return childs.get(i);
	}
	
	/**
	 * 求第一个孩子 结点
	 * 
	 * @return
	 */
	public Tree<T> getFirstChild()
	{
		return childs.get(0);
		
	}
	
	/**
	 * 求最后 一个孩子结点
	 * 
	 * @return
	 */
	public Tree<T> getLastChild()
	{
		return childs.get(childs.size() - 1);
	}
	
	/**
	 * 获取子树
	 * 
	 * @return
	 */
	public List<Tree<T>> getChilds()
	{
		return childs;
	}
	
	/**
	 * 判断是否为空树
	 * 
	 * @return 如果为空,返回true,否则返回false
	 */
	public boolean isEmpty()
	{
		if (childs.isEmpty() && data == null)
		{
			return true;
		}
		return false;
	}
	
	/**
	 * 判断是否为叶子结点
	 * 
	 * @return
	 */
	public boolean isLeaf()
	{
		if (childs.isEmpty())
		{
			return true;
		}
		return false;
	}

	public T getData()
	{
		return this.data;
	}
	 
	
	/**
	 * 求结点数
	 * 
	 * @return 结点的个数
	 */
	public int size()
	{
		return size(this);
	}
	
	/**
	 * 求结点数,有待完善
	 * 
	 * @param tree
	 * @return
	 */
	private int size(Tree<T> tree)
	{
		if (tree.isEmpty())
		{
			return 0;
		}
		else if (tree.isLeaf())
		{
			return 1;
		}
		else
		{
			int count = 1;
			int n = childs.size();
			for (int i = 0; i < n; i++)
			{
				if (!childs.get(i).isEmpty())
				{
					count += size(childs.get(i));
				}
			}
			return count;
		}
	}
	
	public static void main(String[] args)
	{
		Tree<String> tc = new Tree<String>("root");
		tc.addNode(new Tree("A"));
		tc.addNode(new Tree("B"));
		System.out.println("树的深度为:" + tc.dept());
		List<Tree<String>> list = tc.getChilds();
		System.out.println("子节点为:");
		for (Tree<String> node : list)
		{
			System.out.println(node);
		}
	}
	
	@Override
	public String toString()
	{
		return "Tree [data=" + data + "]";
	}
}

该Tree类有三个主要属性为父子节点引用和数据:

data用于保存数据

parent为父节点引用

childs为子节点引用

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树形结构计算可以使用 Java 中的树形数据结构实现,例如二叉树、B树等。以下是一个简单的二叉树的实现,可用于树形结构计算: ```java class Node { int value; Node left; Node right; Node(int value) { this.value = value; left = null; right = null; } } class BinaryTree { Node root; BinaryTree() { root = null; } void insert(int value) { root = insertRecursive(root, value); } Node insertRecursive(Node current, int value) { if (current == null) { return new Node(value); } if (value < current.value) { current.left = insertRecursive(current.left, value); } else if (value > current.value) { current.right = insertRecursive(current.right, value); } else { // value already exists return current; } return current; } int sum() { return sumRecursive(root); } int sumRecursive(Node node) { if (node == null) { return 0; } return node.value + sumRecursive(node.left) + sumRecursive(node.right); } } public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); // insert nodes to the binary tree tree.insert(5); tree.insert(3); tree.insert(7); tree.insert(1); tree.insert(9); // calculate the sum of all nodes int sum = tree.sum(); System.out.println("Sum of all nodes: " + sum); } } ``` 上述代码中,我们首先定义了一个节点 Node 类,包含节点的值和左右子节点。然后定义了一个 BinaryTree 类,包含根节点和插入、计算节点和的方法。在 Main 类中,我们创建了一个 BinaryTree 对象,插入了一些节点,并计算了所有节点的和。 当然,这只是一个简单的例子,你可以根据具体的需求来实现更复杂的树形结构计算。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值