1. 程式人生 > >Java 二叉搜索樹 實現和學習

Java 二叉搜索樹 實現和學習

++ 目標 -c 初始 處理 找到 node classname ()

/**
 * <html>
 * <body>
 *  <P> Copyright 1994 JsonInternational</p>
 *  <p> All rights reserved.  - https://github.com/Jasonandy/Java-Core-Advanced </p>
 *  <p> Created by Jason</p>
 *  </body>
 * </html>
 */
package cn.ucaner.datastructure.BinarySearchTree;

/** * @Package:cn.ucaner.datastructure.BinarySearchTree * @ClassName:BinarySearchTree * @Description: <p> 樹集合了數組(查找速度快)和鏈表(插入、刪除速度快)的優點 </br> CSDN {@link https://blog.csdn.net/a19881029/article/details/24379339} </p> * 二叉樹是一種特殊的樹,即:樹中的每個節點最多只能有兩個子節點 * 二叉搜索樹是一種特殊的二叉樹,即:節點的左子節點的值都小於這個節點的值,節點的右子節點的值都大於等於這個節點的值 * Tips:如果樹中允許存在重復數據,處理起來比較麻煩,故實現中不允許樹中存在重復數據,即節點的右子節點的值必須大於節點的值. * 搜索二叉樹有一個特點,即如果使用中序遍歷遍歷搜索二叉樹,將得到包含搜索二叉樹中所有節點值的升序排序結果 * @Author: - Jason * @CreatTime:2018年4月7日 下午8:37:11 * @Modify By: * @ModifyTime: 2018年4月7日 * @Modify marker: *
@version V1.0 */ public class BinarySearchTree { private TreeNode root;//定義樹的根結點 /** * BinarySearchTree. 根據已知序列構建二叉搜索樹 * @param input */ public BinarySearchTree(int[] input) { createBinarySearchTree(input); } /** * @Description: 根據已知序列構建二叉搜索樹 *
@param input void * @Autor:Jason - [email protected] */ public void createBinarySearchTree(int[] input) { if (input != null) { for (int i = 0; i < input.length; i++) { root = insert(input[i], root); } } } /** * @Description: 二叉搜索樹的搜索算法,遞歸算法 * @param target 目標值 * @param root 二叉搜索樹的根結點 * @return TreeNode * @Autor: Jason - [email protected] */ public TreeNode search(int target, TreeNode root) { TreeNode result = null; if (root != null) { // 遞歸終止條件 if (target == root.data) { // 遞歸終止條件 result = root; return result; } else if (target < root.data) { // 目標值小於根結點值,從左子樹查找 result = search(target, root.left); } else { // 目標值大於根結點值,從右子樹查找 result = search(target, root.right); } } return result; } /** * @Description: 二叉搜索樹的插入操作 * @param target * @param node * @return TreeNode * @Autor: Jason - [email protected] */ public TreeNode insert(int target, TreeNode node) { if (search(target, node) == null) { if (node == null) { return new TreeNode(target); } else { if (target < node.data) { node.left = insert(target, node.left); } else { node.right = insert(target, node.right); } } } return node; } /** * @Description: 刪除搜索二叉樹的制定結點 * @param target * @param node * @return TreeNode * @Autor: jason - [email protected] */ public TreeNode remove(int target, TreeNode node) { TreeNode tmp = null; if (node != null) { if (target < node.data) { // 從左子樹刪除 node.left = remove(target, node.left); } else if (target > node.data) { // 從右子樹刪除 node.right = remove(target, node.right); } else if (node.left != null && node.right != null) { // 找到待刪除結點,且其左右子樹不為空 // 找到以待刪除結點右子樹的中序遍歷第一個結點(最小結點) tmp = node.right; while (tmp.left != null) { tmp = tmp.left; } // 用最小結點補位待刪除結點 node.data = tmp.data; // 刪除待刪除結點右子樹上補位結點 node.right = remove(node.data, node.right); } else { if (node.left == null) { node = node.right; } else { node = node.left; } } } return node; } /** * @Description: 中序遍歷二叉搜索樹,遞歸算法,升序排序 * @param node void * @Autor: Jason - [email protected] */ public void inOrder(TreeNode node) { if (node != null) { inOrder(node.left); System.out.print(root.data + " "); inOrder(node.right); } } /** * @Description: 打印二叉搜索樹 * @param node void * @Autor:jason - [email protected] */ public void printTree(TreeNode node) { if (node != null) { System.out.print(node.data); if (node.left != null || node.right != null) { System.out.print("("); printTree(node.left); System.out.print(","); printTree(node.right); System.out.print(")"); } } } /** * @Description: 訪問二叉搜索樹的根結點 * @return TreeNode * @Autor:Jason - [email protected] */ public TreeNode getRoot() { return root; } }

/**
 * <html>
 * <body>
 *  <P> Copyright 1994 JsonInternational</p>
 *  <p> All rights reserved.  - https://github.com/Jasonandy/Java-Core-Advanced </p>
 *  <p> Created by Jason</p>
 *  </body>
 * </html>
 */
package cn.ucaner.datastructure.BinarySearchTree;

/**
* @Package:cn.ucaner.datastructure.BinarySearchTree   
* @ClassName:TreeNode   
* @Description:   <p> Node節點</p>
* @Author: - Jason   
* @CreatTime:2018年4月7日 下午8:41:02   
* @Modify By:   
* @ModifyTime:  2018年4月7日
* @Modify marker:   
* @version    V1.0
 */
public class TreeNode {

    /**
     *結點的數據項   
     */
    public int data;
    
    /**
     * 結點指向左孩子的引用  
     */
    public TreeNode left; 
    
    /**
     * 結點指向右孩子的引用
     */
    public TreeNode right;
    
    
    /**
    * TreeNode.  構造方法,初始化結點數據項 
    * @param data
     */
    public TreeNode(int data){
        this.data = data;
    }

    @Override
    public String toString() {
        return "TreeNode [data=" + data + "]";
    }
    
}

Java 二叉搜索樹 實現和學習