主要内容:二叉树的定义,层次遍历,前、中、后序遍历,树的深度和叶子结点数
1、二叉树的表示:
程序代码:
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
//用一个类来封装二叉树的结点
class TreeNode {
int data;// 存放结点的数据
TreeNode left;// 左孩子的引用
TreeNode right;// 右孩子的引用
public TreeNode(int data, TreeNode left, TreeNode right) {
this.data = data;
this.left = left;
this.right = right;
}
public TreeNode() {
}
}
// 定义二叉树
class JavaTwoTree {
TreeNode root;// 整棵二叉树的根
Scanner sc = new Scanner(System.in);
public JavaTwoTree() {
this.root = null;// 开始的时候根结点为空
}
// 创建二叉树,递归 根,递归左子树,递归右子树 0表示没有后继的点
public TreeNode createBinaryTree() {
TreeNode t;// 当前树的根结点
int x = sc.nextInt();// 读入当前结点的值,如果为0表示null结点
if (x == 0) {
t = null;
} else {
t = new TreeNode();
t.data = x;
t.left = createBinaryTree();
t.right = createBinaryTree();
}
return t;
}
public void printTree(TreeNode t) {
if (t != null) {
System.out.print(t.data);
if (t.left != null || t.right != null) {
System.out.print("(");
printTree(t.left);
if (t.right != null)
System.out.print(",");
printTree(t.right);
System.out.print(")");
}
}
}
// 前序遍历
public void preOrder(TreeNode root) {
if (root != null) {
System.out.print(root.data + " ");
preOrder(root.left);
preOrder(root.right);
}
}
// 中序遍历
public void midOrder(TreeNode root) {
if (root != null) {
midOrder(root.left);
System.out.print(root.data + " ");
midOrder(root.right);
}
}
// 后序遍历
public void postOrder(TreeNode root) {
if (root != null) {
postOrder(root.left);
postOrder(root.right);
System.out.print(root.data + " ");
}
}
// 层次遍历
public void levelOrder(TreeNode root) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
if (root == null)
return;
q.offer(root);// 根入列
while (!q.isEmpty()) {
TreeNode head = q.poll();// 弹出列头
System.out.print(head.data + " ");
if (head.left != null)
q.offer(head.left);
if (head.right != null)
q.offer(head.right);
}
}
// 树的深度
public int treeDepth(TreeNode root) {
if (root == null)
return 0;
return Math.max(treeDepth(root.left), treeDepth(root.right)) + 1;
}
// 叶子结点个数
public int treeLeaf(TreeNode root) {
if (root == null)
return 0;
else if (root.left == null && root.right == null)
return 1;
else
return treeLeaf(root.left) + treeLeaf(root.right);
}
}
public class 二叉树 {
public static void main(String[] args) {
JavaTwoTree jt = new JavaTwoTree();
jt.root = jt.createBinaryTree();
// 输入:1 2 4 0 0 5 0 0 3 6 0 0 7 0 0
System.out.print("二叉树的结构:");
jt.printTree(jt.root);
System.out.println();
System.out.print("前序遍历:");
jt.preOrder(jt.root);
System.out.println();
System.out.print("中序遍历:");
jt.midOrder(jt.root);
System.out.println();
System.out.print("后序遍历:");
jt.postOrder(jt.root);
System.out.println();
System.out.print("层次遍历:");
jt.levelOrder(jt.root);
System.out.println();
System.out.println("树的深度:" + jt.treeDepth(jt.root));
System.out.println("叶子结点个数:" + jt.treeLeaf(jt.root));
}
}
运行结果:
二叉树的结构:1(2(4,5),3(6,7))
前序遍历:1 2 4 5 3 6 7
中序遍历:4 2 5 1 6 3 7
后序遍历:4 5 2 6 7 3 1
层次遍历:1 2 3 4 5 6 7
树的深度:3
叶子结点个数:4
2、根据二叉树的前序和中序 输出后序
前序:dbacegf
中序:abcdefg
后序:acbfged
import java.util.Scanner;
public class HouXu {
public static void main(String[] args) {
String pre,mid;
Scanner sc = new Scanner(System.in);
pre = sc.next();
mid = sc.next();
System.out.println(f(pre,mid));
}
public static String f(String pre,String mid) {
if(pre.length() == 0) return "";
else if(pre.length() == 1) return pre;
else {
int pos = mid.indexOf(pre.charAt(0));//根据前序的根去中序分左右
String left = f(pre.substring(1,pos+1),mid.substring(0,pos));
String right = f(pre.substring(pos+1),mid.substring(pos+1));
return left+right+pre.charAt(0);//前序的第1个字符为根
}
}
}