简单的顺序二叉树,实现增删改查

简单的顺序二叉树,实现增删改查

package TreeDemos;

import java.util.*;

public class TreeDemo<K extends Comparable<K>,V > {
//    建立排序二叉树,并实现增删改查功能

    //    孩子表示法
    public static class Node<K extends Comparable<K>, V> {
        public K key;
        public V value;
        public Node<K, V> lchlid;
        public Node<K, V> rchlid;

        public Node(K key, V value, Node<K, V> lchlid, Node<K, V> rchlid) {
            this.key = key;
            this.value = value;
            this.lchlid = lchlid;
            this.rchlid = rchlid;
        }

        public void put(Node<K, V> newNode) {
            if (newNode.key.compareTo(this.key) < 0) {
                if (lchlid != null) {
                    lchlid.put(newNode);
                } else {
                    lchlid = newNode;
                }
            } else if (newNode.key.compareTo(this.key) > 0) {
                if (rchlid != null) {
                    rchlid.put(newNode);
                } else {
                    rchlid = newNode;
                }
            } else {
                value = newNode.value;
            }
        }

        public ArrayList<Node<K, V>> nodes = new ArrayList<Node<K, V>>();

        public void traversal(Node<K, V> root){
//            将遍历结果保存到ArrayList中
            if(lchlid!=null){
                lchlid.traversal(root);
            }
            root.nodes.add(this);
            if(rchlid!=null){
                rchlid.traversal(root);
            }
        }

        public Node<K, V> find(K key) {
            if (key.compareTo(this.key) < 0 && lchlid != null) {
                return lchlid.find(key);
            } else if (key.compareTo(this.key) > 0 && rchlid != null) {
                return rchlid.find(key);
            } else if (key.compareTo(this.key) == 0) {
                return this;
            } else {
                return null;
            }
        }

        public void delete(K key, Node<K, V> parent,Node<K, V> root) {
            if (key.compareTo(this.key) < 0 && lchlid != null) {
                lchlid.delete(key, this ,root);
            } else if (key.compareTo(this.key) > 0 && rchlid != null) {
                rchlid.delete(key, this,root);
            } else if (key.compareTo(this.key) == 0) {
                delete1(parent,root);
            } else {
                System.out.println("此节点不存在");
            }
        }

        public Node<K,V> getPre(){
            Node<K,V> l=lchlid;
            while (l.rchlid!=null){
                l=l.rchlid;
            }
            return l;
        }

        public void delete1(Node<K, V> parent,Node<K, V> root){
            //        分为三种情况  第一种:叶子节点,直接删除; 第二种:只含有一个子节点,继承 ,第三种:含有两个节点
            if (lchlid == null && rchlid == null) {
                if (parent.lchlid == this) {
                    parent.lchlid = null;
                }
                if (parent.rchlid == this) {
                    parent.rchlid = null;
                }
            } else if (lchlid != null && rchlid == null) {
                value = lchlid.value;
                this.key=lchlid.key;
                lchlid = null;
            } else if (lchlid == null) {
                value = rchlid.value;
                this.key=rchlid.key;
                rchlid = null;
            } else {
                Node<K,V> pre=getPre();
                root.delete(pre.key,null,root);
                this.value=pre.value;
                this.key=pre.key;
            }
        }

        public String getString() {
            return "[" + key + ":" + value + "]";
        }

        public String toString() {
            StringBuilder stringBuilder = new StringBuilder();
            if (lchlid != null) {
                stringBuilder.append(lchlid.toString());
            }
            stringBuilder.append(getString());
            if (rchlid != null) {
                stringBuilder.append(rchlid.toString());
            }
            return stringBuilder.toString();
        }
    }


    public Node<K,V> root;


    public TreeDemo(ArrayList<Node<K,V>> nodes){
        for(Node<K,V> node :nodes){
            put(node);
        }
    }

    public void put(Node<K,V> node){
        if(root==null){
            root=node;
        }
        else{
            root.put(node);
        }
    }

    public Node<K,V> find(K key){
        return root.find(key);
    }


    public void delete(K key){
        root.delete(key,null,root);

    }

    public String toString(){
//        中序遍历
        return root.toString();
    }

    public static void main(String[] args) {
        ArrayList<Node<Integer,String>> nodes=new ArrayList<Node<Integer,String>>();
        nodes.add(new Node<Integer,String>(324,"A",null,null));
        nodes.add(new Node<Integer,String>(83,"B",null,null));
        nodes.add(new Node<Integer,String>(87,"J",null,null));
        nodes.add(new Node<Integer,String>(5432,"F",null,null));
        nodes.add(new Node<Integer,String>(34,"G",null,null));
        nodes.add(new Node<Integer,String>(44,"K",null,null));
        nodes.add(new Node<Integer,String>(56,"K",null,null));
        nodes.add(new Node<Integer,String>(754,"K",null,null));
        nodes.add(new Node<Integer,String>(45,"K",null,null));


        TreeDemo<Integer,String> myTree=new TreeDemo<>(nodes);
        System.out.println(myTree);

        Node<Integer,String> res= myTree.find(56);
        System.out.println(res.getString());

        myTree.delete(56);
        System.out.println(myTree);




    }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值