Java中二叉樹的遍歷、查詢
阿新 • • 發佈:2022-12-07
1、準備節點
/** * 二叉樹的節點 * @author lurenjia * @date 2022/12/7-12:07 */ public class Node { Object value; Node leftChild; Node rightChild; public Node(Object o){ value = o; } public Node(Object value, Node leftChild, Node rightChild) { this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } @Override public String toString() { return "Node{" + "value=" + value + ", leftChild=" + leftChild + ", rightChild=" + rightChild + '}'; } }
2、實現對二叉樹的操作
import java.util.Deque; import java.util.LinkedList; import java.util.Queue; /** * 二叉樹實現 * @author lurenjia * @date 2022/12/7-12:10 */ public class BinaryTree { private Node root;//根節點 public BinaryTree(Node root) { this.root = root;} public BinaryTree() {} /** * 判斷二叉樹是否為空,為空返回true *@return */ public boolean isEmpty(){ return root==null;} /** * 中序遍歷 */ public void LDRtraverse(){ System.out.print("中序遍歷,使用遞迴實現:"); this.LDRtraverse(root); System.out.println(); } private void LDRtraverse(Node root){ if(root!=null){ //1、遍歷左子樹 this.LDRtraverse(root.leftChild); //2、輸出根 System.out.print(root.value+" "); //3、遍歷右子樹 this.LDRtraverse(root.rightChild); } } /** * 中序遍歷二叉樹,使用棧(後進先出)實現。 */ public void LDRtraverseByStack(){ System.out.print("中序遍歷,使用棧實現:"); //1、建立棧 Deque<Node> stack = new LinkedList<>(); //2、獲取根節點 Node current = root; //若根節點不為空,或者棧不為空則進入迴圈 while ( current!=null || !stack.isEmpty()){ //如果不為空節點,則入棧,而後判斷左子節點是否存在,存在則入棧,最終左下角的元素在棧首 while (current!=null){ stack.push(current); current = current.leftChild; }
//如果棧不為空,則出棧一個(即左下角元素),節點指向出棧的右子節點 if(!stack.isEmpty()){ current = stack.pop(); System.out.print(current.value+" "); current = current.rightChild; } } System.out.println(); } /** * 獲取二叉樹的高度 * @return */ public int getHeight(){ return this.getHeight(root); } private int getHeight(Node root){ if(root==null){ return 0; }else{ //1、獲取左子樹的高度 int nl = this.getHeight(root.leftChild); //2、獲取右子樹的高度 int nr = this.getHeight(root.rightChild); //3、其中大的高度,加一,獲取 return nl>nr?nl+1:nr+1; } } /** * 獲取二叉樹內節點個數 * @return */ public int size(){ return this.size(root); } private int size(Node root){ if(root==null){ return 0; }else { //1、獲取左子樹的結點個數 int nl = this.size(root.leftChild); //2、獲取右子樹的結點個數 int nr = this.size(root.rightChild); //3、返回個數 return nl+nr+1; } } /** * 在樹中尋找對應的元素 * @param value * @return */ public Node findKey(Object value){ return this.findKey(value,root); } private Node findKey(Object value,Node root){ if(root==null){//遞迴頭1:為空樹 return null; }else if(root!=null&&root.value==value){ //遞迴頭2:找到了目標元素 return root; }else {//遞迴體 //遍歷左子樹 Node node1 = this.findKey(value,root.leftChild); //遍歷右子樹 Node node2 = this.findKey(value,root.rightChild); if(node1!=null&&node1.value==value){ return node1; }else if(node2!=null&&node2.value==value){ return node2; }else {//沒找到 return null; } } } /** * 按照層次遍歷二叉樹,使用佇列(先進先出)實現。 */ public void levelOrderByStack(){ System.out.print("按照層次遍歷二叉樹,使用佇列實現:"); if(root==null)return; //1、建立佇列 Queue<Node> queue = new LinkedList<>(); //2、根節點入隊 queue.add(root); //佇列中有元素則進入迴圈 while (queue.size()!=0){ //按照棧中元素的個數進行出隊迴圈 int len = queue.size(); for(int i = 0;i<len;i++){ //根節點出隊 Node temp = queue.poll(); System.out.print(temp.value+" "); //如果根節點有左子節點,左子節點入隊 if(temp.leftChild!=null) queue.add(temp.leftChild); //如果根節點有左子節點,左子節點入隊 if(temp.rightChild!=null) queue.add(temp.rightChild); } } System.out.println(); } }
3、測試程式碼:
/** * @author lurenjia * @date 2022/12/7-12:23 */ public class User { public static void main(String[] args) { //手動建立節點 Node node7 = new Node("G",null,null); Node node4 = new Node("D",null,null); Node node5 = new Node("E",node7,null); Node node6 = new Node("F",null,null); Node node2 = new Node("B",node4,node5); Node node3 = new Node("C",null,node6); Node node1 = new Node("A",node2,node3); //通過根節點建立一個二叉樹物件 BinaryTree btree = new BinaryTree(node1); //判斷是否為空 System.out.println("是否為空:"+btree.isEmpty()); //獲取高度 System.out.println("此二叉樹的高度為:"+btree.getHeight()); //獲取節點個數 System.out.println("此二叉樹的節點個數為:"+btree.size()); //中序遍歷,遞迴實現 btree.LDRtraverse(); //中序遍歷, btree.LDRtraverseByStack(); //按照層次遍歷 btree.levelOrderByStack(); //查詢指定元素 System.out.println("查詢:"+btree.findKey("E")); } }
測試程式碼中建立的二叉樹:
測試結果:
是否為空:false 此二叉樹的高度為:4 此二叉樹的節點個數為:7 中序遍歷,使用遞迴實現:D B G E A C F 中序遍歷,使用棧實現:D B G E A C F 按照層次遍歷二叉樹,使用佇列實現:A B C D E F G 查詢:Node{value=E, leftChild=Node{value=G, leftChild=null, rightChild=null}, rightChild=null} Process finished with exit code 0