二叉樹演算法(java)

來源:互聯網
上載者:User

標籤:演算法   資料結構   二叉樹   java   

為什麼實用二叉樹一,在有序數組中插入刪除資料太慢     1插入或者刪除一條資料會移動後面的所有資料  二,在鏈表中尋找資料太慢    2尋找只能從頭或者尾部一條一條的找 用樹解決問題     有沒有一種插入和刪除像鏈表那麼快,查詢可以向有序數組一樣查得快那樣就好了。 數實現了這些特點,稱為了最有意思的資料結構之一 樹的術語
樹分平衡樹和非平衡樹
二叉樹的類
public class Tree {/** * 跟節點 */private Node root;/** * 構造方法 */public Tree() {}/** * 構造方法 *  * @param root *            跟節點 */public Tree(Node root) {this.root = root;}}class Node {    /* key */    int key;    /* 值 */    Object value;    /* 左節點 */    Node leftChildNode;    /* 右節點 */    Node rightChildNode;    /**     * 構造方法     *      * @param key     *            關鍵字     * @param value     *            值     */    public Node(int key, Object value) {        super();        this.key = key;        this.value = value;    }}


二叉樹插入功能
/** * 插入節點 *  * @param key *            key * @param value *            值 */public void insert(int key, Object value) {Node node = new Node(key, value);if (this.root == null) {this.root = node;} else {Node currentNode = this.root;while (true) {if (key > currentNode.key) {if (currentNode.rightChildNode == null) {currentNode.rightChildNode = node;return;} else {currentNode = currentNode.rightChildNode;}} else {if (currentNode.leftChildNode == null) {currentNode.leftChildNode = node;return;} else {currentNode = currentNode.leftChildNode;}}}}}


二叉樹的尋找功能
/** * 尋找節點 *  * @param key * @return */public Node find(int key) {if (this.root != null) {Node currentNode = this.root;while (currentNode.key != key) {if (key > currentNode.key) {currentNode = currentNode.rightChildNode;} else {currentNode = currentNode.leftChildNode;}if (currentNode == null) {return null;}}}return null;}


二叉樹的展示功能(中序遍曆)

private void show(Node node) {if (node != null) {this.show(node.leftChildNode);System.out.println(node.key + ":" + node.value);this.show(node.rightChildNode);}}

測試
public static void main(String[] args) {Node root = new Node(50, 24);Tree tree = new Tree(root);tree.insert(20, 530);tree.insert(540, 520);tree.insert(4, 540);tree.insert(0, 550);tree.insert(8, 520);tree.show();}


完整代碼
package tree;/** * 二叉樹 *  * @author JYC506 *  */public class Tree {/** * 跟節點 */private Node root;/** * 構造方法 */public Tree() {}/** * 構造方法 *  * @param root *            跟節點 */public Tree(Node root) {this.root = root;}/** * 尋找節點 *  * @param key * @return */public Node find(int key) {if (this.root != null) {Node currentNode = this.root;while (currentNode.key != key) {if (key > currentNode.key) {currentNode = currentNode.rightChildNode;} else {currentNode = currentNode.leftChildNode;}if (currentNode == null) {return null;}}}return null;}/** * 插入節點 *  * @param key *            key * @param value *            值 */public void insert(int key, Object value) {Node node = new Node(key, value);if (this.root == null) {this.root = node;} else {Node currentNode = this.root;while (true) {if (key > currentNode.key) {if (currentNode.rightChildNode == null) {currentNode.rightChildNode = node;return;} else {currentNode = currentNode.rightChildNode;}} else {if (currentNode.leftChildNode == null) {currentNode.leftChildNode = node;return;} else {currentNode = currentNode.leftChildNode;}}}}}/** * 展示 */public void show() {this.show(root);}/** * 中序遍曆 *  * @param node */private void show(Node node) {if (node != null) {this.show(node.leftChildNode);System.out.println(node.key + ":" + node.value);this.show(node.rightChildNode);}}public static void main(String[] args) {Node root = new Node(50, 24);Tree tree = new Tree(root);tree.insert(20, 530);tree.insert(540, 520);tree.insert(4, 540);tree.insert(0, 550);tree.insert(8, 520);tree.show();}}class Node {/* key */int key;/* 值 */Object value;/* 左節點 */Node leftChildNode;/* 右節點 */Node rightChildNode;/** * 構造方法 *  * @param key *            關鍵字 * @param value *            值 */public Node(int key, Object value) {super();this.key = key;this.value = value;}}


二叉樹演算法(java)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.