1. 程式人生 > >Java實現二叉樹——增刪改查

Java實現二叉樹——增刪改查

今天心血來潮,突然想寫個二叉樹的類,哈哈~

功能程式碼如下:

package tree;

/**
 * 傻逼tree
 * @author lwj
 * @date 2018/9/20
 */
public class MyTree<K extends Comparable, V>{
    TreeNode root;

    /**
     * 插入操作
     * @param key
     * @param value
     */
    public void put(K key, V value){
        if (root ==
null){ root = new TreeNode<K, V>(key, value); return; } TreeNode x = root; while (x != null){ if (x.key == key){ TreeNode node = getNode(key); node.value = value; return; }else
if (key.compareTo(x.key) < 0){ if (x.left != null){ x = x.left; }else { TreeNode node = new TreeNode(key, value); node.parent = x; x.left = node; return; }
}else if (key.compareTo(x.key) > 0){ if (x.right != null){ x = x.right; }else { TreeNode node = new TreeNode(key, value); node.parent = x; x.right = node; return; } } } } /** * 刪除節點 * @param key * @return */ public void remove(K key){ TreeNode<K, V> node = getNode(key); if (node == null){ return; } TreeNode<K, V> replication; if (node.left!=null && node.right!=null){ replication = node.right; while (replication.left != null){ replication = replication.left; } node.key = replication.key; node.value = replication.value; node = replication; } if (node.parent.left == node){ if (node.left != null){ node.left.parent = node.parent; node.parent.left = node.left; }else if(node.right != null){ node.right.parent = node.parent; node.parent.left = node.right; }else { node.parent.left = null; } }else { if (node.left != null){ node.left.parent = node.parent; node.parent.right = node.left; }else if(node.right != null){ node.right.parent = node.parent; node.parent.right = node.right; }else { node.parent.right = null; } } node.left = node.right = node.parent = null; } /** * 通過key得到TreeNode * @param key * @return */ TreeNode<K, V> getNode(K key){ TreeNode<K, V> t = root; while (t != null){ if (t.key == key){ return t; }else if (key.compareTo(t.key) < 0){ t = t.left; }else { t = t.right; } } return null; } /** * 通過key得到value * @param key * @return */ public V get(K key){ TreeNode<K, V> node = getNode(key); if (node == null){ return null; }else { return node.value; } } /** * 列印 * @param type 1:前序 * 2:中序 * 3:後序 */ public void print(int type){ switch (type){ case 1 : System.out.print("前序遍歷:");beforePrK(root);System.out.println();return; case 2 : System.out.print("中序遍歷:");midPrK(root);System.out.println();return; case 3 : System.out.print("後序遍歷:");afterPrK(root);System.out.println();return; default : System.out.println();return; } } /** * 前序遍歷 * @param x */ private void beforePrK(TreeNode x){ if (x == null){ return; } System.out.print(x.value); beforePrK(x.left); beforePrK(x.right); } /** * 中序遍歷 * @param x */ private void midPrK(TreeNode x){ if (x == null){ return; } midPrK(x.left); System.out.print(x.value); midPrK(x.right); } /** * 後序遍歷 * @param x */ private void afterPrK(TreeNode x){ if (x == null){ return; } afterPrK(x.left); afterPrK(x.right); System.out.print(x.value); } class TreeNode<K, V>{ K key; V value; TreeNode parent; TreeNode left; TreeNode right; public TreeNode(K key, V value) { this.key = key; this.value = value; } } }

下面我們想生成如圖所示的二叉樹:
在這裡插入圖片描述

ok,寫個測試類來生成上圖的樹,然後測試一下功能:

import org.junit.Before;
import org.junit.Test;
import tree.MyTree;

/**
 * @author lwj
 * @date 2018/9/20
 */
public class MyTreeTest {
    MyTree<Integer, String> tree;
    @Before
    public void init(){
        tree = new MyTree();
        tree.put(7, "A");
        tree.put(4, "B");
        tree.put(10,"C");
        tree.put(2, "D");
        tree.put(5, "E");
        tree.put(8, "F");
        tree.put(11,"G");
        tree.put(1, "H");
        tree.put(3, "I");
        tree.put(6, "J");
        tree.put(9, "K");
    }

    @Test
    public void remove(){
        tree.print(2);
        tree.remove(3);
        tree.print(2);
    }

    @Test
    public void print(){
        tree.print(1);
        tree.print(2);
        tree.print(3);
    }
    
    @Test
    public void get(){
        tree.get(4);
    }
}