package com.fcscs.share.test;
// 出处 http://blog.csdn.net/lhfight/article/details/7788291
import java.util.ArrayDeque;
import java.util.Queue;
public class TestTree {
public static void main(String[] args) {
// 测试Treenode
Treenode node = buildTree(null, 1);
System.out.print("先序遍历:");
preOrder(node);
System.out.println();
System.out.print("中序遍历:");
inOrder(node);
System.out.println();
System.out.print("后序遍历:");
postOrder(node);
System.out.println();
System.out.print("层次遍历:");
levelOrder(node);
System.out.println();
System.out.print("叶子节点数目:");
System.out.println(leafNum(node));
System.out.print("二叉树的深度:");
System.out.println(deep(node));
}
// 建立树@k用来控制树的深度
static char i = 'a';// 节点
public static Treenode buildTree(Treenode node, int k) {
if (k > 4)
return null;
if (node == null) {
node = new Treenode(" " + i++);
}
node.lChild = buildTree(node.lChild, k + 1);
node.rChild = buildTree(node.rChild, k + 1);
return node;
}
// 根据先序和中序来构建二叉树
public Treenode buildTree(String pre, String in) {
if (pre.length() <= 0)
return null;
Treenode root = new Treenode(pre.charAt(0) + "");
// 以根节点为中心,将中序分为两个子序列
String leftin = "";
String rightin = "";
// 根所左中序的长度,将先序分为左右两个子先序
String leftpre = "";
String rightpre = "";
root.lChild = buildTree(leftpre, leftin);
root.rChild = buildTree(rightpre, rightin);
return root;
}
// 先序遍历
public static void preOrder(Treenode node) {
if (node != null) {
System.out.print(node.data);
preOrder(node.lChild);
preOrder(node.rChild);
}
}
// 中序遍历
public static void inOrder(Treenode node) {
if (node != null) {
inOrder(node.lChild);
System.out.print(node.data);
inOrder(node.rChild);
}
}
// 后序遍历
public static void postOrder(Treenode node) {
if (node != null) {
postOrder(node.lChild);
postOrder(node.rChild);
System.out.print(node.data);
}
}
// 层次遍历
public static void levelOrder(Treenode node) {
if (node == null)
return;
Queue<Treenode> queue = new ArrayDeque<Treenode>();
queue.add(node);
while (!queue.isEmpty()) {
Treenode temp = queue.poll();
System.out.print(temp.data);
if (temp.lChild != null)
queue.add(temp.lChild);
if (temp.rChild != null)
queue.add(temp.rChild);
}
}
// 求树的叶子节点数
public static int leafNum(Treenode node) {
if (node != null) {
if (node.lChild == null && node.rChild == null) {
return 1;
}
return leafNum(node.lChild)+leafNum(node.rChild);
}
return 0;
}
//求二叉树的深度
public static int deep(Treenode node){
if(node == null)return 0;
if(node.lChild==null&&node.rChild==null)return 1;
return leafNum(node.lChild)+leafNum(node.rChild);
}
}
class Treenode {
public String data;
public Treenode lChild;
public Treenode rChild;
// 构造函数
public Treenode(String data) {
this.data = data;
this.lChild = null;
this.rChild = null;
}
}
// 出处 http://blog.csdn.net/lhfight/article/details/7788291
import java.util.ArrayDeque;
import java.util.Queue;
public class TestTree {
public static void main(String[] args) {
// 测试Treenode
Treenode node = buildTree(null, 1);
System.out.print("先序遍历:");
preOrder(node);
System.out.println();
System.out.print("中序遍历:");
inOrder(node);
System.out.println();
System.out.print("后序遍历:");
postOrder(node);
System.out.println();
System.out.print("层次遍历:");
levelOrder(node);
System.out.println();
System.out.print("叶子节点数目:");
System.out.println(leafNum(node));
System.out.print("二叉树的深度:");
System.out.println(deep(node));
}
// 建立树@k用来控制树的深度
static char i = 'a';// 节点
public static Treenode buildTree(Treenode node, int k) {
if (k > 4)
return null;
if (node == null) {
node = new Treenode(" " + i++);
}
node.lChild = buildTree(node.lChild, k + 1);
node.rChild = buildTree(node.rChild, k + 1);
return node;
}
// 根据先序和中序来构建二叉树
public Treenode buildTree(String pre, String in) {
if (pre.length() <= 0)
return null;
Treenode root = new Treenode(pre.charAt(0) + "");
// 以根节点为中心,将中序分为两个子序列
String leftin = "";
String rightin = "";
// 根所左中序的长度,将先序分为左右两个子先序
String leftpre = "";
String rightpre = "";
root.lChild = buildTree(leftpre, leftin);
root.rChild = buildTree(rightpre, rightin);
return root;
}
// 先序遍历
public static void preOrder(Treenode node) {
if (node != null) {
System.out.print(node.data);
preOrder(node.lChild);
preOrder(node.rChild);
}
}
// 中序遍历
public static void inOrder(Treenode node) {
if (node != null) {
inOrder(node.lChild);
System.out.print(node.data);
inOrder(node.rChild);
}
}
// 后序遍历
public static void postOrder(Treenode node) {
if (node != null) {
postOrder(node.lChild);
postOrder(node.rChild);
System.out.print(node.data);
}
}
// 层次遍历
public static void levelOrder(Treenode node) {
if (node == null)
return;
Queue<Treenode> queue = new ArrayDeque<Treenode>();
queue.add(node);
while (!queue.isEmpty()) {
Treenode temp = queue.poll();
System.out.print(temp.data);
if (temp.lChild != null)
queue.add(temp.lChild);
if (temp.rChild != null)
queue.add(temp.rChild);
}
}
// 求树的叶子节点数
public static int leafNum(Treenode node) {
if (node != null) {
if (node.lChild == null && node.rChild == null) {
return 1;
}
return leafNum(node.lChild)+leafNum(node.rChild);
}
return 0;
}
//求二叉树的深度
public static int deep(Treenode node){
if(node == null)return 0;
if(node.lChild==null&&node.rChild==null)return 1;
return leafNum(node.lChild)+leafNum(node.rChild);
}
}
class Treenode {
public String data;
public Treenode lChild;
public Treenode rChild;
// 构造函数
public Treenode(String data) {
this.data = data;
this.lChild = null;
this.rChild = null;
}
}