实现一个二叉搜索树,提供添加元素、删除元素、前序、中序、后序遍历、节点个数、查询单一节点等功能,为了实 现BinarySearchTree,需要定义好一个节点类型,如下 : 定义树接口如下: Node<T> findTargetNode(T element); 注:接口中带 TODO 字样的方法,是视频中没有写的,需要你们自己实现。 写一个BinarySearchTree 来实现 Tree 接口,定义如下: public class BinarySearchTree<T extends Comparable<T>> implements Tree<T> { //属性: //1. 指向根节点 private Node<T> root; //2.树中节点的数量 private int size; //TODO 实现接口所有的方法,另外,要实现remove方法,在此类中还要额外提供 findParentNode(T target) 方法和 findMinNodeFromRight(Node<T> target) 方法
时间: 2024-03-26 20:35:32 浏览: 58
binary search tree 二叉搜索树的C++实现,有插入、删除、查找、查找最大最小等功能
5星 · 资源好评率100%
好的,我已经了解了您的要求,下面是实现BinarySearchTree的代码:
```
public interface Tree<T extends Comparable<T>> {
boolean add(T element);
boolean remove(T element);
boolean contains(T element);
void preOrderTraversal();
void inOrderTraversal();
void postOrderTraversal();
int size();
}
public class Node<T extends Comparable<T>> {
T data;
Node<T> left;
Node<T> right;
public Node(T data) {
this.data = data;
left = null;
right = null;
}
}
public class BinarySearchTree<T extends Comparable<T>> implements Tree<T> {
private Node<T> root;
private int size;
public BinarySearchTree() {
root = null;
size = 0;
}
@Override
public boolean add(T element) {
if (root == null) {
root = new Node<>(element);
size++;
return true;
} else {
Node<T> current = root;
Node<T> parent = null;
while (current != null) {
parent = current;
if (element.compareTo(current.data) < 0) {
current = current.left;
} else if (element.compareTo(current.data) > 0) {
current = current.right;
} else {
return false;
}
}
Node<T> newNode = new Node<>(element);
if (element.compareTo(parent.data) < 0) {
parent.left = newNode;
} else {
parent.right = newNode;
}
size++;
return true;
}
}
@Override
public boolean remove(T element) {
Node<T> parent = findParentNode(element);
if (parent == null) {
return false;
}
Node<T> current = (parent.left != null && parent.left.data.compareTo(element) == 0) ? parent.left : parent.right;
if (current.left == null && current.right == null) { // case 1: no child
if (parent.left == current) {
parent.left = null;
} else {
parent.right = null;
}
} else if (current.left != null && current.right == null) { // case 2: only left child
if (parent.left == current) {
parent.left = current.left;
} else {
parent.right = current.left;
}
} else if (current.left == null && current.right != null) { // case 2: only right child
if (parent.left == current) {
parent.left = current.right;
} else {
parent.right = current.right;
}
} else { // case 3: two children
Node<T> minNodeFromRight = findMinNodeFromRight(current);
T tempData = minNodeFromRight.data;
remove(tempData);
current.data = tempData;
}
size--;
return true;
}
@Override
public boolean contains(T element) {
return findTargetNode(element) != null;
}
@Override
public void preOrderTraversal() {
preOrderTraversalHelper(root);
}
private void preOrderTraversalHelper(Node<T> node) {
if (node != null) {
System.out.print(node.data + " ");
preOrderTraversalHelper(node.left);
preOrderTraversalHelper(node.right);
}
}
@Override
public void inOrderTraversal() {
inOrderTraversalHelper(root);
}
private void inOrderTraversalHelper(Node<T> node) {
if (node != null) {
inOrderTraversalHelper(node.left);
System.out.print(node.data + " ");
inOrderTraversalHelper(node.right);
}
}
@Override
public void postOrderTraversal() {
postOrderTraversalHelper(root);
}
private void postOrderTraversalHelper(Node<T> node) {
if (node != null) {
postOrderTraversalHelper(node.left);
postOrderTraversalHelper(node.right);
System.out.print(node.data + " ");
}
}
@Override
public int size() {
return size;
}
private Node<T> findTargetNode(T element) {
Node<T> current = root;
while (current != null) {
if (element.compareTo(current.data) < 0) {
current = current.left;
} else if (element.compareTo(current.data) > 0) {
current = current.right;
} else {
return current;
}
}
return null;
}
private Node<T> findParentNode(T element) {
Node<T> current = root;
Node<T> parent = null;
while (current != null) {
if (element.compareTo(current.data) < 0) {
parent = current;
current = current.left;
} else if (element.compareTo(current.data) > 0) {
parent = current;
current = current.right;
} else {
return parent;
}
}
return null;
}
private Node<T> findMinNodeFromRight(Node<T> node) {
Node<T> current = node.right;
while (current.left != null) {
current = current.left;
}
return current;
}
}
```
请注意,我们在实现 `remove` 方法时,使用了 `findParentNode` 来找到要删除节点的父节点,同时使用了 `findMinNodeFromRight` 来找到右子树中最小的节点,以保证删除后二叉搜索树的性质不变。
阅读全文