📄 node.java
字号:
//
//
// Group 5
//
import java.lang.*;
import java.util.*;
import java.io.*;
public class Node {
static final int NUMBER = 0, OPERATOR = 1; // values for kind.
int kind; // type of node
double number; // The value in a node
char op; // The operator in a node
Node left; // left node
Node right; // right node
static int lx = 1, rx = 1, ly = 0, ry = 0, xor = 512, yor = 50, xtx = 508, ytx = 56, a = 0;
Node(double val) {
kind = NUMBER;
number = val;
}
Node(char val) {
kind = OPERATOR;
this.op = val;
}
Node(char op, Node right, Node left) {
kind = OPERATOR;
this.op = op;
this.left = left;
this.right = right;
}
public static void preoder(Node node)throws IOException{
if ( node.kind == NUMBER) {
System.out.print(node.number + " ");
//noderTest(node);
}else{
System.out.print(node.op + " ");
//noderTest(node);
}
if(node.left!=null)
preoder(node.left);
if(node.right!=null)
preoder(node.right);
}
public static void noderTest(Node node)throws IOException{
FileWriter svg2 = new FileWriter("tree.svg",true);
BufferedWriter out2 = new BufferedWriter(svg2);
if ( node.kind == NUMBER) {
out2.write("<circle cx=\""+xor+"\" cy=\""+yor+"\" r=\"20\" fill=\"red\" stroke=\"black\" stroke-width=\"a\"/> <text x=\""+xtx+"\" y=\""+ytx+"\" stroke=\"black\" font-size=\"14\" font-family=\"Verdana\">"+node.number+"</text>");
}
else {
out2.write("<circle cx=\""+xor+"\" cy=\""+yor+"\" r=\"20\" fill=\"green\" stroke=\"black\" stroke-width=\"a\"/> <text x=\""+xtx+"\" y=\""+ytx+"\" stroke=\"black\" font-size=\"14\" font-family=\"Verdana\">"+node.op+"</text>");
}
if (node.left != null) {
lx++;ly++;
a = 0;
out2.write("<line x1 = \""+(xor-13)+"\" y1 = \""+(yor+13)+"\" x2 = \""+((xor - 300/lx)+13)+"\" y2 = \""+((yor + ly*50)-13)+"\" stroke = \"black\" stroke-width = \"3\"/>");
xor = xor - 300/lx;
yor = yor + ly*50;
xtx = xtx - 300/lx;
ytx = ytx + ly*50;
noderTest(node.left);
}
if (node.right != null) {
if (a == 0)
{
xor = xor + 2*(300/lx);
xtx = xtx + 2*(300/lx);
out2.write("<line x1 = \""+(xor-13)+"\" y1 = \""+(yor-13)+"\" x2 = \""+((xor - 300/lx)+13)+"\" y2 = \""+((yor - ly*50)+13)+"\" stroke = \"black\" stroke-width = \"3\"/>");
a++;
}
else{
lx--;
if (lx == 2)
{
xor = 512 + 300/lx;
xtx = 512 + 300/lx;
yor = yor - ly*50;
ytx = ytx - ly*50;
out2.write("<line x1 = \""+(xor-13)+"\" y1 = \""+(yor-13)+"\" x2 = \""+((xor - 300/lx)+15)+"\" y2 = \""+((yor - ly*50)+63)+"\" stroke = \"black\" stroke-width = \"3\"/>");
}
else{
xor = xor + 300/lx;
yor = yor - ly*50;
xtx = xtx + 300/lx;
ytx = ytx - ly*50;
out2.write("<line x1 = \""+(xor-13)+"\" y1 = \""+(yor-13)+"\" x2 = \""+((xor - 300/lx)+40)+"\" y2 = \""+((yor - ly*50)+60)+"\" stroke = \"black\" stroke-width = \"3\"/>");
}
ly--;
}
noderTest(node.right);
}
out2.close();
}
// Return the value of the expression
static double getValue( Node node ) throws IOException {
if ( node.kind == NUMBER) {
return node.number;
}
else {
double leftVal = getValue( node.left );
double rightVal = getValue( node.right );
switch(node.op) {
case '+': return leftVal + rightVal;
case '-': return leftVal - rightVal;
case '*': return leftVal * rightVal;
case '/': return leftVal / rightVal;
default: return Double.NaN; // return Not a Number
}
}
}
public static void main (String[] args) throws IOException {
Node t5 = new Node(4);
Node t6 = new Node(9);
Node t7 = new Node(5);
Node t8 = new Node(3);
Node t9 = new Node(4);
Node t4 = new Node('*', t9, t8);
Node t2 = new Node('+', t5, t4);
Node t3 = new Node('-', t7, t6);
Node t1 = new Node('/', t3, t2);
FileWriter svg = new FileWriter("tree.svg",true);
BufferedWriter out = new BufferedWriter(svg);
out.write("<?xml version=\"1.0\" standalone=\"no\"?>");
out.write("<svg width=\"100%\" height=\"100%\" version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\">");
out.close();
noderTest(t1);
preoder(t1);
FileWriter svg3 = new FileWriter("tree.svg",true);
BufferedWriter out3 = new BufferedWriter(svg3);
out3.write("</svg>");
out3.close();
double value = getValue(t1); // get Value
System.out.println("Result = " + value); // show value
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -