- package exam;
- import java.util.Arrays;
- class Student1 implements Comparable<Student1>{
- private String name;
- private int age;
- private float score;
- public Student1(String name,int age,float score){
- this.name=name;
- this.age=age;
- this.score=score;
- }
- @Override
- public int compareTo(Student1 o) {
- if(this==o)
- return 0;
- if(this.score==o.score&&this.age==o.age){
- return 0;
- }
- if(this.score>o.score)
- return 1;
- if(this.score==o.score){
- if(this.age>o.age)
- return -1;
- else if(this.age==o.age)
- return 0;
- else if(this.age<o.age)
- return 1;
- }
- return -1;
- }
- public String toString(){
- return name+","+age+","+score;
- }
- }
- class BinaryTree{
-
-
- //定义内部节点类
- class Node{
- private Comparable<Comparable> data;
- private Node left;
- private Node right;
- public Node(Comparable<Comparable> node){
- this.data=node;
- }
- //定义增加节点方法
-
- public void add(Node n){
- if(this.data.compareTo(n.data)>0){
- if(this.right==null){
- this.right=n;
- }
- else{
- this.right.add(n);
- }
- }
- if(this.data.compareTo(n.data)<=0){
- if(this.left==null){
- this.left=n;
- }else{
- this.left.add(n);
- }
- }
-
- }
-
-
-
- public void print(){
- if(this.left!=null){
- this.left.print();
- }
- System.out.println(this.data);
- if(this.right!=null){
- this.right.print();
- }
- }
- }
- private Node root;
- public void add(Comparable node){
- if(this.root==null){
- this.root=new Node(node);
- }else{
- root.add(new Node(node));
- }
- }
- public void print(){
- if(this.root==null){
- System.out.println("二叉树为空");
- }
- this.root.print();
- }
- }
- public class MySort {
- public static void main(String args[]){
- Student1 s[]={new Student1("李四",21,89),new Student1("王五",22,89),new Student1("赵六",22,90),new Student1("赵七",23,70)};
- System.out.println("排序之前:"+Arrays.toString(s));
- Arrays.sort(s);
- System.out.println("排序之后:"+Arrays.toString(s));
- BinaryTree bt=new BinaryTree();
- bt.add(6);bt.add(100);bt.add(99);bt.add(34);bt.add(55);bt.add(1);
- bt.print();
- }
- }
复制代码
|
|