数据结构-二叉树

  一个简单的二叉树:实现功能 添加, 删除, 前序,中序,后序遍历

package com.xhga.common;

import java.util.ArrayList;
import java.util.List;

/**
 * 二叉树
 * Created by HuaWeiBo on 2019/3/24.
 */
public class TwoTree {
    private Object value;
    private TwoTree leftTree;
    private TwoTree rightTree;

    public TwoTree() {
        this.leftTree = null;
        this.rightTree = null;
    }

    public TwoTree(Object value) {
        this.value = value;
        this.leftTree = null;
        this.rightTree = null;
    }

    /**
     * 前序遍历
     * @return
     */
    public List getTwoTreeBefore() {
        return getSonTwoTree(new ArrayList(), 1);
    }

    /**
     * 中序遍历
     * @return
     */
    public List getTwoTreeCentre() {
        return getSonTwoTree(new ArrayList(), 2);
    }

    /**
     * 后序遍历
     * @return
     */
    public List getTwoTreeAfter() {
        return getSonTwoTree(new ArrayList(), 3);
    }

    /**
     * 前中序遍历的区别在于左,根,右节点的位置
     * 说白了根节点在左,右节点的哪个位置
     * @param list
     * @param index
     * @return
     */
    private List getSonTwoTree(List list, int index) {
        if (value == null) {
            return list;
        }
        if (index == 1) {
            list.add(value);
        }
        if (leftTree != null) {
            leftTree.getSonTwoTree(list, index);
        }
        if (index == 2) {
            list.add(value);
        }
        if (rightTree != null) {
            rightTree.getSonTwoTree(list, index);
        }
        if (index == 3) {
            list.add(value);
        }
        return list;
    }


    public int insert(Object value) {
        if (this.value == null) {
            this.value = value;
            return 0;
        }
        int codeSon = value.hashCode();
        // 遍历左树->直到左节点为空,直接new TwoTree
        if (this.value.hashCode() > codeSon) {
            if (leftTree == null) {
                this.leftTree = new TwoTree(value);
                return 1;
            }
            return this.leftTree.insert(value);
        } else if (this.value.hashCode() < codeSon) {
            // 遍历右树->直到右节点为空,直接new TwoTree
            if (rightTree == null) {
                this.rightTree = new TwoTree(value);
                return 1;
            }
            return this.rightTree.insert(value);
        }
        return 0;
    }

    public void delete(Object value) {
        if (value == null || this.value == null) {
            return;
        }
        if (this.value == value) {
            // 查询后继节点(比自己大的最小节点)
            TwoTree successor = getSuccessor(this.rightTree);
            this.value = successor.value;
            this.rightTree.deleteValue(this, successor.value);
            return;
        }
        deleteValue(null, value);
    }
    private void deleteValue(TwoTree parent, Object value){
        // 删除value值 -> 左/右节点
        if (! this.value.equals(value)) {
            if (this.value.hashCode() > value.hashCode()) {
                this.leftTree.deleteValue(this, value);
            } else {
                this.rightTree.deleteValue(this, value);
            }
            return;
        }
        // 1.到此时, this.value = value
        if (this.leftTree == null || this.rightTree == null) {
            // 代替删除节点在父节点的TwoTree对象
            TwoTree parentSonTree = this.leftTree;
            // 删除节点的左右子节点存在为空,则直接将另外一个子节点接在父节点上
            if (this.leftTree == null) {
                parentSonTree = this.rightTree;
            }
            // 判断是在父类左节点还是右节点
            if (parent.value.hashCode() > value.hashCode()) {
                parent.leftTree = parentSonTree;
            } else {
                parent.rightTree = parentSonTree;
            }
            return;
        }
        // 2.左右节点都不为空,查询后继节点(比自己大的最小节点)代替删除值
        TwoTree successor = getSuccessor(this.rightTree);
        this.value = successor.value;
        this.rightTree.deleteValue(this, successor.value);
    }

    private TwoTree getSuccessor(TwoTree twoTree){
        TwoTree leftTree = twoTree.leftTree;
        if (leftTree == null) {
            return twoTree;
        }
        return getSuccessor(leftTree);
    }
    @Override
    public String toString() {
        return "TwoTree{" +
                "value=" + value +
                ", leftTree=" + leftTree +
                ", rightTree=" + rightTree +
                '}';
    }
}

测试类:

package com.tree;

import java.util.Random;

/**
 * Created by HuaWeiBo on 2019/3/24.
 */
public class TwoTreeTest {
    public static void main(String[] args) {
        TwoTree twoTree = new TwoTree();
        int flag = 0;
        for (int i = 0; i < 10; i++) {
            Random random = new Random();
            int i2 = random.nextInt(100);
            if (i == 5) {
                flag = i2;
            }
            twoTree.insert(i2);
        }

        System.out.println("树结构:" + twoTree);
        System.out.println("遍历全部数据:" + twoTree.getTwoTreeBefore());
        System.out.println("删除的值:" + flag);
        twoTree.delete(flag);
        System.out.println("遍历删除后的:" + twoTree.getTwoTreeBefore());
    }
}

若有理解不到位的地方希望大家能够指点一下,也避免让别人犯错。非常感谢!!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值