源代码
public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree tree = new BinaryTree();
Node root = new Node(1, "宋江");
Node node2 = new Node(2, "吴用");
Node node3 = new Node(3, "卢俊义");
Node node4 = new Node(4, "林冲");
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
tree.setRoot(root);
System.out.println("删除前,前序遍历:");
tree.preOrder();
tree.delNode(17);
System.out.println("删除后:");
tree.preOrder();
}
}
class BinaryTree{
private Node root;
public void setRoot(Node root) {
this.root = root;
}
public void delNode(int no) {
if(this.root != null) {
if(this.root.getNo() == no) {
this.root = null;
}else {
this.root.delNode(no);
}
}else {
System.out.println("这是一棵空树!");
}
}
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 Node preOrderSearch(int no) {
if(this.root != null) {
return this.root.preOrderSearch(no);
}
return null;
}
public Node infixOrderSearch(int no) {
if(this.root != null) {
return this.root.infixOrderSearch(no);
}
return null;
}
public Node postOrderSearch(int no) {
if(this.root != null) {
return this.root.postOrderSearch(no);
}
return null;
}
}
class Node{
private int no;
private String name;
private Node left;
private Node right;
public Node(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 Node getLeft() {
return left;
}
public void setLeft(Node left) {
this.left = left;
}
public Node getRight() {
return right;
}
public void setRight(Node right) {
this.right = right;
}
@Override
public String toString() {
return "Node [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 Node preOrderSearch(int no) {
Node target = null;
if(this.no == no) {
return this;
}
if(this.left != null) {
target = this.left.preOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.right != null) {
target = this.right.preOrderSearch(no);
}
return target;
}
public Node infixOrderSearch(int no) {
Node target = null;
if(this.left != null) {
target = this.left.infixOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.no == no) {
return this;
}
if(this.right != null) {
target = this.right.infixOrderSearch(no);
}
return target;
}
public Node postOrderSearch(int no) {
Node target = null;
if(this.left != null) {
target = this.left.postOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.right != null) {
target = this.right.postOrderSearch(no);
}
if(target != null) {
return target;
}
if(this.no == no) {
return this;
}
return null;
}
public void delNode(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.delNode(no);
}
if(this.right != null) {
this.right.delNode(no);
}
}
}