二叉树
定义:(参照数据结构)
* 树(Tree)的定义:树是n(n>=0)个节点的有限集合 T,若n=0,则为空树,否则
* (1)有且只有一个特殊的节点称为树的根节点;
* (2)若n>1时,其余的节点被分为m(m>0)个互不相交的子集T1,T2,...Tm,其中每个子集本身又是一颗树,
* 称其为根的子树
*
* 二叉树(Binary Tree)的定义:二叉树是n(n>=0)个节点的有限集合,若n=0,为空树,否则:
* (1)有且只有一个特殊的称为树的根节点;
* (2)若n>1时,其余节点被分为两个互不相交的子集T1,T2,分别称之为左右子树,并且左右子树又都是二叉树。
1.二叉树的遍历:先序遍历、中序遍历、后续遍历、层次遍历
2.求叶子节点数
3.求二叉树的深度
class BinaryTree{
/**
* 二叉树:
* 遍历:(L:左子树,D:根节点;R:右子树)
*/
/**
* 先序遍历DLR:
* 若二叉树为空,遍历结束,否则
* (1)访问根节点;
* (2)先序遍历左子树(递归调用本算法)
* (3)先序遍历右子树(递归调用本算法)
*
*/
public static void DLR(Node root) {
if (root == null) {
return;
}
System.out.println(root.data); //访问根节点
DLR(root.left); //递归遍历左子树
DLR(root.right);//递归遍历右子树
}
/**
* 中序遍历LDR:
* 若二叉树为空,遍历结束,否则
* (1)先序遍历左子树(递归调用本算法)
* (2)访问根节点;
* (3)先序遍历右子树(递归调用本算法)
*
*/
public static void LDR(Node root) {
if (root == null) {
return;
}
LDR(root.left); //递归遍历左子树
System.out.println(root.data); //访问根节点
LDR(root.right);//递归遍历右子树
}
/**
* 后序遍历LRD:
* 若二叉树为空,遍历结束,否则
* (1)先序遍历左子树(递归调用本算法)
* (2)先序遍历右子树(递归调用本算法)
* (3)访问根节点;
*
*/
public static void LRD(Node root) {
if (root == null) {
return;
}
LRD(root.left); //递归遍历左子树
LRD(root.right);//递归遍历右子树
System.out.println(root.data); //访问根节点
}
/**
* 层次遍历二叉树
* 借助队列
*/
public static void LevelTree(Node root) {
if (root == null) {
return;
}
Queue<Node> queue = new ArrayDeque<Node>();
queue.add(root);
while(!queue.isEmpty()) {
Node node = queue.poll();//获取队首元素,并从队列中删除
System.out.println(node.data);
if (node.left != null) {
queue.add(node.left); //左节点入队
}
if (node.right != null) {
queue.add(node.right); //右节点入队
}
}
}
/**
* 获取叶子节点数
* 依然使用递归
*/
public static int getLeafs(Node root) {
if (root != null) {
if (root.left == null && root.right == null) {
return 1;
}else {
return getLeafs(root.left)+getLeafs(root.right);
}
}
return 0;
}
/**
* 获取二叉树的深度
* 借助层次遍历
*/
public static int getHeight(Node root) {
if (root == null) {
return 0;
}
int h = 0;
int front = 0 ,rear =1;
Queue<Node> queue = new ArrayDeque<Node>();
queue.add(root);
while(!queue.isEmpty()) {
Node node = queue.poll();//获取队首元素,并从队列中删除
front++;
if (node.left != null) {
queue.add(node.left); //左节点入队
}
if (node.right != null) {
queue.add(node.right); //右节点入队
}
if (front == rear) { //front 与rear相等时,当前层的节点遍历完成
h++;
rear = queue.size(); //记录了下一层的节点数
front = 0;
}
}
return h;
}
}
class Node{
//树节点
//二叉链表的存储结构
int data;
Node left;
Node right;
public Node() {
super();
}
public Node(int data) {
super();
this.data = data;
}
}
public class Tree {
//测试
public static void main(String[] args) {
//创建一棵二叉树 9(5(2,7),13(12(10),14))
Node root = new Node(9);
Node node1 = new Node(5);Node node2 = new Node(13);
root.left = node1; root.right = node2;
Node node1_1 = new Node(2); Node node1_2 = new Node(7);
node1.left = node1_1; node1.right = node1_2;
Node node2_1 = new Node(12); Node node2_2 = new Node(14);
node2.left=node2_1; node2.right=node2_2;
Node node2_1_1 = new Node(10);
node2_1.left = node2_1_1;
// Node node2_1_1_1 = new Node(11);
// node2_1_1.right = node2_1_1_1;
// Node node2_2_1 = new Node(15);
// node2_2.right = node2_2_1;
BinaryTree binaryTree = new BinaryTree();
//先序遍历 9 5 2 7 13 12 10 14
binaryTree.DLR(root);
System.out.println("############");
//中序遍历 2 5 7 9 10 12 13 14
binaryTree.LDR(root);
System.out.println("############");
//后序遍历 2 7 5 10 12 14 13 9
binaryTree.LRD(root);
System.out.println("############");
//层次遍历 9 5 13 2 7 12 14 10
binaryTree.LevelTree(root);
System.out.println("获取叶子节点数:"+binaryTree.getLeafs(root)); //4
System.out.println("二叉树的高度:"+binaryTree.getHeight(root));
}
}