用java写二分搜索树实现Insert(),visit(),search()方法 实现通用,不能使用模板实现 ,实现处理异常使用嵌套类优化, 实现通用数据的储存
时间: 2023-04-03 18:00:28 浏览: 89
好的,我可以回答这个问题。对于二分搜索树的Insert()方法,我们可以先判断根节点是否为空,如果为空,则直接将新节点作为根节点。如果不为空,则比较新节点和当前节点的大小关系,如果小于当前节点,则递归调用Insert()方法,将新节点插入当前节点的左子树中,否则递归调用Insert()方法,将新节点插入当前节点的右子树中。
对于Visit()方法,我们可以采用中序遍历的方式,先访问左子树,再访问当前节点,最后访问右子树。
对于Search()方法,我们可以先判断当前节点是否为空,如果为空,则返回null。如果不为空,则比较当前节点和目标节点的大小关系,如果相等,则返回当前节点。如果小于当前节点,则递归调用Search()方法,在当前节点的左子树中查找目标节点。如果大于当前节点,则递归调用Search()方法,在当前节点的右子树中查找目标节点。
至于通用数据的储存,我们可以使用泛型来实现。具体实现可以参考以下代码:
```
public class BinarySearchTree<T extends Comparable<T>> {
private Node root;
private class Node {
T data;
Node left;
Node right;
public Node(T data) {
this.data = data;
}
}
public void insert(T data) {
if (root == null) {
root = new Node(data);
} else {
insert(root, data);
}
}
private void insert(Node node, T data) {
if (data.compareTo(node.data) < ) {
if (node.left == null) {
node.left = new Node(data);
} else {
insert(node.left, data);
}
} else {
if (node.right == null) {
node.right = new Node(data);
} else {
insert(node.right, data);
}
}
}
public void visit() {
visit(root);
}
private void visit(Node node) {
if (node != null) {
visit(node.left);
System.out.print(node.data + " ");
visit(node.right);
}
}
public Node search(T data) {
return search(root, data);
}
private Node search(Node node, T data) {
if (node == null) {
return null;
} else if (data.compareTo(node.data) == ) {
return node;
} else if (data.compareTo(node.data) < ) {
return search(node.left, data);
} else {
return search(node.right, data);
}
}
}
```
至于异常处理,我们可以使用try-catch语句来捕获异常。具体实现可以参考以下代码:
```
public class BinarySearchTree<T extends Comparable<T>> {
...
public void insert(T data) {
try {
if (root == null) {
root = new Node(data);
} else {
insert(root, data);
}
} catch (Exception e) {
System.out.println("Insert failed: " + e.getMessage());
}
}
private void insert(Node node, T data) throws Exception {
if (data == null) {
throw new Exception("Data cannot be null");
}
if (data.compareTo(node.data) < ) {
if (node.left == null) {
node.left = new Node(data);
} else {
insert(node.left, data);
}
} else {
if (node.right == null) {
node.right = new Node(data);
} else {
insert(node.right, data);
}
}
}
public void visit() {
try {
visit(root);
} catch (Exception e) {
System.out.println("Visit failed: " + e.getMessage());
}
}
private void visit(Node node) throws Exception {
if (node != null) {
visit(node.left);
System.out.print(node.data + " ");
visit(node.right);
}
}
public Node search(T data) {
try {
return search(root, data);
} catch (Exception e) {
System.out.println("Search failed: " + e.getMessage());
return null;
}
}
private Node search(Node node, T data) throws Exception {
if (data == null) {
throw new Exception("Data cannot be null");
}
if (node == null) {
return null;
} else if (data.compareTo(node.data) == ) {
return node;
} else if (data.compareTo(node.data) < ) {
return search(node.left, data);
} else {
return search(node.right, data);
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)