mirror of
https://github.com/Rushilwiz/APCS.git
synced 2025-04-04 20:40:20 -04:00
76 lines
1.8 KiB
Java
76 lines
1.8 KiB
Java
import java.util.*;
|
|
|
|
/* Driver for the BXT class.
|
|
* Input: a postfix string with space delimited tokens.
|
|
*/
|
|
public class BXT_Driver
|
|
{
|
|
public static void main(String[] args)
|
|
{
|
|
ArrayList<String> postExp = new ArrayList<String>();
|
|
postExp.add("2 3 + 5 / 4 5 - *");
|
|
|
|
for( String postfix : postExp )
|
|
{
|
|
System.out.println("Postfix Exp: " + postfix);
|
|
BXT tree = new BXT();
|
|
tree.buildTree( postfix );
|
|
System.out.println("BXT: ");
|
|
System.out.println( tree.display() );
|
|
System.out.print("Infix order: ");
|
|
System.out.println( tree.inorderTraverse() );
|
|
System.out.print("Prefix order: ");
|
|
System.out.println( tree.preorderTraverse() );
|
|
System.out.print("Evaluates to " + tree.evaluateTree());
|
|
System.out.println( "\n------------------------");
|
|
}
|
|
|
|
/* extension: prints parentheses */
|
|
BXT tree2 = new BXT();
|
|
//tree2.buildTree("3 5 + 4 *"); // "( 3 + 5 ) * 4"
|
|
System.out.println(tree2.inorderTraverseWithParentheses());
|
|
}
|
|
}
|
|
|
|
/***************************************
|
|
|
|
Postfix Exp: 14 -5 /
|
|
-5
|
|
/
|
|
14
|
|
Infix order: 14 / -5
|
|
Prefix order: / 14 -5
|
|
Evaluates to -2.8
|
|
------------------------
|
|
Postfix Exp: 20.0 3.0 -4.0 + *
|
|
-4.0
|
|
+
|
|
3.0
|
|
*
|
|
20.0
|
|
Infix order: 20.0 * 3.0 + -4.0
|
|
Prefix order: * 20.0 + 3.0 -4.0
|
|
Evaluates to -20.0
|
|
------------------------
|
|
Postfix Exp: 2 3 + 5 / 4 5 - *
|
|
5
|
|
-
|
|
4
|
|
*
|
|
5
|
|
/
|
|
3
|
|
+
|
|
2
|
|
Infix order: 2 + 3 / 5 * 4 - 5
|
|
Prefix order: * / + 2 3 5 - 4 5
|
|
Evaluates to -1.0
|
|
------------------------
|
|
Postfix Exp: 5.6
|
|
5.6
|
|
Infix order: 5.6
|
|
Prefix order: 5.6
|
|
Evaluates to 5.6
|
|
------------------------
|
|
|
|
*******************************************/ |