私は、割り当てのさまざまな段階に分割されたいくつかの異なる操作をサポートすることになっている 2 ~ 3 個の検索ツリーを作成する割り当てを受けました。ステージ 1 では、操作 get、put、および size をサポートすることになっています。私は現在 get 操作を実装しようとしていますが、行き詰まっており、続行する方法に頭を悩ませることができないため、自分が書いたすべてのコードに疑問を呈しており、他の誰かの入力が必要だと感じています.
2 ~ 3 個の検索ツリーを作成する方法を調べてみましたが、意味をなさない、または必要な機能を実行していないコードが大量に見つかりました。ゼロからの自己、そして今ここにいます。
私のノードクラス
package kth.id2010.lab.lab04;
public class Node {
boolean isLeaf = false;
int numberOfKeys;
String[] keys = new String[2]; //each node can contain up to 2 keys
int[] values = new int[2]; //every key contains 2 values
Node[] subtrees = new Node[3]; //every node can contain pointers to 3 different nodes
Node(Node n) {
n.numberOfKeys = 0;
n.isLeaf = true;
}
}
マイツリー作成教室
package kth.id2010.lab.lab04;
public class Tree {
Node root; // root node of the tree
int n; // number of elements in the tree
private Tree(){
root = new Node(root);
n = 0;
}
//Return the values of the key if we find it
public int[] get(String key){
//if the root has no subtrees check if it contain the right key
if(this.root.subtrees.length == 0){
if(this.root.keys[0] == key){
return(this.root.keys[0].values);
}
else if(this.root.keys[1] == key){
return(this.root.keys[1].values);
}
}
//if noot in root, check its subtree nodes
//and here I can't get my head around how to traverse down the tree
else{
for(int i = 0; i < this.root.subtrees.length; i++){
for(int j = 0; j < this.root.subtrees[i].keys.length; j++){
if(this.root.subtrees[i].keys[j] == key){
return(this.root.subtrees[i].keys[j].values);
}
}
}
}
return null;
}
}
私が自分で言えることは、values[]
各キーにバインドする方法を見つける必要があるということですが、その方法がわかりません。寝不足か、この考え方にとらわれているのかもしれません。