使用前,中,后序的方式来查询指定节点元素
前序查找思路
- 先判断当前节点的no是否等于查找节点的no,如果相等,则返回当前节点
- 如果不相等,则判断当前节点的左子节点是否为空,如果不为空则递归查找
- 如果左递归前序查找,找到了节点,则返回,否则继续判断当前节点的右子节点是否为空,如果不为空,则继续向右递归查找
中序查找思路
- 判断当前节点的左子节点是否为空,如果不为空,则向左递归中序查找
- 如果找到,则返回,如果找不到,就和当前节点比较,如果等于当前节点则返回,否则继续进行右递归中序查找
- 如果当前节点的右子节点不为空,则右递归进行中序查找,找到返回,否则返回空
后序查找思路
- 判断当前节点左子节点是否为空,如果不为空,则递归后序查找
- 如果找到,则返回,如果没有找到,就判断当前节点的右子节点是否为空,如果不为空,则右递归进行后序查找,如果找到,就返回
- 如果找不到,就和当前节点比较,如果和当前节点相等就返回当前节点,否则返回空
package com.hanlin.tree;
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree = new BinaryTree();
HeroNode root = new HeroNode(1, "root");
HeroNode node2 = new HeroNode(2, "node2");
HeroNode node3 = new HeroNode(3, "node3");
HeroNode node4 = new HeroNode(4, "node4");
HeroNode node5 = new HeroNode(5, "node5");
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
// System.out.println("前序遍历结果为:");
// binaryTree.preOrder();
// System.out.println("中序遍历结果为:");
// binaryTree.infixOrder();
// System.out.println("后序遍历结果为:");
// binaryTree.postOrder();
System.out.println("二叉树查找指定编号的节点元素信息");
HeroNode heroNode = binaryTree.postOrderSearch(5);
if(heroNode != null) {
System.out.printf("找到指定编号为%d 姓名为:%s",heroNode.getNo(),heroNode.getName());
}else {
System.out.printf("没有找到指定编号为的元素");
}
}
}
/**
* 创建一个二叉树对象
*/
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("当前二叉树为空,无法遍历!");
}
}
/**
* 前序方式查找节点
* @param no 查找节点编号
* @return 返回查找的结果
*/
public HeroNode preOrderSearch(int no){
if(root != null) {
return root.preOrderSearch(no);
}else {
return null;
}
}
/**
* 中序方式查找节点
* @param no 查找节点编号
* @return 返回查找的结果
*/
public HeroNode infixOrderSearch(int no) {
if(root != null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
/**
* 后序方式查找节点
* @param no 查找节点编号
* @return 返回查找的结果
*/
public HeroNode postOrderSearch(int no) {
if(root != null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
}
/**
* 创建一颗树的一个数据节点
*/
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;
}
@Override
public String toString() {
return "HeroNode{" +
"no=" + no +
", 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;
}
/**
* 实现前序遍历
*/
public void preOrder(){
//先输入父节点
System.out.println(this);
//递归向左子数前序遍历
if(this.left != null) {
this.left.preOrder();
}
//递归向右子数前序遍历
if(this.right != null) {
this.right.preOrder();
}
}
/**
* 实现中序遍历
*/
public void infixOrder(){
//1,递归向左子数中序遍历
if(this.left != null) {
this.left.infixOrder();
}
//2,输出父节点
System.out.println(this);
//3,递归向右子数中序遍历
if(this.right != null) {
this.right.infixOrder();
}
}
/**
* 实现后序遍历
*/
public void postOrder(){
//1,递归向左子树后序遍历
if(this.left != null) {
this.left.postOrder();
}
//2,递归向右子树后序遍历
if(this.right != null) {
this.right.postOrder();
}
//3,输出当前节点
System.out.println(this);
}
/**
* 通过前序的方式实现查找指定编号的节点
* @param no 待查找的编号
* @return 返回找到的节点
*/
public HeroNode preOrderSearch(int no){
//比较当前节点编号是否和no相等
if(this.no == no) {
return this;
}
//左子节点递归查找
HeroNode resultNode = null;
if(this.left != null) {
resultNode = this.left.preOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
//右子节点递归查找
if(this.right != null) {
resultNode = this.right.preOrderSearch(no);
}
return resultNode;
}
/**
* 通过中序的方式实现查找指定编号的节点
* @param no 待查找的编号
* @return 返回找到的节点
*/
public HeroNode infixOrderSearch(int no) {
//左子节点递归查找
HeroNode resultNode = null;
if(this.left != null) {
resultNode = this.left.infixOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
//比较当前节点编号是否和no相等
if(this.no == no) {
return this;
}
//右子节点递归查找
if(this.right != null) {
resultNode = this.right.infixOrderSearch(no);
}
return resultNode;
}
/**
* 通过后序的方式实现查找指定编号的节点
* @param no 待查找的编号
* @return 返回找到的节点
*/
public HeroNode postOrderSearch(int no) {
//左子节点递归查找
HeroNode resultNode = null;
if(this.left != null) {
resultNode = this.left.postOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
//右子节点递归查找
if(this.right != null) {
resultNode = this.right.postOrderSearch(no);
}
if(resultNode != null) {
return resultNode;
}
//比较当前节点编号是否和no相等
if(this.no == no) {
return this;
}
return resultNode;
}
}