遍历二叉树的过程实质上是把二叉树的节点进行线性排列的过程。
二叉树的特点:
- 二叉树的第i层至多有2^(i-1)个节点,其中i>=1;
- 深度为n的二叉树至多有2^n-1个节点,至少有n个节点,其中n>=1;
- 对于任意一棵二叉树而言,其叶子节点数目为N0,度为2的节点数目为N2,则满足以下关系:N0=N2+1;
- 具有n个节点的完全二叉树的深度:[logn]+1
完全二叉树:只有最下面的两层节点度小于2,并且最下面一层的节点都集中在该层最左边的若干位置的二叉树。
满二叉树:除了叶节点外,每一个节点都有左右子叶且叶节点都处在最底层的二叉树。满二叉树是一颗特殊的完全二叉树。
二叉搜索树(查找树):
【1】若任意节点的左子树不为空,则左子树上所有节点的值均小于它的根节点的值;
【2】若任意节点的右子树不为空,则右子树上所有节点的值均大于它的根节点的值;
【3】任意节点的左、右子树也分别为二叉搜索树。
二叉树的遍历主要包括以下三种:
【1】前序遍历:按照“根左右”的原则,先遍历根节点,再遍历左子树,后遍历右子树;
【2】中序遍历:按照“左根右”的原则,先遍历左子树,再遍历根节点,后遍历右子树;
【3】后序遍历:按照“左右根”的原则,先遍历左子树,再遍历右子树,后遍历根节点。
Java实现:
import java.util.Stack;
public class BinaryTree {
private Node root;
public void setRoot(Node root) {
this.root = root;
}
public Node getRoot(){
return root;
}
// 创建一棵二叉树
// 5
// / \
// 3 7
// / \ / \
// 2 4 6 8
public Node initial(){
Node D = new Node(2);
Node E = new Node(4);
Node F = new Node(6);
Node G = new Node(8);
Node B = new Node(3, D, E);
Node C = new Node(7, F, G);
Node A = new Node(5, B, C);
return A; //root
}
//递归实现【前序遍历】“根左右”
public void preOrder(Node node){
if (node == null) {
return;
}
System.out.print(node.getN() + " ");
preOrder(node.getLeftChild());
preOrder(node.getRightChild());
}
//递归实现【中序遍历】“左根右”
public void inOrder(Node node){
if (node == null) {
return;
}
inOrder(node.getLeftChild());
System.out.print(node.getN() + " ");
inOrder(node.getRightChild());
}
//递归实现【后序遍历】“左右根”
public void postOrder(Node node){
if (node == null) {
return;
}
postOrder(node.getLeftChild());
postOrder(node.getRightChild());
System.out.print(node.getN() + " ");
}
//非递归实现【前序遍历】“根左右”
public void iterativePreOrder(Node node) {
Stack<Node> stack = new Stack<>();
while (node != null || stack.size() > 0) {
//压入所有的左子节点,压入前先访问它
while (node != null){
System.out.print(node.getN() + " ");
stack.push(node);
node = node.getLeftChild();
}
if (stack.size() > 0) {
node = stack.pop();
node = node.getRightChild();
}
}
}
//非递归实现【中序遍历】“左根右”
public void iterativeInOrder(Node node){
Stack<Node> stack = new Stack<>();
while (node != null || stack.size() > 0){
//压入所有的左子节点,暂时先不访问
while (node != null) {
stack.push(node);
node = node.getLeftChild();
}
if (stack.size() > 0) {
node = stack.pop();
System.out.print(node.getN() + " ");
node = node.getRightChild();
}
}
}
//非递归实现【后序遍历】双栈法 “左右根”
public void iterativePostOrder(Node node){
Stack<Node> stack = new Stack<>();
Stack<Node> temp = new Stack<>();
while (node != null || stack.size() > 0) {
//压入所有的右子节点,暂时先不访问
while (node != null) {
temp.push(node);
stack.push(node);
node = node.getRightChild();
}
if (stack.size() > 0) {
node = stack.pop();
node = node.getLeftChild();
}
}
while (temp.size() > 0) {
node = temp.pop();
System.out.print(node.getN() + " ");
}
}
public int getTreeDepth(Node node){
if (node == null) {
return 0;
}
int left = getTreeDepth(node.getLeftChild());
int right = getTreeDepth(node.getRightChild());
return (left < right) ? (right + 1) : (left + 1);
}
public static void main(String[] args) {
BinaryTree bt = new BinaryTree();
bt.setRoot(bt.initial());
System.out.println("preOrder traversal of binary tree: ");
bt.preOrder(bt.getRoot());
System.out.println();
bt.iterativePreOrder(bt.getRoot());
System.out.println();
System.out.println("inOrder traversal of binary tree: ");
bt.inOrder(bt.getRoot());
System.out.println();
bt.iterativeInOrder(bt.getRoot());
System.out.println();
System.out.println("postOrder traversal of binary tree: ");
bt.postOrder(bt.getRoot());
System.out.println();
bt.iterativePostOrder(bt.getRoot());
System.out.println();
System.out.print("the depth of binary tree: ");
System.out.println(bt.getTreeDepth(bt.getRoot()));
}
}
Node类:
public class Node{
private int n;
private Node leftChild;
private Node rightChild;
public Node(int n){
this(n, null, null);
}
public Node(int n, Node leftChild, Node rightChild){
this.n = n;
this.leftChild = leftChild;
this.rightChild = rightChild;
}
public int getN() {
return n;
}
public Node getLeftChild() {
return leftChild;
}
public Node getRightChild() {
return rightChild;
}
}