比较器 二叉树实现(BinaryTree)

与链表不同的是,树的最大特征是可以针对数据进行排序。(二叉排序树)

二叉排序树的操作原理选择第一个数据作为根节点,而后比根节点小的放在左子树,大的放在右子树,取得的时候按照中序遍历方式取出(左,中,右)。
在任何数据结构里面Node类的核心功能是保存真实数据,以及配置节点关系。

范例:实现二叉排序树:

package TestDemo;


import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;



class Book implements Comparable<Book>{
    private String title;
    private double price;
    public Book(String title,double price){
        this.title=title;
        this.price=price;
    }
    @Override
    public String toString() {
        return "BookName:"+this.title+"price:"+this.price;
    }

    @Override
    public int compareTo(Book o) {//此方法 Arrays.sort()会自动调用。
        if(this.price>o.price){
            return 1;
        }else if(this.price<o.price){
            return -1;
        }else{
            return 0;
        }
    }
}

@SuppressWarnings("rawtypes")
class BinaryTree{
    private class Node{//定义节点类型
        private Comparable data;//排序的依据
        private Node left;//保存左节点
        private Node right;//保存右节点
        @SuppressWarnings("unused")
        public Node(Comparable data){//这里用于节点初始化,可以传入Comparable接口或其子类
            this.data=data;
        }
        @SuppressWarnings("unchecked")
        public void addNode(Node newNode){//二叉树节点增加变成节点自己增加
            if(this.data.compareTo(newNode.data)<0){//如果新增节点比当前节点值小
                if(this.left==null){
                    this.left=newNode;
                }else{
                    this.left.addNode(newNode);//递归
                }
            }else{//新节点比当前节点大
                if(this.right==null){
                    this.right=newNode;
                }else{
                    this.right.addNode(newNode);
                }
            }
        }

        public void toArrayNode(){//中序遍历
            if(this.left!=null){
                this.left.toArrayNode();
            }
            BinaryTree.this.retData[BinaryTree.this.foot++]=this.data;
            if(this.right!=null){
                this.right.toArrayNode();//右子树输出
            }
        }
    }

    private Node root;//定义二叉树根节点
    private int count;//保存元素个数

    private Object[] retData;//中序遍历出的对象数组
    private int foot;//角标,中序遍历使用
    public void add(Object obj){//实现二叉树数据的增加
            Comparable com=(Comparable)obj;
            Node newNode=new Node(com);//创建新节点
            if(this.root==null){
                this.root=newNode;
            }else{
                this.root.addNode(newNode);//交给节点处理
            }
            this.count++;
    }

    public Object[] toArray(){//中序遍历函数
        if(this.root==null){
            return null;
        }
        this.foot=0;
        this.retData=new Object[this.count];
        this.root.toArrayNode();//交给节点处理
        return this.retData;
    }
}


public class TestDemo{
    
    public static void main(String[] args) throws Exception{
            BinaryTree bt=new BinaryTree();
            bt.add(new Book("Java", 11.1));
            bt.add(new Book("JSP",22.2));
            bt.add(new Book("Android",33.3));

            Object obj[]=bt.toArray();//将二叉树对象化为对象数组
            System.out.println(Arrays.toString(obj));//将对象数组化为String输出
    }   


}


结果的确是按照要求存储,并且中序遍历了所有节点并且输出。


9003228-52cbf34c5f9fb7a0.png
image.png

猜你喜欢

转载自blog.csdn.net/weixin_34101229/article/details/87040358