并分别使用三種查找方式,查找 heroNO = 5 的節點
代碼示例:
package com.wxit.tree;
/**
* @Author wj
**/
public class BinaryTreeDemo {
public static void main(String[] args) {
//先建立一顆二叉樹
BinaryTree binaryTree = new BinaryTree();
//建立需要的節點
HeroNode root = new HeroNode(1, "吳傑");
HeroNode node2 = new HeroNode(2, "吳昊");
HeroNode node3 = new HeroNode(3, "小昊");
HeroNode node4 = new HeroNode(4, "張三");
HeroNode node5 = new HeroNode(5, "李婷");
//先手動建立二叉樹
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 resNode = binaryTree.preOrderSearch(5);
if (resNode != null){
System.out.printf("找到了,資訊為no=%d name=%s",resNode.getNo(),resNode.getName());
} else {
System.out.printf("沒有找到no = %d 的英雄",5);
}
}
}
//建立HeroNode節點
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);
}
//前序周遊查找
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;
}
//中序周遊查找
public HeroNode infixOrderSearch(int no){
//判斷目前節點的左子節點是否為空,如果不為空,則遞歸中序查找
HeroNode 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 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 (root != null){
return root.preOrderSearch(no);
}else {
return null;
}
}
//中序周遊
public HeroNode infixOrderSearch(int no){
if (root != null){
return root.infixOrderSearch(no);
} else {
return null;
}
}
//後序周遊
public HeroNode postOrderSearch(int no){
if (root != null){
return postOrderSearch(no);
} else {
return null;
}
}
}