二叉树的创建,前序、中序、后序遍历。看了网上的写法后,我之前用c 语言写过链表、栈和队列,受到指针的启发,在java中我用引用改写了下二叉树的遍历过程。

代码如下:

  1 import java.util.Objects;
  2 
  3 public class Main {
  4     public static void main(String[] args) {
  5         //创建节点
  6         Node rootNode = new Node();
  7         rootNode.setData("唐僧");
  8         rootNode.setNo(1);
  9         Node node1 = new Node();
 10         node1.setData("孙悟空");
 11         node1.setNo(2);
 12         Node node2 = new Node();
 13         node2.setData("猪八戒");
 14         node2.setNo(3);
 15         Node node3 = new Node();
 16         node3.setData("沙和尚");
 17         node3.setNo(4);
 18         Node node4 = new Node();
 19         node4.setNo(5);
 20         node4.setData("白龙马");
 21         //挂数据
 22         rootNode.setLeftNode(node1);
 23         rootNode.setRightNode(node2);
 24         node2.setRightNode(node3);
 25         node2.setLeftNode(node4);
 26         //打印输出
 27         BinaryTree tree = new BinaryTree();
 28         System.out.println("前序遍历");
 29         tree.preOrder(rootNode);
 30         System.out.println("中序遍历");
 31         tree.middleOrder(rootNode);
 32         System.out.println("后序遍历");
 33         tree.backOrder(rootNode);
 34     }
 35 
 36 }
 37 class BinaryTree {
 38 
 39     //前序遍历 父的先后
 40     public void preOrder(Node node) {
 41         System.out.println(node.toString());
 42         if(Objects.nonNull(node.getLeftNode())) {
 43             preOrder(node.getLeftNode());
 44         }
 45         if(Objects.nonNull(node.getRightNode())) {
 46             preOrder(node.getRightNode());
 47         }
 48     }
 49     public void middleOrder(Node node) {
 50         if(Objects.nonNull(node.getLeftNode())) {
 51             middleOrder(node.getLeftNode());
 52         }
 53         System.out.println(node.toString());
 54         if(Objects.nonNull(node.getRightNode())) {
 55             middleOrder(node.getRightNode());
 56         }
 57     }
 58     public void backOrder(Node node) {
 59 
 60         if(Objects.nonNull(node.getLeftNode())) {
 61             backOrder(node.getLeftNode());
 62         }
 63         if(Objects.nonNull(node.getRightNode())) {
 64             backOrder(node.getRightNode());
 65         }
 66         System.out.println(node.toString());
 67     }
 68 }
 69 
 70 //定义节点结构
 71 class Node {
 72     private Node leftNode;
 73     private Node rightNode;
 74     private String data;
 75     private int no;
 76 
 77     public int getNo() {
 78         return no;
 79     }
 80 
 81     public void setNo(int no) {
 82         this.no = no;
 83     }
 84 
 85 
 86 
 87 
 88     public Node getLeftNode() {
 89         return leftNode;
 90     }
 91 
 92     public void setLeftNode(Node leftNode) {
 93         this.leftNode = leftNode;
 94     }
 95 
 96     public Node getRightNode() {
 97         return rightNode;
 98     }
 99 
100     public void setRightNode(Node rightNode) {
101         this.rightNode = rightNode;
102     }
103 
104     public String getData() {
105         return data;
106     }
107 
108     public void setData(String data) {
109         this.data = data;
110     }
111 
112     @Override
113     public String toString() {
114         return "Node{" +
115                 "data='" + data + '\'' +
116                 ", no=" + no +
117                 '}';
118     }
119 }