【code】java树的实现

树的父节点存储实现

import java.util.*;
public class TreeParent<E>
{
	public static class Node<T>
	{
		T data;
		//记录其父节点的位置
		int parent;
		public Node()
		{
		}
		public Node(T data)
		{
			this.data = data;
		}
		public Node(T data , int parent)
		{
			this.data = data;
			this.parent = parent;
		}
		public String toString()
		{
			return "TreeParent$Node[data=" + data + ", parent="
				+ parent + "]";
		}
	}
	private final int DEFAULT_TREE_SIZE = 100;
	private int treeSize = 0;
	//使用一个Node[]数组来记录该树里的所有节点
	private Node<E>[] nodes;
	//记录节点数
	private int nodeNums;
	//以指定根节点创建树
	public TreeParent(E data)
	{
		treeSize = DEFAULT_TREE_SIZE;
		nodes = new Node[treeSize];
		nodes[0] = new Node<E>(data , -1);
		nodeNums++;
	}
	//以指定根节点、指定treeSize创建树
	public TreeParent(E data ,int treeSize)
	{
		this.treeSize = treeSize;
		nodes = new Node[treeSize];
		nodes[0] = new Node<E>(data , -1);
		nodeNums++;
	}
	//为指定节点添加子节点
	public void addNode(E data , Node parent)
	{
		for (int i = 0 ; i < treeSize ; i++)
		{
			//找到数组中第一个为null的元素,该元素保存新节点
			if (nodes[i] == null)
			{
				//创建新节点,并用指定的数组元素保存它
				nodes[i] = new Node(data , pos(parent));;
				nodeNums++;
				return;
			}
		}
		throw new RuntimeException("该树已满,无法添加新节点");
	}
	//判断树是否为空。
	public boolean empty()
	{
		//根节点是否为null
		return nodes[0] == null;
	}
	//返回根节点
	public Node<E> root()
	{
		//返回根节点
		return nodes[0];
	}
	//返回指定节点(非根节点)的父节点。
	public Node<E> parent(Node node)
	{
		//每个节点的parent记录了其父节点的位置
		return nodes[node.parent];
	}
	//返回指定节点(非叶子节点)的所有子节点。
	public List<Node<E>> children(Node parent)
	{
		List<Node<E>> list = new ArrayList<Node<E>>();
		for (int i = 0 ; i < treeSize  ; i++)
		{
			//如果当前节点的父节点的位置等于parent节点的位置
			if (nodes[i] != null &&
				nodes[i].parent == pos(parent))
			{
				list.add(nodes[i]);
			}
		}
		return list;
	}
	//返回该树的深度。
	public int deep()
	{
		//用于记录节点的最大深度
		int max = 0;
		for(int i = 0 ; i < treeSize && nodes[i] != null
			; i++)
		{
			//初始化本节点的深度
			int def = 1;
			//m记录当前节点的父节点的位置
			int m = nodes[i].parent;
			//如果其父节点存在
			while(m != -1 && nodes[m] != null)
			{
				//向上继续搜索父节点
				m = nodes[m].parent;
				def++;
			}
			if(max < def)
			{
				max = def;
			}
		}
		//返回最大深度
		return max; 
	}
	//返回包含指定值的节点。
	public int pos(Node node)
	{
		for (int i = 0 ; i < treeSize ; i++)
		{
			//找到指定节点
			if (nodes[i] == node)
			{
				return i;
			}
		}
		return -1;
	}
	
	public static void main(String[] args) 
	{
		TreeParent<String> tp = new TreeParent<String>("root");
		TreeParent.Node root = tp.root();
		System.out.println(root);
		tp.addNode("节点1" , root);
		System.out.println("此树的深度:" + tp.deep());
		tp.addNode("节点2" , root);
		//获取根节点的所有子节点
		List<TreeParent.Node<String>> nodes = tp.children(root);
		System.out.println("根节点的第一个子节点:" + nodes.get(0));
		//为根节点的第一个子节点新增一个子节点
		tp.addNode("节点3" , nodes.get(0));
		System.out.println("此树的深度:" + tp.deep());
	}
}
 

树的子节点链表实现

import java.util.*;
public class TreeChild<E>
{
	private static class SonNode
	{
		//记录当前节点的位置
		private int pos;
		private SonNode next;
		public SonNode(int pos , SonNode next)
		{
			this.pos = pos;
			this.next = next;
		}
	}
	public static class Node<T>
	{
		T data;
		//记录第一个子节点
		SonNode first;
		public Node(T data)
		{
			this.data = data;
			this.first = null;
		}
		public String toString()
		{
			if (first != null)
			{
				return "TreeChild$Node[data=" + data + ", first="
					+ first.pos + "]";
			}
			else
			{
				return "TreeChild$Node[data=" + data + ", first=-1]";
			}
		}
	}
	private final int DEFAULT_TREE_SIZE = 100;
	private int treeSize = 0;
	//使用一个Node[]数组来记录该树里的所有节点
	private Node<E>[] nodes;
	//记录节点数
	private int nodeNums;
	//以指定根节点创建树
	public TreeChild(E data)
	{
		treeSize = DEFAULT_TREE_SIZE;
		nodes = new Node[treeSize];
		nodes[0] = new Node<E>(data);
		nodeNums++;
	}
	//以指定根节点、指定treeSize创建树
	public TreeChild(E data ,int treeSize)
	{
		this.treeSize = treeSize;
		nodes = new Node[treeSize];
		nodes[0] = new Node<E>(data);
		nodeNums++;
	}
	//为指定节点添加子节点
	public void addNode(E data , Node parent)
	{
		for (int i = 0 ; i < treeSize ; i++)
		{
			//找到数组中第一个为null的元素,该元素保存新节点
			if (nodes[i] == null)
			{
				//创建新节点,并用指定数组元素来保存它
				nodes[i] = new Node(data);
				if (parent.first == null)
				{
					parent.first = new SonNode(i , null);
				}
				else
				{
					SonNode next = parent.first;
					while (next.next != null)
					{
						next = next.next;
					}
					next.next = new SonNode(i , null);
				}
				nodeNums++;
				return;
			}
		}
		throw new RuntimeException("该树已满,无法添加新节点");
	}
	//判断树是否为空。
	public boolean empty()
	{
		//根节点是否为null
		return nodes[0] == null;
	}
	//返回根节点
	public Node<E> root()
	{
		//返回根节点
		return nodes[0];
	}
	//返回指定节点(非叶子节点)的所有子节点。
	public List<Node<E>> children(Node parent)
	{
		List<Node<E>> list = new ArrayList<Node<E>>();
		//获取parent节点的第一个子节点
		SonNode next = parent.first;
		//沿着孩子链不断搜索下一个孩子节点
		while (next != null)
		{
			//添加孩子链中的节点
			list.add(nodes[next.pos]);
			next = next.next;
		}
		return list;
	}
	//返回指定节点(非叶子节点)的第index个子节点。
	public Node<E> child(Node parent , int index)
	{
		//获取parent节点的第一个子节点
		SonNode next = parent.first;
		//沿着孩子链不断搜索下一个孩子节点
		for (int i = 0 ; next != null  ; i++)
		{
			if (index == i)
			{
				return nodes[next.pos];
			}
			next = next.next;
		}
		return null;
	}
	//返回该树的深度。
	public int deep()
	{
		//获取该树的深度
		return deep(root()); 
	}
	//这是一个递归方法:每棵子树的深度为其所有子树的最大深度 + 1
	private int deep(Node node)
	{
		if (node.first == null)
		{
			return 1;
		}
		else
		{
			//记录其所有子树的最大深度
			int max = 0;
			SonNode next = node.first;
			//沿着孩子链不断搜索下一个孩子节点
			while (next != null)
			{
				//获取以其子节点为根的子树的深度
				int tmp = deep(nodes[next.pos]);
				if (tmp > max)
				{
					max = tmp;
				}
				next = next.next;
			}
			//最后返回其所有子树的最大深度 + 1
			return max + 1;
		}
	}
	//返回包含指定值的节点。
	public int pos(Node node)
	{
		for (int i = 0 ; i < treeSize ; i++)
		{
			//找到指定节点
			if (nodes[i] == node)
			{
				return i;
			}
		}
		return -1;
	}
	
	public static void main(String[] args) 
	{
		TreeChild<String> tp = new TreeChild<String>("root");
		TreeChild.Node root = tp.root();
		System.out.println("根节点:" + root);
		tp.addNode("节点1" , root);
		tp.addNode("节点2" , root);
		tp.addNode("节点3" , root);
		System.out.println("添加子节点后的根节点:" + root);
		System.out.println("此树的深度:" + tp.deep());
		//获取根节点的所有子节点
		List<TreeChild.Node<String>> nodes = tp.children(root);
		System.out.println("根节点的第一个子节点:" + nodes.get(0));
		//为根节点的第一个子节点新增一个子节点
		tp.addNode("节点4" , nodes.get(0));
		System.out.println("根节点的第一个子节点:" + nodes.get(0));
		System.out.println("此树的深度:" + tp.deep());
	}
}
 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哈夫曼是一种带权路径长度最短的,常用于数据压缩。在Java中,可以通过构建哈夫曼实现哈夫曼编码。下面是一个简单的哈夫曼Java实现的步骤: 1. 定义一个Node类作为哈夫曼的节点,包含权值和左右子节点等属性。 2. 构建哈夫曼,可以通过优先队列(PriorityQueue)来实现。首先将所有节点加入队列中,然后每次取出权值最小的两个节点,合并成一个新节点,再将新节点加入队列中,直到队列中只剩下一个节点,即为哈夫曼的根节点。 3. 根据哈夫曼生成哈夫曼编码,可以通过递归遍历哈夫曼实现。从根节点开始遍历,每次向左走为0,向右走为1,直到叶子节点,即可得到该节点对应的哈夫曼编码。 下面是一个简单的Java代码实现: ```java public class HuffmanTree { private Node root; private class Node implements Comparable<Node> { int weight; Node left; Node right; public Node(int weight) { this.weight = weight; } public int compareTo(Node other) { return this.weight - other.weight; } public boolean isLeaf() { return left == null && right == null; } public String toString() { return "Node(" + weight + ")"; } } public HuffmanTree(int[] weights) { PriorityQueue<Node> pq = new PriorityQueue<>(); for (int weight : weights) { pq.offer(new Node(weight)); } while (pq.size() > 1) { Node left = pq.poll(); Node right = pq.poll(); Node parent = new Node(left.weight + right.weight); parent.left = left; parent.right = right; pq.offer(parent); } root = pq.poll(); } public Map<Character, String> getCodes() { Map<Character, String> codes = new HashMap<>(); getCode(root, "", codes); return codes; } private void getCode(Node node, String code, Map<Character, String> codes) { if (node.isLeaf()) { codes.put((char) node.weight, code); } else { getCode(node.left, code + "0", codes); getCode(node.right, code + "1", codes); } } } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值