package com.cwh.batTest;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author(创建者) cwh
* @datetime(创建时间) 2015-8-13 - 下午04:21:29
* @describe(描述) 一组成绩,一组学生,按照成绩的大小,由高到底排序出姓名和成绩
* 二叉树
*/
public class StudentAndScore2 {
public static void main(String[] args) {
BinaryTree bTree = new BinaryTree();
int[] score = new int[]{88,99,87,93,98,98};
String[] name = new String[]{"小红","小名","小白","小篮","小青","小黄"};
for(int i =0;i<score.length;i++){
bTree.addStudentIntree(name[i], score[i]);
}
bTree.printStudent();
}
}
/**
* 建立学生二叉树
* @author(创建者) cwh
* @datetime(创建时间) 2015-8-13 - 下午04:34:58
* @describe(描述)
*/
class BinaryTree{
private TStudent root;
public void addStudentIntree(String name,int score){
TStudent newStudent =new TStudent(name, score);
if(this.root==null){
this.root = newStudent;
}else{
this.root.addStudent(newStudent);
}
}
public static List list = new ArrayList();
public void printStudent(){
this.root.printInOrder(list);
for (int i = list.size(); i >0; i--) {
System.out.println("["+"姓名为:"+((TStudent)list.get(i-1)).getName()+"&成绩为:"+((TStudent)list.get(i-1)).getScore()+"]");
}
}
}
/**
* 学生节点
* @author(创建者) cwh
* @datetime(创建时间) 2015-8-13 - 下午04:35:07
* @describe(描述)
*/
class TStudent{
private String name;
private int score;
TStudent leftStudent;
TStudent rightStudent;
public TStudent(String name,int score) {
this.name=name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public TStudent getLeftStudent() {
return leftStudent;
}
public void setLeftStudent(TStudent leftStudent) {
this.leftStudent = leftStudent;
}
public TStudent getRightStudent() {
return rightStudent;
}
public void setRightStudent(TStudent rightStudent) {
this.rightStudent = rightStudent;
}
/**
* 用于建立二叉搜索树最坏的情况是O(n),为单链表形式,最好的情况是满二叉树
* @param student
*/
public void addStudent(TStudent student){
if (student.score<this.score) {
if(this.leftStudent == null){
this.leftStudent = student;
}else{
this.leftStudent.addStudent(student);
}
}else{
if(this.rightStudent ==null){
this.rightStudent = student;
}else{
this.rightStudent.addStudent(student);
}
}
}
/**
* 打印后序遍历二叉树
*/
public void printInOrder(List list){
if(this.leftStudent !=null){
this.leftStudent.printInOrder(list);
}
list.add(this);
// System.out.println("["+"姓名为:"+this.name+"&成绩为:"+this.score+"]");
if(this.rightStudent !=null){
this.rightStudent.printInOrder(list);
}
}
}
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
*
* @author(创建者) cwh
* @datetime(创建时间) 2015-8-13 - 下午04:21:29
* @describe(描述) 一组成绩,一组学生,按照成绩的大小,由高到底排序出姓名和成绩
* 二叉树
*/
public class StudentAndScore2 {
public static void main(String[] args) {
BinaryTree bTree = new BinaryTree();
int[] score = new int[]{88,99,87,93,98,98};
String[] name = new String[]{"小红","小名","小白","小篮","小青","小黄"};
for(int i =0;i<score.length;i++){
bTree.addStudentIntree(name[i], score[i]);
}
bTree.printStudent();
}
}
/**
* 建立学生二叉树
* @author(创建者) cwh
* @datetime(创建时间) 2015-8-13 - 下午04:34:58
* @describe(描述)
*/
class BinaryTree{
private TStudent root;
public void addStudentIntree(String name,int score){
TStudent newStudent =new TStudent(name, score);
if(this.root==null){
this.root = newStudent;
}else{
this.root.addStudent(newStudent);
}
}
public static List list = new ArrayList();
public void printStudent(){
this.root.printInOrder(list);
for (int i = list.size(); i >0; i--) {
System.out.println("["+"姓名为:"+((TStudent)list.get(i-1)).getName()+"&成绩为:"+((TStudent)list.get(i-1)).getScore()+"]");
}
}
}
/**
* 学生节点
* @author(创建者) cwh
* @datetime(创建时间) 2015-8-13 - 下午04:35:07
* @describe(描述)
*/
class TStudent{
private String name;
private int score;
TStudent leftStudent;
TStudent rightStudent;
public TStudent(String name,int score) {
this.name=name;
this.score = score;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
public TStudent getLeftStudent() {
return leftStudent;
}
public void setLeftStudent(TStudent leftStudent) {
this.leftStudent = leftStudent;
}
public TStudent getRightStudent() {
return rightStudent;
}
public void setRightStudent(TStudent rightStudent) {
this.rightStudent = rightStudent;
}
/**
* 用于建立二叉搜索树最坏的情况是O(n),为单链表形式,最好的情况是满二叉树
* @param student
*/
public void addStudent(TStudent student){
if (student.score<this.score) {
if(this.leftStudent == null){
this.leftStudent = student;
}else{
this.leftStudent.addStudent(student);
}
}else{
if(this.rightStudent ==null){
this.rightStudent = student;
}else{
this.rightStudent.addStudent(student);
}
}
}
/**
* 打印后序遍历二叉树
*/
public void printInOrder(List list){
if(this.leftStudent !=null){
this.leftStudent.printInOrder(list);
}
list.add(this);
// System.out.println("["+"姓名为:"+this.name+"&成绩为:"+this.score+"]");
if(this.rightStudent !=null){
this.rightStudent.printInOrder(list);
}
}
}