Hello, This is my second problem. i've made a Binary Search Tree. but de delete function dont work. What did i do wrong? the lines
Node current = root;
Node parent = root;
gives the error: Node cannot be resolved to a type
Code:import java.awt.Graphics;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
class BiTree
{ private static final int recNodecount = 0;
BiLink root; // is initial null = empty tree
int nodeCounter;
void empty()
{ root = null;
} // end empty
void show( Graphics g)
{ nodeCounter = 0;
recuShow( root, g, 1);
nodeCounter = 0; // reset for other use
} // end show
private void recuShow( BiLink cursor, Graphics g, int line)
{ if (cursor == null) return;
recuShow( cursor.left, g, line+1);
g.drawString( cursor.asString(), ++nodeCounter*10, 40+line*10 );
recuShow( cursor.right, g, line+1);
} // end recuShow
boolean found( char item)
{ return recuFound( root, item);
} // end found
private boolean recuFound( BiLink cursor, char item)
{ if (cursor == null) return false;
if (cursor.contents == item) return true;
if (item < cursor.contents) return recuFound(cursor.left, item);
else return recuFound( cursor.right, item);
} // end recuFound
//////////////////////// DIEPTE///////////
int depth()
{ return recuDepth( root);
} // end depth
private int recuDepth( BiLink cursor)
{ if (cursor==null) return 0;
else return 1+Math.max(recuDepth(cursor.left),recuDepth(cursor.right));
} // nde recuDepth
///////////////////////////////////////
///////////////////////// de update met de recursiefe methode v //////////////////
void update (char newChar){
root = recUpdate( root, newChar);
} // end method update
private BiLink recUpdate( BiLink cursor, char newKey){
if (cursor == null)
return new BiLink( newKey, null, null);
if ( newKey < (cursor.contents))
cursor.left = recUpdate( cursor.left, newKey); // recursion
else cursor.right = recUpdate( cursor.right, newKey); // recursion
return cursor;
}
///////////////////////// de update met de recursiefe methode ^ //////////////////
//////////////////////////////////////// NODECOUNT v //////////////////////////////
int nodecount() {
return recNodecount (root, nodeCounter);
}
private int recNodecount ( BiLink cursor, int x){
return (root == null || x <=0) ? 0: (1 + recNodecount(cursor.left,x-1) + recNodecount(cursor.right,x-1));
}
////////////////////////////////////////NODECOUNT ^ //////////////////////////////
//////////////////////////////////////DELETE v////////////////////////////////////
public boolean delete(int key) // delete node with given key
{ // (assumes non-empty list)
Node current = root;
Node parent = root;
boolean isLeftChild = true;
while(current.iData != key) // search for node
{
parent = current;
if(key < current.iData) // go left?
{
isLeftChild = true;
current = current.leftChild;
}
else // or go right?
{
isLeftChild = false;
current = current.rightChild;
}
if(current == null) // end of the line,
return false; // didn't find it
} // end while
// found node to delete
// if no children, simply delete it
if(current.leftChild==null &&
current.rightChild==null)
{
if(current == root) // if root,
root = null; // tree is empty
else if(isLeftChild)
parent.leftChild = null; // disconnect
else // from parent
parent.rightChild = null;
}
// if no right child, replace with left subtree
else if(current.rightChild==null)
if(current == root)
root = current.leftChild;
else if(isLeftChild)
parent.leftChild = current.leftChild;
else
parent.rightChild = current.leftChild;
// if no left child, replace with right subtree
else if(current.leftChild==null)
if(current == root)
root = current.rightChild;
else if(isLeftChild)
parent.leftChild = current.rightChild;
else
parent.rightChild = current.rightChild;
else // two children, so replace with inorder successor
{
// get successor of node to delete (current)
Node successor = getSuccessor(current);
// connect parent of current to successor instead
if(current == root)
root = successor;
else if(isLeftChild)
parent.leftChild = successor;
else
parent.rightChild = successor;
// connect successor to current's left child
successor.leftChild = current.leftChild;
} // end else two children
// (successor cannot have a left child)
return true; // success
} // end delete() // success
///////////////////////////////////////DELETE ^/////////////////////////////////
} // end class BiTree
