binarytree.java.bak

来自「用二叉树实现统计字符出现频率算法 JAVA实现」· BAK 代码 · 共 55 行

BAK
55
字号
//二叉树类

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 ) System.out.println( "开始打印" );
		else System.out.println( root.getChar() + "出现的次数为:" + root.getCount() );
		print( root.getRight() );
	}
		

}

⌨️ 快捷键说明

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