用二叉树实现表达式求值Java实现的问题?
我想用二叉树实现表达式求值
下面是节点Node类
protected char key;
protected Node left;
protected Node right;
public Node(){
key = '0';
left = right = null;
}
public Node(char ch){
key = ch;
left = null;
right = null;
}
public Node(char ch, Node left, Node right){
key = ch;
this.left = left;
this.right = right;
}
}
下面是Tree类
private Node node = new Node();
public Tree(){
node.key = 0;
node.left = null;
node.right = null;
}
public static boolean isPlus(char ch){
if(ch=='+' || ch=='-')
return true;
return false;
}
public static boolean isMultiply(char ch){
if(ch=='*' || ch=='/')
return true;
return false;
}
public Node creatTree(String str, int i, int j){
int k,pos = 0;
int flag = 0;
if(i == j){
node.key = str.charAt(i);
node.left = null;
node.right = null;
}
for(k=i; k<=j; k++)
if(isPlus(str.charAt(k))){
++ flag;
pos = k;
}
if(flag == 0){
for(k=i; k<=j; k++)
if(isMultiply(str.charAt(k))){
++ flag;
pos = k;
}
}
if(flag != 0){
node.key = str.charAt(pos);
node.left = creatTree(str, i, pos-1);
node.right = creatTree(str, pos+1, j);
return node;
}
else
return null;
}
double calculate(Node node){
double n1,n2;
if(node == null){
return 0;
}
System.out.println(node.left == null);
System.out.println(node.right == null);
if(node.left == null && node.right == null){
return (node.key-'0');
}
n1 = calculate(node.left);
n2 = calculate(node.right);
switch(node.key){
case '+':
return n1+n2;
case '-':
return n1-n2;
case '*':
return n1*n2;
case '/':
return n1/n2;
}
return 0;
}
}
下面是测试类:
public class Test {
public static void main(String []args){
Tree tree = new Tree();
String str = new String("1");
Node node = new Node(tree.creatTree(str, 0, str.length()-1).key,
tree.creatTree(str, 0, str.length()-1).left,tree.creatTree(str, 0, str.length()-1).right);
System.out.println(node.left == null);
System.out.println(tree.calculate(node));
}
}
为什么总是报Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:6)的错误啊,,求改正,,,在线等,,,,,
下面是节点Node类
public class Node{protected char key;
protected Node left;
protected Node right;
public Node(){
key = '0';
left = right = null;
}
public Node(char ch){
key = ch;
left = null;
right = null;
}
public Node(char ch, Node left, Node right){
key = ch;
this.left = left;
this.right = right;
}
}
下面是Tree类
public class Tree {private Node node = new Node();
public Tree(){
node.key = 0;
node.left = null;
node.right = null;
}
public static boolean isPlus(char ch){
if(ch=='+' || ch=='-')
return true;
return false;
}
public static boolean isMultiply(char ch){
if(ch=='*' || ch=='/')
return true;
return false;
}
public Node creatTree(String str, int i, int j){
int k,pos = 0;
int flag = 0;
if(i == j){
node.key = str.charAt(i);
node.left = null;
node.right = null;
}
for(k=i; k<=j; k++)
if(isPlus(str.charAt(k))){
++ flag;
pos = k;
}
if(flag == 0){
for(k=i; k<=j; k++)
if(isMultiply(str.charAt(k))){
++ flag;
pos = k;
}
}
if(flag != 0){
node.key = str.charAt(pos);
node.left = creatTree(str, i, pos-1);
node.right = creatTree(str, pos+1, j);
return node;
}
else
return null;
}
double calculate(Node node){
double n1,n2;
if(node == null){
return 0;
}
System.out.println(node.left == null);
System.out.println(node.right == null);
if(node.left == null && node.right == null){
return (node.key-'0');
}
n1 = calculate(node.left);
n2 = calculate(node.right);
switch(node.key){
case '+':
return n1+n2;
case '-':
return n1-n2;
case '*':
return n1*n2;
case '/':
return n1/n2;
}
return 0;
}
}
下面是测试类:
public class Test {
public static void main(String []args){
Tree tree = new Tree();
String str = new String("1");
Node node = new Node(tree.creatTree(str, 0, str.length()-1).key,
tree.creatTree(str, 0, str.length()-1).left,tree.creatTree(str, 0, str.length()-1).right);
System.out.println(node.left == null);
System.out.println(tree.calculate(node));
}
}
为什么总是报Exception in thread "main" java.lang.NullPointerException
at Test.main(Test.java:6)的错误啊,,求改正,,,在线等,,,,,
...全文
相关推荐
JAVA数据结构和算法+面试题
**栈**:后进先出(LIFO)的数据结构,常用于函数调用和表达式求值。4. **队列**:先进先出(FIFO)的数据结构,适用于任务调度和消息传递。5.
Java实现表达式二叉树
【Java实现表达式二叉树】是编程领域中一种数据结构的应用,主要用于处理数学表达式。在计算机科学中,二叉树是一种特殊的树形数据结构,每个节点最多有两个子节点,通常分为左子节点和右子节点。
综合实验三基于二叉树的表达式求值算法.pdf
编程语言可以是C、C++、Java等。七、基于二叉树的表达式求值算法的应用基于二叉树的表达式求值算法有广泛的应用领域,如编译器、解释器、计算器等。
二叉树表示的算术表达式
对于求值,中序遍历特别有用,因为它按照运算符优先级的自然顺序处理节点,从而实现正确的计算。源程序文件很可能是用C++、Java或Python等编程语言编写的,它实现了上述的概念和算法。
表达式求值
**实现**:使用某种编程语言(如C++、Java或Python)实现表达式求值器,展示理论知识在实际中的应用。


Java SE
