二叉樹的入門學習(基礎篇)

二叉樹的最基礎學習

最近入職一家銀行外包公司,不僅外包公司需要面試,銀行本身也需要面試,聽說考java基礎和數據結構與算法,正好我一直想好好學習數據結構與算法,藉此機會補充學習一下。

一、基本概念

1.1 專業術語

  • 節點的度:子樹的個數
  • 樹的度:一個樹所有節點的度的最大值
  • 路徑長度:從節點n1到nk的路徑長度就是,包含邊的個數
  • 樹的高度/深度:就是層數

1.2 二叉樹的基本

  • 特殊二叉樹
    • 斜二叉樹:都只有左兒子或者都只有右兒子
    • 完美二叉樹:也叫滿二叉樹,左右兒子都有,而且最後一層是平齊的
    • 完全二叉樹:從上到下,從左到右編號中間不能斷,那麼就是完全二叉樹
  • 二叉樹的存儲方式
    • 順序存儲:就是完全二叉樹的形式,數組的方式存儲
    • 鏈式存儲:我們後面說這個

二、實現二叉樹(鏈式)

  • 寫一個最小節點TreeNode
public class TreeNode{
    private int value;
    private TreeNode lNode;
    private TreeNode rNode;
    
    public TreeNode(int value){
        this.value = value;
    }
    
    //get set 方法:
}
  • 寫一個樹類
public class BinaryTree{
    private TreeNode root;
    
    public BinaryTree(TreeNode root){
        this.root = root;
    }
}
  • 寫一個main的方法進行測試
public class TestBinaryTree{
    public static void main(String[] args){
        TreeNode root = new TreeNode(1);
        BinaryTree binaryTree = new BinaryTree(root);
        //這裏我們生成一個簡單二叉樹
        TreeNode l1 = new TreeNode(2);
	    TreeNode l2 = new TreeNode(3);
		
		root.setlNode(l1);
		root.setrNode(l2);
		
		TreeNode l4 = new TreeNode(4);
		TreeNode l5 = new TreeNode(5);
		
		TreeNode l6 = new TreeNode(6);
		TreeNode l7 = new TreeNode(7);
		
		l1.setlNode(l4);
		l1.setrNode(l5);
		
		l2.setlNode(l6);
		l2.setrNode(l7);
    }
}

這個二叉樹如圖:順便注意右側的東東,分別是前序、中序、後序、遍歷的結果
在這裏插入圖片描述

三、二叉樹的遍歷

二叉樹的遍歷有三種,可以參考上圖

  • 代碼實現:我們在節點上編寫主要方法
public class TreeNode{
    private int value;
    private TreeNode lNode;
    private TreeNode rNode;
    
    public TreeNode(int value){
        this.value = value;
    }
    
    //get set 方法:
    
    //前序遍歷
	public void frontShow() {
		System.out.println(this.value);
		if (this.lNode != null) {
			this.lNode.frontShow();
		}
		if (this.rNode != null) {
			this.rNode.frontShow();
		}
	}
	//中序遍歷
	public void midShow() {
		if (this.lNode != null) {
			this.lNode.midShow();
		}
		System.out.println(this.value);
		if (this.rNode != null) {
			this.rNode.midShow();
		}
	}
    
    //後序遍歷
    public void backShow() {
		if (this.rNode != null) {
			this.rNode.backShow();
		}
		System.out.println(this.value);
		if (this.lNode != null) {
			this.lNode.backShow();
		}
	}

}

四、二叉樹的查找

也是分爲三種,對應遍歷,但是其實並不好寫

public class TreeNode{
    private int value;
    private TreeNode lNode;
    private TreeNode rNode;
    
    public TreeNode(int value){
        this.value = value;
    }
    
    //get set 方法:
	//前序查找
    public TreeNode frontFind(int a ){
        TreeNode target = null;
        //先看看自己
        if(this.value == a){
			return this; 
        }else {
            //然後看看左兒子
            if(this.lNode != null){
				target = this.lNode.frontFind(a);
            }
            //判斷左兒子的值
            if(target != null){
                return target;
            }
            //左兒子不行,然後看看右兒子
            if (this.rNode != null) {
				target = rNode.frontFind(a);
			}
            //判斷右兒子行不行
			if (target != null) {
				return target;
			}
        }
        return target;
    }

}

結語:下面繼續學習更厲害的二叉樹,比如紅黑樹什麼的。。。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章