package EXCISE;
public class BinarySoretTreeDemo {
public static void main(String[] args) {
Hero hero0 = new Hero(7, "Rider");
Hero hero1 = new Hero(3, "Lancer");
Hero hero2 = new Hero(10, "Berserker");
Hero hero3 = new Hero(12, "Extra");
Hero hero4 = new Hero(5, "Assassin");
Hero hero5 = new Hero(1, "Saber");
Hero hero6 = new Hero(9, "Caster");
Hero hero7 = new Hero(2, "Archer");
BinarySortHeroTree binarySortHeroTree = new BinarySortHeroTree();
//添加
binarySortHeroTree.addHero(hero0);
binarySortHeroTree.addHero(hero1);
binarySortHeroTree.addHero(hero2);
binarySortHeroTree.addHero(hero3);
binarySortHeroTree.addHero(hero4);
binarySortHeroTree.addHero(hero5);
binarySortHeroTree.addHero(hero6);
binarySortHeroTree.addHero(hero7);
// //遍历
// binarySortHeroTree.infixOrder();
// //删除
// System.out.println("删除后");
// binarySortHeroTree.delHero(1);//一个子节点
// binarySortHeroTree.delHero(2);//无子节点
// binarySortHeroTree.delHero(10);//两个字节点
// binarySortHeroTree.infixOrder();
//修改
System.out.println("修改后");
Hero newHero = new Hero(3, "ruler");
binarySortHeroTree.alter(3, newHero);
binarySortHeroTree.infixOrder();
//查找
Hero summon = binarySortHeroTree.SummonHeros(1);
System.out.println("查找结果:" + summon.toString());
}
}
//创建英灵树
class BinarySortHeroTree {
private Hero grand;
//遍历
public void infixOrder() {
if (grand == null) {
System.out.println("树为空");
} else {
grand.infixOreder();
}
}
//添加
public void addHero(Hero hero) {
if (grand == null) {
grand = hero;
} else {
grand.add(hero);
}
}
//删除
//准备
//准备1
public Hero SearchHero(int rank) {
if (grand == null) {
return null;
} else {
return grand.SearchHero(rank);
}
}
//准备2
public Hero SearchPreHero(int rank) {
if (grand == null) {
return null;
} else {
return grand.SearchPreHero(rank);
}
}
//准备3—为了有两颗子树进行准备
public int DelRightHeroMin(Hero hero) {
Hero temp = hero;
while (temp.left != null) {
temp = temp.left;
}
delHero(temp.rank);
return temp.rank;
}
public int DelLeftHeroMax(Hero hero) {
Hero temp = hero;
while (temp.right != null) {
temp = temp.right;
}
delHero(temp.rank);
return temp.rank;
}
//开始删除
public void delHero(int rank) {
if (grand == null) {
return;
} else {
Hero tergetHero = SearchHero(rank);
if (tergetHero == null) {
return;
}
Hero preHerp = SearchPreHero(rank);
if (preHerp == null) {
grand = null;
return;
}
if (tergetHero.left == null && tergetHero.right == null) {
if (preHerp.left != null && preHerp.left.rank == rank) {
preHerp.left = null;
} else if (preHerp.right != null && preHerp.right.rank == rank) {
preHerp.right = null;
}
} else if (tergetHero.left != null && tergetHero.right != null) {
// int min = DelRightHeroMin(tergetHero.right);
// tergetHero.rank = min;
int max = DelLeftHeroMax(tergetHero.left);
tergetHero.rank = max;
} else {
if (tergetHero.left != null) {
if (preHerp != null) {
if (preHerp.left.rank == rank) {
preHerp.left = tergetHero.left;
} else {
preHerp.right = tergetHero.left;
}
} else {
grand = tergetHero.left;
}
} else {
if (preHerp != null) {
if (preHerp.left.rank == rank) {
preHerp.left = tergetHero.right;
} else {
preHerp.left = tergetHero.right;
}
} else {
grand = tergetHero.right;
}
}
}
}
}
//修改
public void alter(int rank, Hero newHero) {
Hero hero = SearchHero(rank);
if (hero == null) {
System.out.println("不存在该英灵");
} else {
hero.rank = newHero.rank;
hero.server = newHero.server;
}
}
//查找
public Hero SummonHeros(int rank) {
if (grand == null) {
return null;
} else {
return grand.Summon(rank);
}
}
}
//创建节点
class Hero {
int rank;
String server;
Hero left;
Hero right;
public Hero(int rank, String server) {
super();
this.rank = rank;
this.server = server;
}
@Override
public String toString() {
return "Hero{" + "rank=" + rank + ", server=" + server + '}';
}
//中序遍历
public void infixOreder() {
if (this.left != null) {
this.left.infixOreder();
}
System.out.println(this);
if (this.right != null) {
this.right.infixOreder();
}
}
//添加
public void add(Hero hero) {
if (hero == null) {
return;
}
if (hero.rank < this.rank) {
if (this.left == null) {
this.left = hero;
} else {
this.left.add(hero);
}
} else {
if (this.right == null) {
this.right = hero;
} else {
this.right.add(hero);
}
}
}
//删除准备 = 准备1+准备2
//准备1——找到该hero
public Hero SearchHero(int rank) {
if (rank == this.rank) {
return this;
} else if (rank < this.rank) {
if (this.left == null) {
return null;
}
return this.left.SearchHero(rank);
} else {
if (this.right == null) {
return null;
}
return this.right.SearchHero(rank);
}
}
//准备2——找到该hero的前hero
public Hero SearchPreHero(int rank) {
if ((this.left != null && this.left.rank == rank) || (this.right != null && this.right.rank == rank)) {
return this;
} else {
if (rank < this.rank && this.left != null) {
return this.left.SearchPreHero(rank);
} else if (rank >= this.rank && this.right != null) {
return this.right.SearchPreHero(rank);
} else {
return null;
}
}
}
//查找
public Hero Summon(int rank) {
if (this.rank == rank) {
return this;
} else {
if (rank < this.rank) {
if (this.left != null) {
return this.left.Summon(rank);
} else {
return null;
}
} else {
if (this.right != null) {
return this.right.Summon(rank);
} else {
return null;
}
}
}
}
}
Ps:本人重度Fate控,根节点没用root表示而是grand表示 >_<