代码实现
package com.springboot.数据结构和算法.tree;
/**
* @author: 牧羊
* @Date: 2020/5/28 18:13
* 二叉树遍历
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
//需要先创建一个二叉树
BinaryTree binaryTree = new BinaryTree();
//创建一个需要的节点
Node root = new Node(1,"宋江");
Node node2 = new Node(2,"吴用");
Node node3 = new Node(3,"卢俊义");
Node node4 = new Node(4,"林冲");
Node node5 = new Node(5,"关胜");
//说明,我们先手动创建二叉树,后面我们学习递归的方式创建二叉树
root.setLeftNode(node2);
root.setRightNode(node3);
node3.setLeftNode(node5);
node3.setRightNode(node4);
binaryTree.setRoot(root);
//测试
System.out.println("前序遍历");
binaryTree.proOrder();
//测试
System.out.println("中序遍历");
binaryTree.infixOrder();
//测试
System.out.println("后序遍历");
binaryTree.postOrder();
}
}
//定义BinaryTree二叉树
class BinaryTree{
private Node root;
public void setRoot(Node root){
this.root = root;
}
//前序遍历
public void proOrder(){
if(root == null){
System.out.println("二叉树为空");
}else{
this.root.preOrder();
}
}
//中序遍历
public void infixOrder(){
if(root == null){
System.out.println("二叉树为空");
}else{
this.root.infixOrder();
}
}
//后序遍历
public void postOrder(){
if(root == null){
System.out.println("二叉树为空");
}else{
this.root.postOrder();
}
}
}
//创建节点
class Node{
private int no;
private String name;
private Node leftNode;//左边节点
private Node rightNode;//右边节点
public Node(int no, String name) {
this.no = no;
this.name = name;
}
//编写前序遍历方法
public void preOrder(){
System.out.println(this);
//递归向左子树前序遍历
if(this.leftNode != null){
this.leftNode.preOrder();
}
//递归向右子树前序遍历
if(this.rightNode != null){
this.rightNode.preOrder();
}
}
//中序遍历
public void infixOrder(){
//递归向左子树前序遍历
if(this.leftNode != null){
this.leftNode.preOrder();
}
System.out.println(this);
//递归向右子树前序遍历
if(this.rightNode != null){
this.rightNode.preOrder();
}
}
//后序遍历
public void postOrder(){
//递归向左子树前序遍历
if(this.leftNode != null){
this.leftNode.preOrder();
}
//递归向右子树前序遍历
if(this.rightNode != null){
this.rightNode.preOrder();
}
System.out.println(this);
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}
代码实现
package com.springboot.数据结构和算法.tree;
/**
* @author: 牧羊
* @Date: 2020/5/28 18:13
* 二叉树遍历
*/
public class BinaryTreeDemo {
public static void main(String[] args) {
//需要先创建一个二叉树
BinaryTree binaryTree = new BinaryTree();
//创建一个需要的节点
Node root = new Node(1,"宋江");
Node node2 = new Node(2,"吴用");
Node node3 = new Node(3,"卢俊义");
Node node4 = new Node(4,"林冲");
Node node5 = new Node(5,"关胜");
//说明,我们先手动创建二叉树,后面我们学习递归的方式创建二叉树
root.setLeftNode(node2);
root.setRightNode(node3);
node3.setLeftNode(node5);
node3.setRightNode(node4);
binaryTree.setRoot(root);
//测试
//System.out.println("前序遍历");
// binaryTree.proOrder();
//测试
// System.out.println("中序遍历");
// binaryTree.infixOrder();
//测试
// System.out.println("后序遍历");
// binaryTree.postOrder();
System.out.println(binaryTree.findPreOrder(6));
}
}
//定义BinaryTree二叉树
class BinaryTree{
private Node root;
public void setRoot(Node root){
this.root = root;
}
//前序遍历
public void proOrder(){
if(root == null){
System.out.println("二叉树为空");
}else{
this.root.preOrder();
}
}
//中序遍历
public void infixOrder(){
if(root == null){
System.out.println("二叉树为空");
}else{
this.root.infixOrder();
}
}
//后序遍历
public void postOrder(){
if(root == null){
System.out.println("二叉树为空");
}else{
this.root.postOrder();
}
}
//前序遍历查找
public Node findPreOrder(int id){
if(root == null){
System.out.println("二叉树为空");
return null;
}else{
return root.findPreOrder(id);
}
}
//中序序遍历查找
public Node findInfixOrder(int id){
if(root == null){
System.out.println("二叉树为空");
return null;
}else{
return root.findInfixOrder(id);
}
}
//后序序遍历查找
public Node findPostOrder(int id){
if(root == null){
System.out.println("二叉树为空");
return null;
}else{
return root.findPostOrder(id);
}
}
}
//创建节点
class Node{
private int no;
private String name;
private Node leftNode;//左边节点
private Node rightNode;//右边节点
public int sum = 0;
public Node(int no, String name) {
this.no = no;
this.name = name;
}
//编写前序遍历方法
public void preOrder(){
System.out.println(this);
//递归向左子树前序遍历
if(this.leftNode != null){
this.leftNode.preOrder();
}
//递归向右子树前序遍历
if(this.rightNode != null){
this.rightNode.preOrder();
}
}
//中序遍历
public void infixOrder(){
//递归向左子树前序遍历
if(this.leftNode != null){
this.leftNode.preOrder();
}
System.out.println(this);
//递归向右子树前序遍历
if(this.rightNode != null){
this.rightNode.preOrder();
}
}
//后序遍历
public void postOrder(){
//递归向左子树前序遍历
if(this.leftNode != null){
this.leftNode.preOrder();
}
//递归向右子树前序遍历
if(this.rightNode != null){
this.rightNode.preOrder();
}
System.out.println(this);
}
//编写前序遍历查找方法
public Node findPreOrder(int no){
if(this.no == no){
return this;
}
Node redNode = null;
//递归向左子树前序遍历
if(this.leftNode != null){
redNode = this.leftNode.findPreOrder(no);
}
if(redNode != null){
return redNode;
}
//递归向右子树前序遍历
if(this.rightNode != null){
redNode = this.rightNode.findPreOrder(no);
}
return redNode;
}
//编写中序遍历查找方法
public Node findInfixOrder(int no){
Node redNode = null;
//递归向左子树前序遍历
if(this.leftNode != null){
redNode = this.leftNode.findInfixOrder(no);
}
if(redNode != null){
return redNode;
}
if(this.no == no){
return this;
}
//递归向右子树前序遍历
if(this.rightNode != null){
redNode = this.rightNode.findInfixOrder(no);
}
return redNode;
}
//编写后序遍历查找方法
public Node findPostOrder(int no){
Node redNode = null;
//递归向左子树前序遍历
if(this.leftNode != null){
redNode = this.leftNode.findPostOrder(no);
}
if(redNode != null){
return redNode;
}
//递归向右子树前序遍历
if(this.rightNode != null){
redNode = this.rightNode.findPostOrder(no);
}
if(this.no == no){
return this;
}
return redNode;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Node getLeftNode() {
return leftNode;
}
public void setLeftNode(Node leftNode) {
this.leftNode = leftNode;
}
public Node getRightNode() {
return rightNode;
}
public void setRightNode(Node rightNode) {
this.rightNode = rightNode;
}
@Override
public String toString() {
return "Node{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
}