//创建节点
class HeroNode{
//编号
private int no;
//姓名
private String name;
//左子树
private HeroNode left;
//右子树
private HeroNode right;
//构造器,左子树和右子树默认为空
public HeroNode(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 HeroNode getLeft() {
return left;
}
public void setLeft(HeroNode left) {
this.left = left;
}
public HeroNode getRight() {
return right;
}
public void setRight(HeroNode right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode{" +
"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);
}
//前序查找
/**
* 前序遍历查找
* @param no 要查找的节点编号
* @return 返回查找的结果
*/
public HeroNode preOrderSearch(int no){
//先判断当前节点是不是要查找的节点
if (this.no == no){
return this;
}
//如果当前节点不是要查找的节点,则判断左子树是否为空,若不为空,则递归前序查找
HeroNode 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;
}
//中序查找
/**
* 中序遍历查找
* @param no 要查找的节点编号
* @return 返回查找的结果
*/
public HeroNode infixOrderSearch(int no){
//先判断当前节点左子树是否为空,如果不为空则递归中序查找
//定义变量保存查找的结果
HeroNode resNode = null;
if (this.left != null){
resNode = this.left.preOrderSearch(no);
}
//如果查找到,则直接返回
if (resNode != null){
return resNode;
}
//如果没有找到,判断当前节点是不是要查找的节点
if (this.no == no){
return this;
}
//如果还没有找到,则判断右子树是否为空,不为空则递归中序查找
if (this.right != null){
resNode = this.right.infixOrderSearch(no);
}
return resNode;
}
//后序查找
/**
* 后续遍历查找
* @param no 要查找的节点编号
* @return 返回查找的结果
*/
public HeroNode postOrderSearch(int no){
//判断当前节点的左子树是否为空,如果不为空,则递归后续查找
HeroNode 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 resNode;
}
}
//创建一颗二叉树
class BinaryTree{
//二叉树必有根节点
private HeroNode root;
public void setRoot(HeroNode 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 HeroNode preOrderSearch(int no){
if (this.root != null){
return this.root.preOrderSearch(no);
}else {
return null;
}
}
//中序查找
public HeroNode infixOrderSearch(int no){
if (this.root != null){
return this.root.infixOrderSearch(no);
}else {
return null;
}
}
//后续查找
public HeroNode postOrderSearch(int no){
if (this.root != null){
return this.root.postOrderSearch(no);
}else {
return null;
}
}
}
public static void main(String[] args) {
//创建一颗二叉树
BinaryTree binaryTree = new BinaryTree();
//创建四个节点
HeroNode node1 = new HeroNode(1, "宋江");
HeroNode node2 = new HeroNode(2, "吴用");
HeroNode node3 = new HeroNode(3, "卢俊义");
HeroNode node4 = new HeroNode(4, "林冲");
HeroNode node5 = new HeroNode(5, "关胜");
//设置二叉树的根节点
binaryTree.setRoot(node1);
//将各个节点连起来
node1.setLeft(node2);
node1.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
//测试前序中序后续遍历
System.out.println("前序遍历");
binaryTree.preOrder();
System.out.println("中序遍历");
binaryTree.infixOrder();
System.out.println("后续遍历");
binaryTree.postOrder();
//测试前序中序后续查找
System.out.println("前序查找");
HeroNode resNode = null;
resNode = binaryTree.preOrderSearch(3);
if (resNode != null){
System.out.println(resNode);
}else {
System.out.println("没有查找到...");
}
System.out.println("中序查找");
resNode = binaryTree.infixOrderSearch(4);
if (resNode != null){
System.out.println(resNode);
}else {
System.out.println("没有查找到...");
}
System.out.println("后序查找");
resNode = binaryTree.postOrderSearch(5);
if (resNode != null){
System.out.println(resNode);
}else {
System.out.println("没有查找到...");
}
}
原文:https://www.cnblogs.com/mx-info/p/14853880.html