算法步骤:(使用队列)
<1>,将根结点指针入队。
<2>,将根结点指针出队,如果出队结点的左右孩子存在的话,
将出队结点的左右孩子入队。并输出出队结点的数据。
<3>,判断队列是否为空,如果为空,算法结束,否则转至<2>.
package TreeAlgorithms;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
/**
* @author MohnSnow
* @time 2015年6月18日 下午1:59:12
* @title 分层便利二叉树
*
*/
public class HierarchicalTraversal {
/**
* @param argsmengdx
* -fnst
*/
public static class TreeNode {
int value;
TreeNode leftChild;
TreeNode rightChild;
public TreeNode(int Tvalue, TreeNode TleftChild, TreeNode TrightChild) {
value = Tvalue;
leftChild = TleftChild;
rightChild = TrightChild;
}
}
public static void tHierarchicalTraversal(TreeNode tTree) {
Queue<TreeNode> aQueue = new LinkedList<TreeNode>();
if (tTree != null) {
aQueue.add(tTree);
printHierarchicalTraversal(aQueue);
}
}
public static void printHierarchicalTraversal(Queue<TreeNode> aQueue) {
TreeNode sonTree = aQueue.poll();
if (sonTree != null) {
System.out.println("打印数字: " + sonTree.value);
if (sonTree.leftChild != null) {
aQueue.add(sonTree.leftChild);
}
if (sonTree.rightChild != null) {
aQueue.add(sonTree.rightChild);
}
printHierarchicalTraversal(aQueue);
} else {
return;
}
}
public static void main(String[] args) {
TreeNode e = new TreeNode(5, null, null);
TreeNode d = new TreeNode(4, null, null);
TreeNode c = new TreeNode(3, null, null);
TreeNode b = new TreeNode(2, d, e);
TreeNode a = new TreeNode(1, b, c);
tHierarchicalTraversal(a);
}
}
2,先序遍历二叉树。
算法步骤:(递归)
<1>,如果结点指针不为空,则转至<2>,否则算法结束。
<2>,访问结点(数据).
<3>,访问结点的左孩子。
<4>,访问结点的右孩子。
package TreeAlgorithms;
import java.util.LinkedList;
import java.util.Queue;
/**
* @author MohnSnow
* @time 2015年6月18日 下午2:28:46
* @title先序遍历
*
*/
public class PreorderTraversal {
/**
* @param argsmengdx
* -fnst
*/
public static class TreeNode {
int value;
TreeNode leftChild;
TreeNode rightChild;
public TreeNode(int Tvalue, TreeNode TleftChild, TreeNode TrightChild) {
value = Tvalue;
leftChild = TleftChild;
rightChild = TrightChild;
}
}
public static void tPreorderTraversal(TreeNode tTree) {
if (tTree != null) {
printPreorderTraversal(tTree);
}
}
public static void printPreorderTraversal(TreeNode tTree) {
if (tTree != null) {
System.out.println("打印数字: " + tTree.value);
}
if (tTree.leftChild != null) {
printPreorderTraversal(tTree.leftChild);
}
if (tTree.rightChild != null) {
printPreorderTraversal(tTree.rightChild);
}
}
public static void main(String[] args) {
TreeNode f = new TreeNode(6, null, null);
TreeNode e = new TreeNode(5, null, f);
TreeNode d = new TreeNode(4, null, null);
TreeNode c = new TreeNode(3, null, null);
TreeNode b = new TreeNode(2, d, e);
TreeNode a = new TreeNode(1, b, c);
tPreorderTraversal(a);
}
}
void pre_order(btree *root)
{
if (root != NULL) {
visit(root);
pre_order(root->lchild);
pre_order(root->rchild);
}
}
3,中序遍历二叉树。(很容易理解)
package TreeAlgorithms;
import TreeAlgorithms.PreorderTraversal.TreeNode;
/**
* @author MohnSnow
* @time 2015年6月18日 下午2:41:24
* @title 中序遍历
*/
public class InorderTraversal {
/**
* @param argsmengdx
* -fnst
*/
public static class TreeNode {
int value;
TreeNode leftChild;
TreeNode rightChild;
public TreeNode(int Tvalue, TreeNode TleftChild, TreeNode TrightChild) {
value = Tvalue;
leftChild = TleftChild;
rightChild = TrightChild;
}
}
public static void tInorderTraversal(TreeNode tTree) {
if (tTree != null) {
printInorderTraversal(tTree);
}
}
public static void printInorderTraversal(TreeNode tTree) {
if (tTree != null) {
if (tTree.leftChild != null) {
printInorderTraversal(tTree.leftChild);
}
System.out.println("打印数字: " + tTree.value);
if (tTree.rightChild != null) {
printInorderTraversal(tTree.rightChild);
}
}
}
public static void main(String[] args) {
TreeNode f = new TreeNode(6, null, null);
TreeNode e = new TreeNode(5, null, f);
TreeNode d = new TreeNode(4, null, null);
TreeNode c = new TreeNode(3, null, null);
TreeNode b = new TreeNode(2, d, e);
TreeNode a = new TreeNode(1, b, c);
tInorderTraversal(a);
}
}
void in_order(bstree *root) { if (root != NULL) { in_order(root->lchild); printf("%d ",root->key); in_order(root->rchild); } }
4,后序遍历二叉树。(很容易理解)
public static void printSubsequentTraversal(TreeNode tTree) {
if (tTree != null) {
if (tTree.leftChild != null) {
printSubsequentTraversal(tTree.leftChild);
}
if (tTree.rightChild != null) {
printSubsequentTraversal(tTree.rightChild);
}
System.out.println("后续遍历: " + tTree.value);
}
}
void post_order(bstree *root) { if (root != NULL) { post_order(root->lchild); post_order(root->rchild); printf("%d ",root->key); } }
5,统计树中叶子结点的个数。(或输出叶子结点)
算法步骤:
<1>,先序遍历二叉树。(别的遍历方式也可以)
<2>,判断每经过的结点,如果左右孩子均为空则表示该结点为叶子结点。
<3>,如果是叶子结点则将count++。
注意:这问题就是二叉树遍历的一个应用。
在算法编写过程中,要用局部静态变量,只初始化一次。
public static int sum = 0;
public static int leafSum(TreeNode tTree) {
if (tTree != null) {
if (tTree.leftChild == null && tTree.rightChild == null) {
sum++;
}
if (tTree.leftChild != null) {
leafSum(tTree.leftChild);
}
if (tTree.rightChild != null) {
leafSum(tTree.rightChild);
}
}
return sum;
}
int leaf(btree *root) { static int leaf_count = 0; if (root != NULL) { if (root->lchild == NULL && root->rchild == NULL) leaf_count++; leaf(root->lchild); leaf(root->rchild); } return leaf_count; }
6,求二叉树的高度。(递归)
算法思想:
<1>,二叉树的高度为左右子树高度的最大值加1。
<2>,从下往上统计的思想。
public static int treeDepth(TreeNode tTree) {
if (tTree != null) {
if (tTree.leftChild == null && tTree.rightChild == null) {
return 1;
}
return Math.max(1 + treeDepth(tTree.leftChild), 1 + treeDepth(tTree.rightChild));
}
return sum;
}
int tree_depth(btree *root) { int hl,hr; if (root == NULL) return 0; else { hl = tree_depth(root->lchild); hr = tree_depth(root->rchild); return (MAX(hl,hr) + 1); } }a