package binarysorttree; public class BinarySortTreeClassDemo { public static void main(String[] args) { int[] arr = {7,3,10,12,5,1,9,6}; BinarySortTree binarySortTree = new BinarySortTree(); binarySortTree.createBinarySortTree(arr); binarySortTree.infixList(); binarySortTree.deleteNode(7); binarySortTree.infixList(); } } class BinarySortTree{ public Node root; //将数组建立成排序二叉树 public void createBinarySortTree(int[] arr){ for (int i = 0; i < arr.length; i++) { if (root == null) { root = new Node(arr[i]); }else { root.add(new Node(arr[i])); } } } //中序遍历 public void infixList(){ if (root == null) { System.out.println("该树为空~~~~~"); }else { root.infixOrder(); } } public void deleteNode(int value){ if (root == null) { System.out.println("该树为空,不能删除"); return; } if (root.value == value){ if (root.left == null && root.right == null) {//叶子节点的情况 root = null; }else if (root.left != null && root.right == null){ root = root.left; }else if (root.right != null && root.left == null ){ root = root.right; }else {//可以把这个封装成一个方法 Node pre = root; Node curr = root.right; if (curr.left == null) { root.value = curr.value; root.right = curr.right; }else { while (curr.left != null){ pre = curr; curr = curr.left; } root.value = curr.value; if (curr.right == null) { pre.left = null; }else { pre.left = curr.right; } } } }else { root.delete(value); } } } class Node{ public int value; public Node left; public Node right; public Node(int value) { this.value = value; } @Override public String toString() { return "Node{" + "value=" + value + '}'; } //添加元素 public void add(Node node){ if (node.value < this.value){ if (this.left != null) {//要添加的值小于当前节点的值就往左边放 this.left.add(node); }else { this.left = node; } }else { if (this.right != null) {//大于就往右边放 this.right.add(node); }else { this.right = node; } } } //中序遍历 public void infixOrder(){ if (this.left != null) { this.left.infixOrder(); } System.out.println(this); if (this.right != null) { this.right.infixOrder(); } } //删除节点 public void delete(int value){ if (this.left != null && this.left.value == value) { Node parent = this; Node target = this.left; if (target.left == null && target.right == null) {//叶子节点的情况 parent.left = null; }else if (target.left != null && target.right == null){ parent.left = target.left; }else if (target.right != null && target.left == null ){ parent.left = target.right; }else { Node pre = target; Node curr = target.right; if (curr.left == null) { target.value = curr.value; target.right = curr.right; }else { while (curr.left != null){ pre = curr; curr = curr.left; } target.value = curr.value; if (curr.right == null) { pre.left = null; }else { pre.left = curr.right; } } } } if (this.right != null && this.right.value == value){ Node parent = this; Node target = this.right; if (target.left == null && target.right == null) {//叶子节点的情况 parent.right = null; }else if (target.left != null && target.right == null){ parent.right = target.left; }else if (target.right != null && target.left == null ){ parent.right = target.right; }else { Node pre = target; Node curr = target.right; if (curr.left == null) { target.value = curr.value; target.right = curr.right; }else { while (curr.left != null){ pre = curr; curr = curr.left; } target.value = curr.value; if (curr.right == null) { pre.left = null; }else { pre.left = curr.right; } } } } if (this.left != null) { this.left.delete(value); } if (this.right != null){ this.right.delete(value); } } }