What is two fork tree, no longer introduced here, you can own Baidu: two fork tree. Use Java to implement "Expression binary tree" here.
Definition of expression binary tree
The first step is to understand the expression of the binary tree is what dongdong? Give me a chestnut, expression: (A+BX (c-d))-e/f. Put the number on the leaf node, the operator on the branch node, constitutes a binary tree, because the storage is an expression, called "Expression binary tree."
Children's boots may wonder how this is built. Let's take 45+23*56/2-5 for a while. First, remove the first digit 45 and place it on the leaf node, and then place it on the branch node when you encounter "+".
Then put the "23", "*", "56", "/", "2" in sequence,
Finally put in "-", "5",
This is roughly the case. (These pictures I painted, more ugly, we look at the Good (⊙﹏⊙))
Construction steps of expression binary tree
1. Create node objects;
2. The operator and the data are identified and stored in the corresponding list (queue);
3. Take out the first two digits and an operator to form a new digital node;
4. Repeat the 3rd step until the operator is finished;
5. Make the root node equal to the last node.
Implementation of expression binary tree
first, the Node object class is constructed, including data, left subtree, right subtree, and several sets, get methods.
Package tets0714;
/**
* Node object class
* @author Yuxiu
* *
/Public
class Node {
//data
private String;
Zoozi Tree
private Node lchild;
Right subtree
private Node rchild;
Node () {
}
node (String data) {
this.data = data;
}
Node (String Data, node lchild, node Rchild) {
super ();
This.data = data;
This.lchild = Lchild;
This.rchild = rchild;
}
Public String GetData () {return
data;
}
Public Node Getlchild () {return
lchild;
}
Public Node Getrchild () {return
rchild
}}
Then there is the construction of the expression binary tree.
Package tets0714;
Import java.util.ArrayList;
/** * Expression binary tree class * @author Yuxiu * * */public class Formaluetree {private String s= ""; private Node root; Root node/** * Create expression binary tree * @param str expression */public void Creattree (String str) {//Declare an array list, stored operator, subtraction a
rraylist<string> operlist = new arraylist<string> ();
Declares an array list that holds the node's data arraylist<node> numlist = new arraylist<node> (); First, the analysis of operators and data, stored in the corresponding list for (int i=0;i<str.length (); i++) {Char ch = str.charat (i);
Remove each character of the string if (ch>= ' 0 ' &&ch<= ' 9 ') {s+=ch;
}else{Numlist.add (new Node (s));
S= "";
Operlist.add (ch+ "");
}//Add the final number to the Numlist.add (new Node (s)); while (Operlist.size () >0) {//step three, repeat the second step until the operator is finished//second, take out the first two digits and an operator to form a new digital node left = Numlist.remove (0
);
Node right = numlist.remove (0);
String oper = operlist.remove (0);
Node node = new node (oper,left,right); Numlist.add (0,node);
The new node is the first node, and the previously index=0 node becomes the index=1}//Fourth step, leaving the root node equal to the last node root = numlist.get (0); /** * Output Node data */public void output () {output (root); Start traversal from the root node/** * Output node Data * @param node/public void output (node node) {if (Node.getlchild ()!=null) {
If the leaf node will terminate output (Node.getlchild ()); } System.out.print (Node.getdata ());
The traversal includes the first order traversal (the root or left), the middle sequence traversal (left root right), the sequential traversal (or left root) if (Node.getrchild ()!=null) {output (Node.getrchild ());
} public static void Main (string[] args) {Formaluetree tree = new Formaluetree ();
Tree.creattree ("45+23*56/2-5"); The two-fork Tree tree.output ();//output validation}}
At the end of the console can be output "45+23*56/2-5", OK. In this use of the sequence traversal, the small partners can try to use the first-order traversal, sequential traversal is what effect. As for traversal, we'll talk about it later.
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.