package com.haiyang.datastructure.binarytree;
public class BinaryTreeDemo {
public static void main(String[] args) {
TreeNode root = new TreeNode(1, "haiyang");
TreeNode node1 = new TreeNode(2, "haiyang1");
TreeNode node2 = new TreeNode(3, "haiyang2");
TreeNode node3 = new TreeNode(4, "haiyang3");
TreeNode node4 = new TreeNode(5, "haiyang4");
root.setLeft(node1);
root.setRight(node2);
node1.setLeft(node3);
node1.setRight(node4);
BinaryTree binaryTree = new BinaryTree(root);
}
class BinaryTree {
private TreeNode root;
public BinaryTree(TreeNode root) {
this.root = root;
}
public void preOrder() {
if (this.root != null) {
this.root.preOrder();
} else {
System.out.println("二叉树为空");
}
}
public void infixOrder() {
if (this.root != null) {
this.root.infixOrder();
} else {
System.out.println("二叉树为空");
}
}
public void postOrder() {
if (this.root != null) {
this.root.postOrder();
} else {
System.out.println("二叉树为空");
}
}
public TreeNode preOrder(int no) {
if (root != null) {
return root.preOrderSearch(no);
} else {
return null;
}
}
public TreeNode infixOrder(int no) {
if (root != null) {
return root.infixOrderSearch(no);
} else {
return null;
}
}
public TreeNode postOrder(int no) {
if (root != null) {
return root.postOrderSearch(no);
} else {
return null;
}
}
}
class TreeNode {
private int no;
private String name;
private TreeNode left;
private TreeNode right;
public TreeNode(int no, String name) {
this.no = no;
this.name = name;
}
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 TreeNode getLeft() {
return left;
}
public void setLeft(TreeNode left) {
this.left = left;
}
public TreeNode getRight() {
return right;
}
public void setRight(TreeNode right) {
this.right = right;
}
@Override
public String toString() {
return "TreeNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
public void preOrder() {
System.out.println(this);
if (this.left != null) {
this.left.preOrder();
}
if (this.right != null) {
this.right.preOrder();
}
}
public void infixOrder() {
if (this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOrder();
}
}
public void postOrder() {
if (this.left != null) {
this.left.postOrder();
}
if (this.right != null) {
this.right.postOrder();
}
System.out.println(this);
}
public TreeNode preOrderSearch(int no) {
if (this.no == no) {
return this;
}
TreeNode resNode = null;
if (this.left != null) {
resNode = this.left.preOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.preOrderSearch(no);
}
return resNode;
}
public TreeNode infixOrderSearch(int no) {
TreeNode resNode = null;
if (this.left != null) {
resNode = this.left.infixOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
if (this.right != null) {
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
public TreeNode postOrderSearch(int no) {
TreeNode resNode = null;
if (this.left != null) {
resNode = this.left.postOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.right != null) {
resNode = this.right.postOrderSearch(no);
}
if (resNode != null) {
return resNode;
}
if (this.no == no) {
return this;
}
return null;
}
}