二叉树的前中后序查找+思路分析
生活随笔
收集整理的這篇文章主要介紹了
二叉树的前中后序查找+思路分析
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
思路分析
代碼實現
package com.atguigu.tree;/*** @創建人 wdl* @創建時間 2021/3/24* @描述*/ 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("前序遍歷");//1,2,3,5,4binaryTree.preOrder();System.out.println("中序遍歷");//2,1,5,3,4binaryTree.infixOrder();System.out.println("后序遍歷");//2,5,4,3,1binaryTree.postOrder();// //前序查找 // //前序查找的次數 // System.out.println("前序查找"); // HeroNode resNode = binaryTree.preOrderSearch(5); // if(resNode!=null){ // System.out.println("找到了,信息為no="+resNode.getNo()+",name="+resNode.getName()); // }else { // System.out.println("沒有找都no="+15+"的英雄"); // }// //中序查找 // //中序查找的次數 // System.out.println("中序查找"); // HeroNode resNode = binaryTree.infixOrderSearch(5); // if(resNode!=null){ // System.out.println("找到了,信息為no="+resNode.getNo()+",name="+resNode.getName()); // }else { // System.out.println("沒有找都no="+15+"的英雄"); // }//后序查找//后序查找的次數System.out.println("后序查找");HeroNode resNode = binaryTree.postOrderSearch(5);if(resNode!=null){System.out.println("找到了,信息為no="+resNode.getNo()+",name="+resNode.getName());}else {System.out.println("沒有找都no="+15+"的英雄");}}}//定義BinaryTree二叉樹 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 root.postOrderSearch(no);}else {return null;}}}//先創建HeroNode節點 class HeroNode{private int no;private String name;private HeroNode left;//默認為nullprivate HeroNode right;//默認為nullpublic HeroNode(int no, String name) {super();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);//先輸出父節點//遞歸向左子樹前序遍歷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 查找no* @return 如果找到就返回該Node,如果沒有找到返回null*///前序遍歷查找public HeroNode preOrderSearch(int no){System.out.println("進入前序查找");//比較當前節點是不是if(this.no==no){return this;}//1.則判斷當前節點的左子節點是否為空,如果不為空,則遞歸前序查找//2.如果左遞歸前序查找,找到節點,則返回HeroNode resNode=null;if(this.left!=null){resNode=this.left.preOrderSearch(no);}if(resNode!=null){//說明我們左子樹找到return resNode;}//1.左遞歸前序查找,找到節點,則返回,否則判斷//2.當前的節點的右子節點是否為空,如果不空,則繼續向右遞歸前序查找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;}System.out.println("進入中序查找");//如果找到,則返回,如果沒有找到,就和當前節點比較,如果是則返回當前節點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;}System.out.println("進入后序遍歷");//如果左右子樹都沒有找到,就比較當前節點是不是if(this.no==no){return this;}return resNode;}}總結
以上是生活随笔為你收集整理的二叉树的前中后序查找+思路分析的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 笔记本电池的杀手被找到了
- 下一篇: 电脑死机怎么办电脑死机咋办