数据结构 - 二叉树(前序中序后序查找)
生活随笔
收集整理的這篇文章主要介紹了
数据结构 - 二叉树(前序中序后序查找)
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
public static int i = 1, j = 1, k =1;//編寫前序查找方法public HeroNode preOrderSearch(int no){System.out.println("前序遍歷"+(i++)+"次");if (this.no == no){return this;}HeroNode heroNode = null;if (this.left != null){heroNode = this.left.preOrderSearch(no);}//不等于空說明在左邊找到了if (heroNode != null){return heroNode;}if (this.right != null){heroNode = this.right.preOrderSearch(no);}return heroNode;}//中序遍歷查找public HeroNode infixOrderSearch(int no){HeroNode heroNode = null;//先判斷當(dāng)前節(jié)點的左子節(jié)點是否為空,不為空繼續(xù)進行中序查找if (this.left != null){heroNode = this.left.infixOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("中序遍歷"+(j++)+"次");if (this.no == no){return this;}if (this.right != null){heroNode = this.right.infixOrderSearch(no);}return heroNode;}//后序遍歷查找public HeroNode postOrderSearch(int no){HeroNode heroNode = null;//判斷當(dāng)前節(jié)點的左子節(jié)點是否為空,不為空,則遞歸后序遍歷查找if (this.left != null){heroNode = this.left.postOrderSearch(no);}if (heroNode != null){return heroNode;}//判斷當(dāng)前節(jié)點的右子節(jié)點是否為空,不為空,則遞歸后序遍歷查找if (this.right != null){heroNode = this.right.postOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("后序遍歷"+(k++)+"次");//左右子樹都沒有找到,比較當(dāng)前節(jié)點是不是if (this.no == no){return this;}return heroNode;}
//前序查找public HeroNode preOrederSearch(int no){if (root != null){return root.preOrderSearch(no);}else {return null;}}//中序查找public HeroNode infixOrderSeach(int no){if (root != null){return root.infixOrderSearch(no);}else {return null;}}//后序查找public HeroNode postOrderSeach(int no){if (root != null){return root.postOrderSearch(no);}else {return null;}}
完整代碼
package tree;public class BinaryTreeDemo {public static void main(String[] args) {//先需要創(chuàng)建一顆二叉樹BinaryTree binaryTree = new BinaryTree();//創(chuàng)建需要的節(jié)點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, "關(guān)勝");//說明,先手動創(chuàng)建該二叉樹,后面學(xué)習(xí)遞歸方式創(chuàng)建二叉樹binaryTree.setRoot(root);root.setLeft(node2);root.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 heroNode1 = binaryTree.preOrederSearch(5);if (heroNode1 != null){System.out.println("找到節(jié)點:" + heroNode1.toString());}else {System.out.println("沒有找到");}// //中序遍歷查找System.out.println("中序遍歷查找:~~~~");HeroNode heroNode2 = binaryTree.infixOrderSeach(5);if (heroNode2 != null){System.out.println("找到節(jié)點:" + heroNode2.toString());}else {System.out.println("沒有找到");}//后序遍歷查找System.out.println("后序遍歷查找:~~~~");HeroNode heroNode3 = binaryTree.postOrderSeach(5);if (heroNode3 != null){System.out.println("找到節(jié)點:" + heroNode3.toString());}else {System.out.println("沒有找到");}} }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 preOrederSearch(int no){if (root != null){return root.preOrderSearch(no);}else {return null;}}//中序查找public HeroNode infixOrderSeach(int no){if (root != null){return root.infixOrderSearch(no);}else {return null;}}//后序查找public HeroNode postOrderSeach(int no){if (root != null){return root.postOrderSearch(no);}else {return null;}}} class HeroNode{private int no;private String name;private HeroNode left;//默認(rèn)nullprivate HeroNode right;//默認(rèn)null;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;}@Overridepublic String toString() {return "HeroNode{" +"no=" + no +", name='" + name + '\'' +'}';}//編寫前序遍歷方法public void preOrder(){System.out.println(this);//先輸出父節(jié)點//遞歸向左子樹前序遍歷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);//輸出父節(jié)點//遞歸向右子樹前序遍歷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 static int i = 1, j = 1, k =1;//編寫前序查找方法public HeroNode preOrderSearch(int no){System.out.println("前序遍歷"+(i++)+"次");if (this.no == no){return this;}HeroNode heroNode = null;if (this.left != null){heroNode = this.left.preOrderSearch(no);}//不等于空說明在左邊找到了if (heroNode != null){return heroNode;}if (this.right != null){heroNode = this.right.preOrderSearch(no);}return heroNode;}//中序遍歷查找public HeroNode infixOrderSearch(int no){HeroNode heroNode = null;//先判斷當(dāng)前節(jié)點的左子節(jié)點是否為空,不為空繼續(xù)進行中序查找if (this.left != null){heroNode = this.left.infixOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("中序遍歷"+(j++)+"次");if (this.no == no){return this;}if (this.right != null){heroNode = this.right.infixOrderSearch(no);}return heroNode;}//后序遍歷查找public HeroNode postOrderSearch(int no){HeroNode heroNode = null;//判斷當(dāng)前節(jié)點的左子節(jié)點是否為空,不為空,則遞歸后序遍歷查找if (this.left != null){heroNode = this.left.postOrderSearch(no);}if (heroNode != null){return heroNode;}//判斷當(dāng)前節(jié)點的右子節(jié)點是否為空,不為空,則遞歸后序遍歷查找if (this.right != null){heroNode = this.right.postOrderSearch(no);}if (heroNode != null){return heroNode;}System.out.println("后序遍歷"+(k++)+"次");//左右子樹都沒有找到,比較當(dāng)前節(jié)點是不是if (this.no == no){return this;}return heroNode;} }后序遍歷查找最快
總結(jié)
以上是生活随笔為你收集整理的数据结构 - 二叉树(前序中序后序查找)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 兴业信用卡美食优惠 兴业银行大众点评活动
- 下一篇: 3岁男童发O音就能召唤蚊子 吸引470万