实现 二叉树的 先序遍历 中序遍历 后序遍历 层序遍历 求结点个数 求叶子结点的个数 求第k层结点的个数 查找结点。
package ds.bin_tree;
import java.util.*;
/**
* @Author liusifan
* @Data 2022/2/20 10:28
*/
public class MyTree {
public static class TreeNode
{
private TreeNode right;
private TreeNode left;
private char value;
public TreeNode(char value)
{
this.value = value;
}
public static TreeNode treebuild()
{
TreeNode nodeA=new TreeNode('A');
TreeNode nodeB=new TreeNode('B');
TreeNode nodeC=new TreeNode('C');
TreeNode nodeD=new TreeNode('D');
TreeNode nodeE=new TreeNode('E');
TreeNode nodeF=new TreeNode('F');
TreeNode nodeH=new TreeNode('H');
TreeNode nodeG=new TreeNode('G');
nodeA.left=nodeB;
nodeB.left=nodeD;
nodeB.right=nodeE;
nodeE.right=nodeH;
nodeA.right=nodeC;
nodeC.right=nodeG;
nodeC.left=nodeF;
return nodeA;
}
/**
* 前序排列
* @param root
*/
public static void preOder(TreeNode root)
{
if (root==null)
{
return;
}
System.out.print(root.value+",");
preOder(root.left);
preOder(root.right);
}
/**
* 中序遍历
* @param root
*/
public static void midOder(TreeNode root)
{
if (root==null)
{
return;
}
midOder(root.left);
System.out.print(root.value+",");
midOder(root.right);
}
/**
* 后续遍历
* @param root
*/
public static void postOder(TreeNode root)
{
if (root==null)
{
return;
}
postOder(root.left);
postOder(root.right);
System.out.print(root.value+",");
}
/**
* 二叉树的高度
* @param root
* @return
*/
public static int height(TreeNode root)
{
if (root==null)
{
return 0;
}
return 1+Math.max(height(root.left),height(root.right));
}
/**
*结点个数
* @param root
* @return
*/
public static int getSize(TreeNode root)
{
if (root==null)
{
return 0;
}
return 1+getSize(root.right)+getSize(root.left);
}
/**
* 叶子结点个数
* @param root
* @return
*/
public static int getLeftSize(TreeNode root)
{
if (root==null)
{
return 0;
}
if (root.left==null&&root.right==null)
{
return 1;
}
return getLeftSize(root.left)+getLeftSize(root.right);
}
/**
* 第k层结点
* @param root
* @param k
* @return
*/
public static int getLevelSize(TreeNode root,int k)
{
if (root==null||k<=0)
{
return 0;
}
if (k==1)
{
return 1;
}
return getLevelSize(root.left,k-1)+getLevelSize(root.right,k-1);
}
/**
* 叶子结点个数(迭代)
* @param root
* @return
*/
public static int getlevelOrderTraversal(TreeNode root) {
if (root == null) {
return 0;
}
int size=0;
Deque<TreeNode> queue = new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty())
{
TreeNode cur=queue.poll();
if (cur.right==null&&cur.left==null)
{
size++;
continue;
}
if (cur.left!=null)
{
queue.offer(cur.left);
}
if (cur.right!=null)
{
queue.offer(cur.right);
}
}
return size;
}
/**
* 结点个数(迭代)
* @param root
* @return
*/
public static int getNodeOrderTraversal(TreeNode root)
{
if (root==null)
{
return 0;
}
int size=1;
Queue<TreeNode> queue=new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty())
{
TreeNode cur= queue.poll();
if (cur.left!=null)
{
queue.offer(cur.left);
size++;
}
if (cur.right!=null)
{
queue.offer(cur.right);
size++;
}
}
return size;
}
/**
* 层序遍历
* @param root
*/
public static void levelOrderTraversal(TreeNode root)
{
if (root==null)
{
return;
}
Deque<TreeNode> queue=new LinkedList<>();
queue.offer(root);
while (!queue.isEmpty())
{
TreeNode cur= queue.poll();
System.out.print(cur.value+",");
if (cur.left!=null)
{
queue.offer(cur.left);
}
if (cur.right!=null)
{
queue.offer(cur.right);
}
}
}
/**
* 查找结点
* @param root
* @param value
* @return
*/
public static TreeNode find(TreeNode root, char value)
{
if (root==null)
{
return null;
}
if (root.value==value)
{
return root;
}
TreeNode left=find(root.left,value);
if (left!=null)
{
return left;
}
TreeNode right=find(root.right,value);
if (right!=null)
{
return right;
}
return null;
}
public static void main(String[] args) {
TreeNode root=treebuild();
System.out.print("前序遍历:");
preOder(root);
System.out.println();
System.out.print("中序遍历:");
midOder(root);
System.out.println();
System.out.print("后序遍历:");
postOder(root);
System.out.println();
System.out.print("层序遍历:");
levelOrderTraversal(root);
System.out.println();
System.out.println("当前二叉树的高度是:"+height(root));
System.out.println("当前二叉树的结点个数是"+getSize(root));
System.out.println("当前二叉树叶子结点的个数是"+getLeftSize(root));
System.out.println("当前第3层结点个数为"+getLevelSize(root,3));
System.out.println("当前二叉树叶子结点个数是(层序遍历):"+getlevelOrderTraversal(root));
System.out.println("当前二叉树结点个数为(层序遍历):"+getNodeOrderTraversal(root));
System.out.println(find(root,'A'));
}
}
}