1.前中后序查找思路分析
1.1前序查找
- 判斷與當(dāng)前節(jié)點(diǎn)是否相等,如果相等則返回當(dāng)前節(jié)點(diǎn),否則判斷當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)是否為空
- 如果左子節(jié)點(diǎn)不為空徘铝,則遞歸前序查找左子樹南用,找到就返回節(jié)點(diǎn),否則就繼續(xù)判斷當(dāng)前節(jié)點(diǎn)右子節(jié)點(diǎn)是否為空
- 如果右子節(jié)點(diǎn)不為空仿野,則遞歸前序查找右子樹,找到就返回節(jié)點(diǎn),否則返回null
//前序查找
public HeroNode preIndexSearch(int no){
//判斷與當(dāng)前節(jié)點(diǎn)no是否相同典尾,相同返回
if(this.no==no){
return this;
}
//判斷左子節(jié)點(diǎn)是否為空,如果不為空糊探,遞歸前序查詢左子樹,如果相等钾埂,返回
HeroNode temp = null;
if(this.left!=null){
temp = this.left.preIndexSearch(no);
}
if(temp!=null){
return temp;
}
if(this.right!=null){
temp = this.right.preIndexSearch(no);
}
return temp;
}
1.2中序查找
- 判斷當(dāng)前節(jié)點(diǎn)的左子節(jié)點(diǎn)是否為空,如果不為空就遞歸中序查找科平,找到就返回
- 否則與當(dāng)前節(jié)點(diǎn)比較褥紫,如果相等就返回,否則繼續(xù)判斷當(dāng)前節(jié)點(diǎn)的右子節(jié)點(diǎn)是否為空
-如果右子節(jié)點(diǎn)不為空瞪慧,就遞歸中序查找右子樹故源,找到就返回,否則返回null
//中序查找
public HeroNode infixIndexSearch(int no){
HeroNode temp = null;
if(this.left!=null){
temp = this.left.infixIndexSearch(no);
}
if(temp!=null){
return temp;
}
if (this.no==no){
return this;
}
if(this.right!=null){
temp = this.right.infixIndexSearch(no);
}
return temp;
}
1.3后序查找
- 判斷當(dāng)前節(jié)點(diǎn)的左子樹是否為空汞贸,如果不為空绳军,則遞歸后序查找左子樹,找到就返回
-否則判斷當(dāng)前節(jié)點(diǎn)的右子節(jié)點(diǎn)是否為空矢腻,如果不為空门驾,則遞歸查找右子樹,找到就返回
- 否則與當(dāng)前節(jié)點(diǎn)比較多柑,相等返回當(dāng)前節(jié)點(diǎn)奶是,否則返回null
//后序查找
public HeroNode postIndexSearch(int no){
HeroNode temp = null;
if(this.left!=null){
temp = this.left.postIndexSearch(no);
}
if(temp!=null){
return temp;
}
if(this.right!=null){
temp = this.right.postIndexSearch(no);
}
if(temp!=null){
return temp;
}
if(this.no==no){
return this;
}
return temp;
}
2.完整代碼
package com.yc.day06;
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, "林沖");
binaryTree.setRoot(root);
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
System.out.println("前序遍歷:");
binaryTree.preSort();
System.out.println("");
HeroNode heroNode = binaryTree.preIndexSearch(2);
System.out.println("前序查找結(jié)果:"+heroNode);
HeroNode heroNode1 = binaryTree.infixIndexSearch(2);
System.out.println("中序查找結(jié)果:"+heroNode1);
HeroNode heroNode2 = binaryTree.postIndexSearch(2);
System.out.println("后序查找結(jié)果:"+heroNode2);
}
}
//創(chuàng)建二叉樹
class BinaryTree{
HeroNode root;
public void setRoot(HeroNode heroNode){
this.root = heroNode;
}
//前序遍歷
public void preSort(){
root.preSort();
}
//中序遍歷
public void infixSort(){
root.infixSort();
}
//后序遍歷
public void postSort(){
root.postSort();
}
//前序查找
public HeroNode preIndexSearch(int no){
if(root!=null){
return root.preIndexSearch(no);
}
return null;
}
//中序查找
public HeroNode infixIndexSearch(int no){
if(root!=null){
return root.infixIndexSearch(no);
}
return null;
}
//后序查找
public HeroNode postIndexSearch(int no){
if(root!=null) {
return root.postIndexSearch(no);
}
return null;
}
}
//創(chuàng)建節(jié)點(diǎn)類
class HeroNode{
private int no;
private String name;
private HeroNode left;
private HeroNode right;
public HeroNode() {
}
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 preSort(){
System.out.println(this);
if(this.left!=null){
this.left.preSort();
}
if(this.right!=null){
this.right.preSort();
}
}
//中序遍歷
public void infixSort(){
if(this.left!=null){
this.left.infixSort();
}
System.out.println(this);
if(this.right!=null){
this.right.infixSort();
}
}
//后序遍歷
public void postSort(){
if(this.left!=null){
this.left.postSort();
}
if(this.right!=null){
this.right.postSort();
}
System.out.println(this);
}
//前序查找
public HeroNode preIndexSearch(int no){
//判斷與當(dāng)前節(jié)點(diǎn)no是否相同,相同返回
if(this.no==no){
return this;
}
//判斷左子節(jié)點(diǎn)是否為空,如果不為空聂沙,遞歸前序查詢左子樹,如果相等秆麸,返回
HeroNode temp = null;
if(this.left!=null){
temp = this.left.preIndexSearch(no);
}
if(temp!=null){
return temp;
}
if(this.right!=null){
temp = this.right.preIndexSearch(no);
}
return temp;
}
//中序查找
public HeroNode infixIndexSearch(int no){
HeroNode temp = null;
if(this.left!=null){
temp = this.left.infixIndexSearch(no);
}
if(temp!=null){
return temp;
}
if (this.no==no){
return this;
}
if(this.right!=null){
temp = this.right.infixIndexSearch(no);
}
return temp;
}
//后序查找
public HeroNode postIndexSearch(int no){
HeroNode temp = null;
if(this.left!=null){
temp = this.left.postIndexSearch(no);
}
if(temp!=null){
return temp;
}
if(this.right!=null){
temp = this.right.postIndexSearch(no);
}
if(temp!=null){
return temp;
}
if(this.no==no){
return this;
}
return temp;
}
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者