注意:BST - 二进制搜索树(初始化)
正如标题所说, 这是一项家庭作业,所以我不是在寻找答案。 相反,我只需要一个正确方向的点。
对于作业, 我应该创建一个BST类,它直接定义为最多可容纳2个BST子节点的数据 (即不使用辅助节点类)。
我获得了一个用JavaDocs注释的类,我应该填写TODOS。
至少开始时绊倒我的是在课程的中途实施插入方法。
import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
/**
* A class to implement the Binary Search Tree data structure.
* The structure is generic in the type of Objects it contains.
* @param <T> the type of the contained elements this structure is to hold
*/
public class BinarySearchTree<T> {
private Comparator<T> comparator;
private T data;
private BinarySearchTree<T> left;
private BinarySearchTree<T> right;
/**
* Constructs an empty BST with a Comparator
* @param comp the Comparator to use to impose an ordering on instances of T
*/
public BinarySearchTree(Comparator<T> comp) {
this.comparator = comp;
}
/**
* Constructs a BST with data and a Comparator
* @param data the data this BST should hold
* @param comp the Comparator to use to impose an ordering on instances of T
*/
public BinarySearchTree(T data, Comparator<T> comp) {
this.data = data;
this.comparator = comp;
}
/**
* Inserts an element into this BST
* @param element the element to insert into this BST
*/
public void insert(T element) {
//TODO
if(left == null && right == null){
left = new BinarySearchTree(element, comparator);
}else{
/**
*Do something with the comparator to figure out if the element goes
*to left or right?
*/
}
}
/**
* Searchs for a given element in this BST
* @param element the element to search this BST for
* @return the element in this BST matching the given element, if found,
* otherwise null if the given element is not in this BST
*/
public T find(T element) {
// TODO
}
/**
* Gathers all the elements of this BST in order
* @return a List holding the elements in this BST in order
*/
public List<T> getElements() {
List<T> list = new ArrayList<>();
// TODO
return list;
}
/**
* Pretty prints the contents of this BST in a horizontal tree-like fashion
*/
public void prettyPrint() {
prettyPrint(0);
}
private void prettyPrint(int indentLevel) {
// TODO
// similar to printInOrder from assignment09,
// but print `indentLevel` amount of spaces before printing data on its own line
// you may use a for loop to print `indentLevel` amount of spaces
// each time you recurse, you add to indentLevel
}
/**
* A main method supplied for any debugging needs
*/
public static void main(String[] args) {
// Up to you how you use this main method, feel free to change it
Comparator<Integer> intComp = (i, j) -> i - j; // lambda expression
BinarySearchTree<Integer> tree = new BinarySearchTree<>(intComp);
tree.insert(3);
tree.insert(8);
tree.insert(1);
tree.insert(0);
tree.insert(3);
tree.insert(9);
tree.insert(4);
tree.prettyPrint();
}
}
我觉得我理解它是如何工作的概念,但我不知道如何实现代码。
如果格式不可读,或者为了获得一些帮助而分享太多信息,我会为格式道歉。
答案 0 :(得分:1)
BST类是节点。 首先,确定null元素是否小于或大于非null元素。 要实现插入,您需要执行以下操作:
1)验证element参数不为null。如果为null,则不执行任何操作。
2)未完成的代码必须决定插入元素的位置。
a)如果其中一个子项为null,则最终在插入后两个子项都不为null。将新元素与非null元素进行比较;这两个人中较大的一个是正确的孩子。
b)如果两个子节点都不为null,则确定放置新元素的位置(小于左侧,使用左侧。大于左侧,使用右侧)并调用该元素的插入。
注意:如果你还没有注意到,这个赋值就是递归。