⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 binarytree.java

📁 用二叉树实现统计字符出现频率算法 JAVA实现
💻 JAVA
字号:
//二叉树类

class binaryTree 
{
	private Node root;    //根结点

	public binaryTree( )
	{
		root = new Node( 'k');
		root.setCount( 0 );
	}

	public Node getRoot()
	{
		return root;
	}

	public void insert( Node r, char c )    //从根点开始插入,默认根结点存的字符为K
	{
		if( c == r.getChar() ) 
		{
			r.addCount();
			return ;
		}
		else if( c < r.getChar() )
		{
			if( r.getLeft() == null )
			{
				Node node = new Node( c );
				r.setLeft( node );
			}
			else insert( r.getLeft(), c );
		}
		else
		{
			if( r.getRight() == null )
			{
				Node node = new Node( c );
				r.setRight( node );
			}
			else insert( r.getRight(), c );
		}			
	}

	public void print( Node root )  //打印结果,中序遍历
	{
		if( root == null ) return;
		print( root.getLeft() );
		if( root.getCount() == 0 ) 
		{}
		else System.out.println( root.getChar() + "出现的次数为:" + root.getCount() );
		print( root.getRight() );
	}
		

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -