简介
- 二叉排序树的构造
- 查找二叉排序树中的对应节点
- 删除二叉排序树中的某一节点
代码
public class BinarySortTreeDemo {
public static void main(String[] args) {
int[] arr = {6, 3, 5, 1, 2, 9, 7};
BinarySortTree bst = new BinarySortTree();
for(int i=0; i<arr.length; i++) {
bst.add(new Node(arr[i]));
}
System.out.println("中序遍历:");
bst.infixOrder();
System.out.println("前序遍历:");
bst.preOrder();
System.out.println("测试删除节点:");
bst.deleteNode(6);
bst.infixOrder();
int[] arr2 = {1, 2, 3, 4, 5};
BinarySortTree bst2 = new BinarySortTree();
for(int i=0; i<arr2.length; i++) {
bst2.add(new Node(arr2[i]));
}
System.out.println("中序遍历:");
bst2.infixOrder();
System.out.println("前序遍历");
bst2.preOrder();
bst2.deleteNode(1);
System.out.println("中序遍历:");
bst2.infixOrder();
}
}
class BinarySortTree{
private Node root;
public void add(Node node) {
if(root == null) {
root = node;
}else {
root.add(node);
}
}
public void infixOrder() {
if(root == null) {
System.out.println("二叉排序树为空");
return;
}
root.infixOrder();
}
public void preOrder() {
if(root == null) {
System.out.println("二叉排序树为空");
return;
}
root.preOrder();
}
public Node search(int value) {
if(root == null) {
return null;
}else {
return root.search(value);
}
}
public Node searchParent(int value) {
if(root == null) {
return null;
}else {
return root.searchParent(value);
}
}
public int delRightTreeMin(Node node) {
Node target = node;
while(target.left != null){
target = target.left;
}
int value = target.value;
this.deleteNode(value);
return value;
}
public void deleteNode(int value) {
if(this.root == null) {
return;
}else {
Node targetNode = this.search(value);
if(targetNode == null) {
return;
}
if(root.left == null && root.right == null) {
root = null;
return;
}
if(targetNode == root && !(root.left != null && root.right != null)) {
if(root.left != null) {
root = root.left;
}else {
root = root.right;
}
return;
}
Node parent = this.searchParent(value);
if(targetNode.left == null && targetNode.right == null) {
if(parent.left != null && parent.left.value == value) {
parent.left = null;
}else if(parent.right != null && parent.right.value == value){
parent.right = null;
}
}else if(targetNode.left != null && targetNode.right != null) {
int min = this.delRightTreeMin(targetNode.right);
targetNode.value = min;
}else {
if(parent.left.value == value) {
if(targetNode.left != null) {
parent.left = targetNode.left;
}else if(targetNode.right != null) {
parent.left = targetNode.right;
}
}else if(parent.right.value == value){
if(targetNode.left != null) {
parent.right = targetNode.left;
}else if(targetNode.right != null) {
parent.right = targetNode.right;
}
}
}
}
}
}
class Node{
int value;
Node left;
Node right;
public Node(int value) {
this.value = value;
}
public void add(Node node) {
if(node == null) {
return;
}
if(node.value < this.value) {
if(this.left == null) {
this.left = node;
}else {
this.left.add(node);
}
}else {
if(this.right == null) {
this.right = node;
}else {
this.right.add(node);
}
}
}
public void infixOrder() {
if(this.left != null) {
this.left.infixOrder();
}
System.out.println(this);
if(this.right != null) {
this.right.infixOrder();
}
}
public void preOrder() {
System.out.println(this);
if(this.left != null) {
this.left.preOrder();
}
if(this.right != null) {
this.right.preOrder();
}
}
public Node search(int value) {
if(value == this.value) {
return this;
}else if(value < this.value) {
if(this.left != null) {
return this.left.search(value);
}else {
return null;
}
}else {
if(this.right != null) {
return this.right.search(value);
}else {
return null;
}
}
}
public Node searchParent(int value) {
if((this.left != null && this.left.value == value) ||
(this.right != null && this.right.value == value)) {
return this;
}else {
if(value < this.value && this.left != null) {
return this.left.searchParent(value);
}else if(value >= this.value && this.right != null) {
return this.right.searchParent(value);
}else {
return null;
}
}
}
@Override
public String toString() {
return "Node: " + this.value;
}
}