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

📄 infixpostfixstring.java

📁 国外的数据结构与算法分析用书
💻 JAVA
字号:
import dslib.tree.LinkedSimpleTreeUos;
import dslib.base.*;
import java.io.*;

public class InfixPostfixString
{
	LinkedSimpleTreeUos exprTree;

	public InfixPostfixString() 
	{
		exprTree = new LinkedSimpleTreeUos();
		makeExprTree();
		printExprTree();
	}

	/**	Create the expression tree for 2 * (a + b * c)
		Analysis: Time = (1) */
	public void makeExprTree()
	{
		LinkedSimpleTreeUos t1, t2, t3;
		t1 = new LinkedSimpleTreeUos(null, new Character('b'), null);
		t2 = new LinkedSimpleTreeUos(null, new Character('c'), null);
		t3 = new LinkedSimpleTreeUos(t1, new Character('*'), t2);
		t1 = new LinkedSimpleTreeUos(null, new Character('a'), null);
		t2 = new LinkedSimpleTreeUos(t1, new Character('+'), t3);
		t1 = new LinkedSimpleTreeUos(null, new Character('2'), null);
		exprTree = new LinkedSimpleTreeUos(t1, new Character('*'), t2);
		System.out.println(exprTree);
	}

	public void printExprTree() 
	{
		System.out.println("Infix String " + infixString(exprTree));
		System.out.println("Postfix String " + postfixString(exprTree));
	}

	/**	For the expression tree t, return its expression in standard infix notation. */
	public String infixString(LinkedSimpleTreeUos t)
	{
		if (t.isEmpty())
			return new String();
		else 
		{
			return "(" + infixString((LinkedSimpleTreeUos)t.rootLeftSubtree())
				+ t.rootItem() + infixString((LinkedSimpleTreeUos)t.rootRightSubtree()) + ")"; 
		}
	}

	/**	For the expression tree t, return its expression in standard postfix notation. */
	public String postfixString(LinkedSimpleTreeUos t) 
	{
		if (t.isEmpty())
			return new String();
		else
		{
			return postfixString((LinkedSimpleTreeUos)t.rootLeftSubtree())
				+ postfixString((LinkedSimpleTreeUos)t.rootRightSubtree())
				+ t.rootItem();
		}	
	}

	public static void main(String args[]) throws Exception
	{
		InfixPostfixString c = new InfixPostfixString();
	}
}

⌨️ 快捷键说明

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