The following is the model shape of a binary tree to be parsed ：

There are two ways to parse binary tree ： They are recursive traversal and stack traversal

First, create a binary tree
public class Node { private int data; private Node leftNode; private Node
rightNode; public Node(int data, Node leftNode, Node rightNode){ this.data =
data; this.leftNode = leftNode; this.rightNode = rightNode; } public int
getData() { return data; } public void setData(int data) { this.data = data; }
public Node getLeftNode() { return leftNode; } public void setLeftNode(Node
leftNode) { this.leftNode = leftNode; } public Node getRightNode() { return
rightNode; } public void setRightNode(Node rightNode) { this.rightNode =
rightNode; } }
Method 1 ： recursive resolution
public class BinaryTree { /** * @author tsh * The first order, middle order and last order of binary tree */
// Note that it must be established in reverse order , Create a child node first , And then build it up in reverse order , Because non leaf nodes will use the following nodes , // Initialization is in order , If you do not set up in reverse order, you will report an error public Node
init() { Node J = new Node(8, null, null); Node H = new Node(4, null, null);
Node G = new Node(2, null, null); Node F = new Node(7, null, J); Node E = new
Node(5, H, null); Node D = new Node(1, null, G); Node C = new Node(9, F, null);
Node B = new Node(3, D, E); Node A = new Node(6, B, C); return A; // Return to root node }
public void printNode(Node node){ System.out.print(node.getData()); } public
void theFirstTraversal(Node root) { // Preorder traversal printNode(root); if
(root.getLeftNode() != null) { // Using recursion to traverse the left child
theFirstTraversal(root.getLeftNode()); } if (root.getRightNode() != null) {
// Recursive traversal of right child theFirstTraversal(root.getRightNode()); } } public void
theInOrderTraversal(Node root) { // Middle order traversal if (root.getLeftNode() != null) {
theInOrderTraversal(root.getLeftNode()); } printNode(root); if
(root.getRightNode() != null) { theInOrderTraversal(root.getRightNode()); } }
public void thePostOrderTraversal(Node root) { // Postorder traversal if (root.getLeftNode() !=
null) { thePostOrderTraversal(root.getLeftNode()); } if(root.getRightNode() !=
null) { thePostOrderTraversal(root.getRightNode()); } printNode(root); } public
static void main(String[] args) { BinaryTree tree = new BinaryTree(); Node root
= tree.init(); System.out.println(" Preorder traversal "); tree.theFirstTraversal(root);
System.out.println(""); System.out.println(" Middle order traversal ");
tree.theInOrderTraversal(root); System.out.println("");
System.out.println(" Postorder traversal "); tree.thePostOrderTraversal(root);
System.out.println(""); } }
Method 2 ： Stack traversal
public class BinaryTree1 { public Node init() { // Note that it must be established in reverse order , Create a child node first , And then build it up in reverse order ,
// Because non leaf nodes will use the following nodes , Initialization is in order , If you do not set up in reverse order, you will report an error Node J = new Node(8, null, null); Node
H = new Node(4, null, null); Node G = new Node(2, null, null); Node F = new
Node(7, null, J); Node E = new Node(5, H, null); Node D = new Node(1, null, G);
Node C = new Node(9, F, null); Node B = new Node(3, D, E); Node A = new Node(6,
B, C); return A; // Return to root node } public void printNode(Node node){
System.out.print(node.getData()); } public void theFirstTraversal_Stack(Node
root) { // Preorder traversal Stack<Node> stack = new Stack<Node>(); Node node = root; while
(node != null || stack.size() > 0) { // Put all the left children on the stack if (node != null) { // Access before stack pressing
printNode(node); stack.push(node); node = node.getLeftNode(); } else { node =
stack.pop(); node = node.getRightNode(); } } } public void
theInOrderTraversal_Stack(Node root) { // Middle order traversal Stack<Node> stack = new
Stack<Node>(); Node node = root; while (node != null || stack.size() > 0) { if
(node != null) { stack.push(node); // Direct stack pressing node = node.getLeftNode(); } else {
node = stack.pop(); // Out of stack and access printNode(node); node = node.getRightNode(); } } }
public void thePostOrderTraversal_Stack(Node root) { // Postorder traversal Stack<Node> stack =
new Stack<Node>(); Stack<Node> output = new Stack<Node>();// An intermediate stack is constructed to store the results of backward traversal
Node node = root; while (node != null || stack.size() > 0) { if (node != null)
{ output.push(node); stack.push(node); node = node.getRightNode(); } else {
node = stack.pop(); node = node.getLeftNode(); } }
System.out.println(output.size()); while (output.size() > 0) {
printNode(output.pop()); } } public static void main(String[] args) {
BinaryTree1 tree = new BinaryTree1(); Node root = tree.init();
System.out.println(" Preorder traversal "); tree.theFirstTraversal_Stack(root);
System.out.println(""); System.out.println(" Middle order traversal ");
tree.theInOrderTraversal_Stack(root); System.out.println("");
System.out.println(" Postorder traversal "); tree.thePostOrderTraversal_Stack(root);
System.out.println(""); } }
summary ： Binary tree is the basis of all kinds of data , For example, binary search tree ,AVL tree , Red and black trees and so on , theoretically , The above two traversal methods can also traverse other derived binary trees , Here is for your reference only .

Technology
Daily Recommendation
views 4