二叉树相关

import java.util.Arrays;
import java.util.Comparator;

class BinaryTree<T extends Comparable<T>> {
    private class Node {
        private Comparable<T> data;

        private Node parent;

        private Node left;

        private Node right;

        public Node(Comparable<T> data) {
            this.data = data;
        }

        /**
         * 实现节点数据的适当位置的存储
         */

        public void addNode(Node newNode) {
            if (newNode.data.compareTo((T)this.data) <= 0) {
                if (this.left == null) {
                    this.left = newNode;
                    newNode.parent = this;
                } else {
                    this.left.addNode(newNode);
                }
            } else {
                if (this.right == null) {
                    this.right = newNode;
                    newNode.parent = this;
                } else {
                    this.right.addNode(newNode);
                }
            }
        }

        /**
         * 实现所有数据的获取处理,按照中序遍历的形式来完成
         */

        public void toArrayNode() {
            if (this.left != null) {
                this.left.toArrayNode();
            }

            BinaryTree.this.returnData[BinaryTree.this.foot++] = this.data;

            if (this.right != null) {
                this.right.toArrayNode();
            }
        }

        //进行数据检索
        public boolean containsNode(Comparable<T> data) {
            if (data.compareTo((T) this.data) == 0) {
                return true;
            } else if (data.compareTo((T) this.data) < 0) {
                if (this.left != null) {
                    return this.left.containsNode(data);
                } else {
                    return false;
                }
            } else {
                if (this.right != null) {
                    return this.right.containsNode(data);
                } else {
                    return false;
                }
            }
        }
    }

    private Node root;
    private int count ;
    private Object[] returnData;
    private int foot = 0;

    public void add(Comparable<T> data) {
        if (data == null) {
            throw new NullPointerException("保存数据为空时抛出的异常");
        }

        Node newNode = new Node(data);
        if (this.root == null) {// 根节点不存在
            this.root = newNode; // 第一个节点作为根节点
        } else {
            this.root.addNode(newNode); //交由Node类处理
        }
        this.count++;
    }

    public Object[] toArray() {
        if (this.count == 0) {
            return null;
        }
        this.returnData = new Object[this.count];
        this.foot = 0;
        this.root.toArrayNode();
        return this.returnData;
    }

    //追加数据查询方法
    public boolean contains(Comparable<T> data) {
        if(this.count == 0) {
            return false;
        }
        return this.root.containsNode(data);
    }
}


class Member1 implements Comparable<Member1>{
    private String name;

    private int age;

    public  Member1(String name, int age) {
        this.name = name;

        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Member{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Member1 o) {
        return this.age - o.age;
    }
}
public class ArrayDemo {
    public static void main(String[] args) throws Exception {
        BinaryTree<Member1> member1BinaryTree = new BinaryTree<>();

        member1BinaryTree.add(new Member1("李兴华",18));
        member1BinaryTree.add(new Member1("魔乐科技",50));
        member1BinaryTree.add(new Member1("小李老师",23));

        System.out.println(Arrays.toString(member1BinaryTree.toArray()));
    }
}

你可能感兴趣的:(java,算法,开发语言)