栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java哈夫曼树的生成

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java哈夫曼树的生成

简单测试,写的有点差

package com.dong;

import java.util.ArrayList;

class tree{
//	先序遍历,根左右
	static void pro(Node root) {
		if(root!=null) {
			System.out.print(root.data+",");
			pro(root.left);
			pro(root.right);
		}
	}
//	中序遍历,左根右
	static void mid(Node root) {
		if(root!=null) {
			mid(root.left);
			System.out.print(root.data);
			mid(root.right);
		}
	}
//	后续遍历,左右根
	static void last(Node root) {
		if(root!=null) {
			
			last(root.left);
			last(root.right);
			System.out.print(root.data);
		}
	}
}
class Node {
//	存储的数据类型为Sting类型
	String data;
	Node left=null;
	Node right=null;
	void Node(String data,Node left,Node right) {
		this.data=data;
		this.left=left;
		this.right=right;
		
	}	
}

public class Test {
	public static void main(String[] args) {
//		创捷节点对象
		Node node5 = new Node();
		Node node4 = new Node();
		Node node3 = new Node();
		Node node2 = new Node();
		Node node1 = new Node();
		ArrayList list = new ArrayList();	
		list.add(4);
		list.add(2);
		list.add(6);	
//		-----------生成 24
		list.sort(null);
		list.add(list.get(0)+list.get(1));
		node1.Node(list.get(1).toString(), null, null);	
		node2.Node(list.get(0).toString(), null, null);
	
		list.remove(0);
		list.remove(0);
//		--------------------------------------
		list.sort(null);
		list.add(list.get(0)+list.get(1));
		node3.Node(list.get(0).toString(), node2, node1);
		node4.Node(list.get(1).toString(), null, null);	
//		-------------------------------------
		node5.Node(Integer.toString(list.get(1).intValue()+list.get(0).intValue()), node3, node4);	
		System.out.println("先序:");
		tree.pro(node5);

		

		
	}
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/459834.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号