用java写一个至少十个随机数的数组,将其中数依次插入树里,实现BST的增删查改以及successor
时间: 2023-06-16 16:05:07 浏览: 122
好的,下面是Java代码实现:
```java
import java.util.Random;
public class BST {
private Node root;
private class Node {
int val;
Node left, right;
Node(int val) {
this.val = val;
}
}
public void insert(int val) {
root = insert(root, val);
}
private Node insert(Node node, int val) {
if (node == null) {
return new Node(val);
}
if (val < node.val) {
node.left = insert(node.left, val);
} else if (val > node.val) {
node.right = insert(node.right, val);
}
return node;
}
public void delete(int val) {
root = delete(root, val);
}
private Node delete(Node node, int val) {
if (node == null) {
return null;
}
if (val < node.val) {
node.left = delete(node.left, val);
} else if (val > node.val) {
node.right = delete(node.right, val);
} else {
if (node.left == null && node.right == null) {
node = null;
} else if (node.left == null) {
node = node.right;
} else if (node.right == null) {
node = node.left;
} else {
Node successor = findMin(node.right);
node.val = successor.val;
node.right = delete(node.right, successor.val);
}
}
return node;
}
public boolean search(int val) {
return search(root, val);
}
private boolean search(Node node, int val) {
if (node == null) {
return false;
}
if (node.val == val) {
return true;
} else if (val < node.val) {
return search(node.left, val);
} else {
return search(node.right, val);
}
}
public void update(int oldVal, int newVal) {
delete(oldVal);
insert(newVal);
}
public int successor(int val) {
Node node = find(root, val);
if (node == null) {
return -1;
}
if (node.right != null) {
return findMin(node.right).val;
} else {
Node successor = null;
Node curr = root;
while (curr != node) {
if (node.val < curr.val) {
successor = curr;
curr = curr.left;
} else {
curr = curr.right;
}
}
return successor == null ? -1 : successor.val;
}
}
private Node find(Node node, int val) {
if (node == null) {
return null;
}
if (node.val == val) {
return node;
} else if (val < node.val) {
return find(node.left, val);
} else {
return find(node.right, val);
}
}
private Node findMin(Node node) {
while (node.left != null) {
node = node.left;
}
return node;
}
public static void main(String[] args) {
BST bst = new BST();
Random rand = new Random();
int[] arr = new int[10];
System.out.print("原数组: ");
for (int i = 0; i < 10; i++) {
arr[i] = rand.nextInt(100);
System.out.print(arr[i] + " ");
bst.insert(arr[i]);
}
System.out.println();
System.out.println("插入后的BST: ");
System.out.println(bst);
int idx = rand.nextInt(10);
int val = rand.nextInt(100);
System.out.println("将第" + idx + "个数 " + arr[idx] + " 修改为 " + val);
bst.update(arr[idx], val);
arr[idx] = val;
System.out.println(bst);
idx = rand.nextInt(10);
System.out.println("删除第" + idx + "个数 " + arr[idx]);
bst.delete(arr[idx]);
arr[idx] = -1;
System.out.println(bst);
idx = rand.nextInt(10);
System.out.println("查找第" + idx + "个数 " + arr[idx] + " 是否存在: " + bst.search(arr[idx]));
idx = rand.nextInt(10);
int successor = bst.successor(arr[idx]);
System.out.println("第" + idx + "个数 " + arr[idx] + " 的后继: " + successor);
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
inOrder(root, sb);
return sb.toString();
}
private void inOrder(Node node, StringBuilder sb) {
if (node == null) {
return;
}
inOrder(node.left, sb);
sb.append(node.val).append(" ");
inOrder(node.right, sb);
}
}
```
在 `main` 函数中,先生成一个长度为 10 的随机数组,并用 `insert` 方法将其中的数插入到 BST 中。然后,随机选取一个数,用 `update` 方法将其修改,再随机选取一个数,用 `delete` 方法将其删除。接着,随机选取一个数,用 `search` 方法查找其是否存在,最后随机选取一个数,用 `successor` 方法寻找其后继。
阅读全文