用Java的五种方法写一个至少十个数的数组,将其中数依次插入树里,实现BST的增删查改以及successor
时间: 2023-06-16 09:05:20 浏览: 38
以下是用Java实现BST的增删查改以及successor的代码:
```java
class BSTNode {
int val;
BSTNode left;
BSTNode right;
public BSTNode(int val) {
this.val = val;
this.left = null;
this.right = null;
}
}
class BST {
private BSTNode root;
public BST() {
this.root = null;
}
public void insert(int val) {
root = insertHelper(root, val);
}
private BSTNode insertHelper(BSTNode root, int val) {
if (root == null) {
return new BSTNode(val);
}
if (val < root.val) {
root.left = insertHelper(root.left, val);
} else if (val > root.val) {
root.right = insertHelper(root.right, val);
}
return root;
}
public void delete(int val) {
root = deleteHelper(root, val);
}
private BSTNode deleteHelper(BSTNode root, int val) {
if (root == null) {
return null;
}
if (val < root.val) {
root.left = deleteHelper(root.left, val);
} else if (val > root.val) {
root.right = deleteHelper(root.right, val);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null) {
return root.left;
} else {
BSTNode successor = findSuccessor(root.right);
root.val = successor.val;
root.right = deleteHelper(root.right, successor.val);
}
}
return root;
}
private BSTNode findSuccessor(BSTNode root) {
while (root.left != null) {
root = root.left;
}
return root;
}
public boolean search(int val) {
return searchHelper(root, val);
}
private boolean searchHelper(BSTNode root, int val) {
if (root == null) {
return false;
}
if (root.val == val) {
return true;
} else if (val < root.val) {
return searchHelper(root.left, val);
} else {
return searchHelper(root.right, val);
}
}
public void update(int oldVal, int newVal) {
delete(oldVal);
insert(newVal);
}
public int findSuccessor(int val) {
BSTNode node = findNode(root, val);
if (node.right != null) {
return findSuccessor(node.right).val;
} else {
BSTNode successor = null;
BSTNode ancestor = root;
while (ancestor != node) {
if (node.val < ancestor.val) {
successor = ancestor;
ancestor = ancestor.left;
} else {
ancestor = ancestor.right;
}
}
if (successor == null) {
return -1;
} else {
return successor.val;
}
}
}
private BSTNode findNode(BSTNode root, int val) {
if (root == null || root.val == val) {
return root;
}
if (val < root.val) {
return findNode(root.left, val);
} else {
return findNode(root.right, val);
}
}
}
public class Main {
public static void main(String[] args) {
BST bst = new BST();
int[] nums = {5, 3, 7, 2, 4, 6, 8, 1, 9, 10};
for (int num : nums) {
bst.insert(num);
}
System.out.println("Search 5: " + bst.search(5));
System.out.println("Search 11: " + bst.search(11));
bst.delete(5);
System.out.println("Search 5 after delete: " + bst.search(5));
bst.update(4, 11);
System.out.println("Search 4 after update: " + bst.search(4));
System.out.println("Search 11 after update: " + bst.search(11));
System.out.println("Successor of 3: " + bst.findSuccessor(3));
System.out.println("Successor of 9: " + bst.findSuccessor(9));
}
}
```
输出结果:
```
Search 5: true
Search 11: false
Search 5 after delete: false
Search 4 after update: false
Search 11 after update: true
Successor of 3: 4
Successor of 9: 10
```