1、二叉树的递归创建(少说废话,直接上code)
//前序遍历的创建二叉树 public static TreeNode preCreateBinaryTree() { Scanner scanner = new Scanner(System.in); //得到用户输入 int data = scanner.nextInt(); //如果用户输入为0,认为其为空节点 if (data == 0) return null; //递归创建二叉树 TreeNode node = new TreeNode(data, preCreateBinaryTree(),preCreateBinaryTree()); return node; }
2、二叉树的前序遍历、中序遍历、后续遍历、层次遍历
public static void visitNodeElements(int nodeValue){
System.out.print(nodeValue+" ");
}
//前序递归遍历
public static void preOrderTraversal(TreeNode treeNode){
if( treeNode != null){
//访问第一个节点
visitNodeElements(treeNode.data);
//递归左子树
preOrderTraversal(treeNode.lchild);
//递归右子树
preOrderTraversal(treeNode.rchild);
}
}
//中序递归遍历
public static void inOrderTraversal(TreeNode treeNode){
if( treeNode != null){
//递归左子树
inOrderTraversal(treeNode.lchild);
//访问第一个节点
visitNodeElements(treeNode.data);
//递归右子树
inOrderTraversal(treeNode.rchild);
}
}
//后序递归遍历
public static void postOrderTraversal(TreeNode treeNode){
if( treeNode != null){
//递归左子树
postOrderTraversal(treeNode.lchild);
//递归右子树
postOrderTraversal(treeNode.rchild);
//访问第一个节点
visitNodeElements(treeNode.data);
}
}
//层次遍历
public static void levelOrder(TreeNode node) {
LinkedList<TreeNode> list = new LinkedList<>();
list.add(node);
while(!list.isEmpty()) {
node=list.removeFirst();
System.out.print(node.data+" ");
if(node.lchild!=null)
list.add(node.lchild);
if(node.rchild!=null)
list.add(node.rchild);
}
}
3、最后贴出所有代码
package com.yuanfeng.test;/**
* Created by yuanfeng on 2019/7/20 9:35
*/
import java.util.LinkedList;
import java.util.Scanner;
/**
*@ClassName TreeTreeNode
*@Description T0D0
*@Author yuanfeng
*@Date 2019/7/20 9:35
*@Version 1.0
**/
public class TreeNode {
private int data;//节点数据域
private TreeNode lchild;//节点左孩子
private TreeNode rchild;//节点右孩子
public int getData() {
return data;
}
public void setData(int data) {
this.data = data;
}
public TreeNode getLchild() {
return lchild;
}
public void setLchild(TreeNode lchild) {
this.lchild = lchild;
}
public TreeNode getRchild() {
return rchild;
}
public void setRchild(TreeNode rchild) {
this.rchild = rchild;
}
public TreeNode(int data, TreeNode lchild, TreeNode rchild) {
this.data = data;
this.lchild = lchild;
this.rchild = rchild;
}
//前序遍历的创建二叉树
public static TreeNode preCreateBinaryTree() {
Scanner scanner = new Scanner(System.in);
//得到用户输入
int data = scanner.nextInt();
//如果用户输入为0,认为其为空节点
if (data == 0)
return null;
//递归创建二叉树
TreeNode node = new TreeNode(data, preCreateBinaryTree(),preCreateBinaryTree());
return node;
}
public static void visitNodeElements(int nodeValue){
System.out.print(nodeValue+" ");
}
//前序递归遍历
public static void preOrderTraversal(TreeNode treeNode){
if( treeNode != null){
//访问第一个节点
visitNodeElements(treeNode.data);
//递归左子树
preOrderTraversal(treeNode.lchild);
//递归右子树
preOrderTraversal(treeNode.rchild);
}
}
//中序递归遍历
public static void inOrderTraversal(TreeNode treeNode){
if( treeNode != null){
//递归左子树
inOrderTraversal(treeNode.lchild);
//访问第一个节点
visitNodeElements(treeNode.data);
//递归右子树
inOrderTraversal(treeNode.rchild);
}
}
//后序递归遍历
public static void postOrderTraversal(TreeNode treeNode){
if( treeNode != null){
//递归左子树
postOrderTraversal(treeNode.lchild);
//递归右子树
postOrderTraversal(treeNode.rchild);
//访问第一个节点
visitNodeElements(treeNode.data);
}
}
//层次遍历
public static void levelOrder(TreeNode node) {
LinkedList<TreeNode> list = new LinkedList<>();
list.add(node);
while(!list.isEmpty()) {
node=list.removeFirst();
System.out.print(node.data+" ");
if(node.lchild!=null)
list.add(node.lchild);
if(node.rchild!=null)
list.add(node.rchild);
}
}
public static void main(String[] args) {
TreeNode node = preCreateBinaryTree();
System.out.println("前序遍历");
preOrderTraversal(node);//前序
System.out.println();
System.out.println("中序遍历");
inOrderTraversal(node);//中序
System.out.println();
System.out.println("后序遍历");
postOrderTraversal(node);//后序
System.out.println();
System.out.println("层次遍历");
levelOrder(node);//后序
}
}
效果图