|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
|
|
#1
|
|||
|
|||
|
B+ Tree help ! ! !
Hi to all of you!!!I am new to Java programming, as I am a student and new to this forum!!! I have a problem in a assignment for my university. I must build a B+ Tree which stores keys from records... The Code tha I have build until now is this one : Class Tree Code:
import java.util.*;
public class Tree {
private int p;
private int pLeaf;
private float fillFactor;
Node root;
public Tree(int p, int pLeaf, float fillFactor) {
this.p = p;
this.pLeaf = pLeaf;
this.fillFactor = fillFactor;
this.root = new NodeLeaf(pLeaf);
}
public void insert(int key){
Node n;
Stack parents = new Stack();
int numberOfPointers;
n = this.root;
while ( !(n instanceof NodeLeaf) ){
parents.push(n);
numberOfPointers = this.p;
if (key <= n.printKey(0)) {
n = n.children[0];
}
else if ( key > n.printKey(numberOfPointers - 1) && n.printKey(numberOfPointers - 1) != 0 ){
n = n.children[numberOfPointers];
}
else{
int position;
// It searches the Node for the position where the key must be placed.
position = n.search(key);
n = n.printChildren(position);
}
}
// It searches the NodeLeaf if the key already exists in the database.
int found;
found = n.search(key);
if (found == 1){
System.out.println (" The record is already in the databse. It cannot be inserted again!");
}
else{
if ( !(n.isTheNodeFull()) ){
n.insert(key);
}
else{
NodeLeaf temp = new NodeLeaf(pLeaf + 1);
temp = n;
temp.insert(key);
NodeLeaf newLeaf = new NodeLeaf(pLeaf);
newLeaf.pNext = n.pNext;
}
}
}
public void delete(){
}
public void update(){
}
public void search(){
}
}
Class Node: Code:
public class Node {
private int p;
private int []key;
NodeLeaf []children;
public Node() {
}
public Node(int p) {
this.p = p;
this.key = new int[p-1];
this.children = new NodeLeaf[p];
}
public void insert(int key){
}
public void delete(){
}
public void update(){
}
/* The function search, searches between witch keys fits the gives search key. */
/* When the position i in the arreay is found, it is returned. */
public int search(int key){
int i;
for(i=1; i<this.key.length-1; i++){
if ( ( this.key[i-1]< key ) && ( key <= this.key[i] ) ){
break;
}
}
return i;
}
public boolean isTheNodeFull(){
for (int i=0; i<this.key.length; i++) {
if ( this.key[i] == 0 ){
return false;
}
}
return true;
}
public Node printChildren(int i){
return this.children[i];
}
public int printKey(int i){
return this.key[i];
}
public void changeChildren(int i, NodeLeaf newChildren){
this.children[i] = newChildren;
}
public void changeKey(int i, int newKey){
this.key[i] = newKey;
}
}
Class NodeLeaf: Code:
public class NodeLeaf extends Node{
private int pLeaf;
int []key;
NodeLeaf pNext;
public NodeLeaf() {
}
public NodeLeaf(int pLeaf) {
this.pLeaf = pLeaf;
this.key = new int[pLeaf];
}
public void insert(int key){
for(int i=0; i<this.key.length; i++){
if (this.key[i] == 0){
this.key[i] = key;
}
}
}
public void delete(){
}
public void update(){
}
/* The search function search for the key in the NodeLeaf. */
/* If found, it means that the key already exists in the database */
/* so it prints the apropriate message and returns 0. If not found*/
/* thesn returns 1. */
public int search(int key){
int i;
for (i=0; i<this.key.length; i++) {
if ( this.key[i] == key ){
System.out.println("The record is in tha database");
return 1;
}
}
return 0;
}
public boolean isTheNodeFull(){
for (int i=0; i<this.key.length; i++) {
if ( this.key[i] == 0 ){
return false;
}
}
return true;
}
}
So here is my problem now (the red one): the insert in class Tree. The compiler throws this message : Code:
Tree.java [49:1] incompatible types
found : Node
required: NodeLeaf
temp = n;
^
Tree.java [52:1] cannot resolve symbol
symbol : variable pNext
location: class Node
newLeaf.pNext = n.pNext;
^
2 errors
Errors compiling Tree.
And I don't know how to solve it !!! Please if anyone can help me ) |
|
#2
|
||||
|
||||
|
As for line 49....
How can a NodeLeaf = a new Node? Sounds like a type mismatch... what is it you're trying to do here? The second error is linked to the first... fix the first and the second will go away =) |
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > B+ Tree help ! ! ! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|