写了一个二叉树,可以完成增删查的功能,但不够完善,后续会进行改进。特别注意如果要对查找操作计数,注意计数的代码所在位置。另外有地方可以进行优化:例如在进行删除操作的时候,即便在对树的左结点遍历时完成了删除操作该代码仍然将右结点进行了遍历。
class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree bi=new BinaryTree();
Book root=new Book(1,"Joker");
Book book2=new Book(2,"No face man");
Book book3=new Book(3,"Traveler");
Book book4=new Book(4,"Immortal");
Book book5=new Book(5,"Red sacrifices");
Book book6=new Book(6,"Light trap");
Book book7=new Book(7,"Handed man");
root.setLeft(book2);
root.setRight(book3);
book3.setRight(book4);
book3.setLeft(book5);
bi.setRoot(root);
System.out.println("前序遍历");
bi.preOrder();
System.out.println("中序遍历");
bi.infixOrder();
System.out.println("后序遍历");
bi.postOrder();
System.out.println("前序遍历查找");
Book resbook=bi.preOrderSearch(5);
if(resbook!=null) {
System.out.println(resbook);
}else {
System.out.println("没有找到编号为"+5+"的书");
}
System.out.println("后序遍历查找");
Book resbook1=bi.postOrderSearch(5);
if(resbook1!=null) {
System.out.println(resbook1);
}else {
System.out.println("没有找到编号为"+5+"的书");
}
System.out.println("删除节点");
bi.del(5);
bi.preOrder();
}
}
class BinaryTree {
private Book root;
public void setRoot(Book root) {
this.root=root;
}
public void preOrder() {
if(this.root!=null) {
this.root.preOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
public void infixOrder() {
if(this.root!=null) {
this.root.infixOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
public void postOrder() {
if(this.root!=null) {
this.root.postOrder();
}else {
System.out.println("二叉树为空,无法遍历");
}
}
public Book preOrderSearch(int no) {
if(root!=null) {
return root.preOrderSearch(no);
}else {
return null;
}
}
public Book infixOrderSearch(int no) {
if(root!=null) {
return root.infixOrderSearch(no);
}else {
return null;
}
}
public Book postOrderSearch(int no) {
if(root!=null) {
return root.postOrderSearch(no);
}else {
return null;
}
}
public void del(int no) {
if(root!=null) {
if(root.getNo()==no) {
root=null;
}else {
root.del(no);
}
}else {
System.out.println("空树无法删除");
}
}
}
class Book {
private int no;
private String name;
private Book left;
private Book right;
public Book(int no,String name) {
this.no=no;
this.name=name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Book getLeft() {
return left;
}
public void setLeft(Book left) {
this.left = left;
}
public Book getRight() {
return right;
}
public void setRight(Book right) {
this.right = right;
}
@Override
public String toString() {
return "Book [no=" + no + ", name=" + name + "]";
}
public void preOrder() {
System.out.println(this);
if(this.left!=null) {
this.left.preOrder();
}
if(this.right!=null) {
this.right.preOrder();
}
}
public void infixOrder() {
if(this.left!=null) {
this.left.infixOrder();
}
System.out.println(this);
if(this.right!=null) {
this.right.infixOrder();
}
}
public void postOrder() {
if(this.left!=null) {
this.left.postOrder();
}
if(this.right!=null) {
this.right.postOrder();
}
System.out.println(this);
}
public Book preOrderSearch(int no) {
if (this.no == no) {
return this;
}
Book resbook=null;
if (this.left != null) {
resbook=this.left.preOrderSearch(no);
}
if(resbook!=null) {
return resbook;
}
if(this.right!=null) {
resbook=this.right.preOrderSearch(no);
}
return resbook;
}
public Book infixOrderSearch(int no) {
Book resbook=null;
if(this.left!=null) {
resbook=this.left.infixOrderSearch(no);
}
if(resbook!=null) {
return resbook;
}
if(this.no==no) {
return this;
}
if(this.right!=null) {
resbook=this.right.infixOrderSearch(no);
}
return resbook;
}
public Book postOrderSearch(int no) {
Book resbook=null;
if(this.left!=null) {
resbook=this.left.postOrderSearch(no);
}
if(resbook!=null) {
return resbook;
}
if(this.right!=null) {
resbook=this.right.postOrderSearch(no);
}
if(resbook!=null) {
return resbook;
}
System.out.println("进行了一次查找");
if(this.no==no) {
return this;
}else {
return resbook;
}
}
public void del(int no) {
if(this.left!=null&&this.left.no==no) {
this.left=null;
return;
}
if(this.right!=null&&this.right.no==no) {
this.right=null;
return;
}
if(this.left!=null) {
this.left.del(no);
}
if(this.right!=null) {
this.right.del(no);
}
}
}