[英]Homework: Java BST without nodes (BST of BSTs)
注意:BST-二進制搜索樹(初始化)
如標題所示,這是一項家庭作業,所以我不在尋找答案。 相反,我只需要指向正確的方向。
對於該作業,我應該創建一個BST類,該類直接定義為一些最多包含2個BST子級的數據(即,不使用輔助節點類)。
我得到了一個用JavaDocs注釋的類,並且應該填充TODOS。
至少使我入門的絆腳石是在類中途實現insert方法。
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();
}
}
我覺得我了解它是如何工作的,但是我不知道如何實現代碼。
對於格式不可讀或無法共享太多信息,我對此表示歉意。
BST類是節點。 首先,確定null元素是否小於或大於非null元素。 要實現插入,您需要執行以下操作:
1)確認element參數不為null。 如果為null,則什么也不做。
2)未完成的代碼必須決定在哪里插入元素。
a)如果子項之一為空,則插入后,兩個子項都不為空。 將新元素與非null元素進行比較; 兩者中較大者為合適的孩子。
b)如果兩個孩子都不為空,則確定將新元素放置在何處(小於左側,使用left。大於左側,使用right),並為該元素調用插入。
注意:如果您尚未注意到,則此分配與遞歸有關。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.