版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_40301026/article/details/88775956
所谓的前序遍历就是:先遍历根再遍历左子树再遍历右子树,按这个顺序根,左,右遍历直到整个树遍历完。
所谓的中序遍历就是:先遍历左子树再遍根再遍历右子树,按这个顺序左,根,右直到整个树遍历完。
所谓的中序遍历就是:先遍历左子树再遍右子树再遍根,按这个顺序左,右,根直到整个树遍历完。
以此树来详解一下:
1. 我们可以把此树先分为根和左子树,右子树(红色圈出来的),根据根,左,右的顺序遍历(先序遍历)。根结点保存【1】
2.再遍历左子树,把左子树看成一个完整的二叉树,分为根,左子树,右子树(绿色圈出来的),再根据根,左,右的顺序遍历。保存根结点【1,2】。3.同理,再把左子树看为完整的树,分为根,左子树,右子树,根据根,左,右的顺序遍历(先序遍历),保存根结点【1,2,4】。4.再把左子树7看为完整的树(左子树右子树为空),分为根,左子树,右子树,根据根,左,右的顺序遍历(先序遍历),保存根结点【1,2,4,7】。4轮根左右遍历完,第3轮根左遍历完,再遍历右子树,保存根结点【1,2,4,7,5】。同理再一直回溯。直到遍历完。
具体代码:
package cn.liu.BinaryTree;
/**
* 二叉链表的结点
* @author Administrator
*
*/
public class Node {
private Object data;//数据域
private Node leftChild;//左孩子(左子树)
private Node rightChild;//右孩子(右子树)
public Node() {
super();
}
public Node(Object data) {
super();
this.data = data;
}
public Node(Object data, Node leftChild, Node rightChild) {
super();
this.data = data;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
public Node getLeftChild() {
return leftChild;
}
public void setLeftChild(Node leftChild) {
this.leftChild = leftChild;
}
public Node getRightChild() {
return rightChild;
}
public void setRightChild(Node rightChild) {
this.rightChild = rightChild;
}
@Override
public String toString() {
return "Node [data=" + data + ", leftChild=" + leftChild + ", rightChild=" + rightChild + "]";
}
}
package cn.liu.BinaryTree;
/*
* 二叉树的遍历(先序,中序,后序(递归实现))
*/
public class Demo {
private Node root; //树的根结点
public Demo(Node root) {
this.root = root;
}
//先序遍历。 根 左 右
public void preorder() {
System.out.println("先序遍历(递归):");
carbonPreorder(root);
System.out.println();
}
private void carbonPreorder(Node node) {
if(node!=null) {
//访问根结点
System.out.print(node.getData()+" ");
//遍历左子树
carbonPreorder(node.getLeftChild());
//遍历右子树
carbonPreorder(node.getRightChild());
}
}
//中序遍历。 左 根 右
public void inorderTraversal() {
System.out.println("中序遍历(中序):");
carbonIT(root);
System.out.println();
}
private void carbonIT(Node node) {
if(node!=null) {
//遍历左子树
carbonIT(node.getLeftChild());
//输出树结点
System.out.print(node.getData()+" ");
//遍历右子树
carbonIT(node.getRightChild());
}
}
//后序遍历。 左 右 根
public void postorderTraversal() {
System.out.println("后序遍历:");
carbonPT(root);
System.out.println();
}
private void carbonPT(Node node) {
if(node!=null) {
carbonPT(node.getLeftChild());
carbonPT(node.getRightChild());
System.out.print(node.getData()+" ");
}
}
}
package cn.liu.BinaryTree;
public class TestDemo {
public static void main(String[] args) {
//创建一个二叉树
Node node10 = new Node(10, null, null);
Node node9 = new Node(9, null, null);
Node node8 = new Node(8, null, null);
Node node7 = new Node(7, null, null);
Node node6 = new Node(6, node10, null);
Node node5 = new Node(5, node8, node9);
Node node4 = new Node(4, node7, null);
Node node3 = new Node(3, null, node6);
Node node2 = new Node(2, node4, node5);
Node node1 = new Node(1,node2,node3);
Demo tree = new Demo(node1);
//先序遍历:1 2 4 7 5 8 9 3 6 10
tree.preorder();
//中序遍历:7 4 2 8 5 9 1 3 10 6
tree.inorderTraversal();
//后序遍历:7 4 8 9 5 2 10 6 3 1
tree.postorderTraversal();
}
}
扫描二维码关注公众号,回复:
5756075 查看本文章