Java实现二叉树——增删改查

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Lin_wj1995/article/details/82785128

今天心血来潮,突然想写个二叉树的类,哈哈~

功能代码如下:

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);
    }
}

猜你喜欢

转载自blog.csdn.net/Lin_wj1995/article/details/82785128