# Thread: Need with my assignment ...

1. Member
Join Date
Apr 2009
Posts
1
Rep Power
0

## Need with my assignment ...

Hey guys,
ok, I have the following psudoecode to search a binary search tree for a word:

to find the correct position.
* we would need to search the tree
* assuming it's a binary search tree
* we compare the word to the top most node
* if it's higher or lower determines whether we look to the right or left of the tree
* we for loop through the tree (iteratively or recursively, assignment requires both) until we find the word or have searched all nodes.
* if we find the word we insert the line position into the corresponding nodes list
* with the list insertion, I dont think it matters how we do that, because it does not matter if the line and position values are at the start or end of the list, assuming many values, as loingas they can be associated with the correct node is all that matters.
* If we dont find the word then we must seacrh the tree for the correct position to insert it
* this we would do by searching for the word, this should lead to a null link, we can then replace that link to anode containing the word.
* or by setting parent to null, set current to the bst's root
* then looping through:
- if current is null, replace null link which curent had with newly created leaf node with word
- if word is equal to current then terminate // word exists
- if word is less than current, set parent to current and set current to currents left child
- if word is greater than current, set parent to current and set current to currents right child
- then need to insert line position for this node in the List

Heres the code that I have to apply it to,
but I'm unsure of what to do next, or how to implement it,
any help at all would be much apprecited,

Jimmy

Java Code:
```class Tree091 /*(i.e., WordTree)*/
{ private char[] word=null;
private List091 list=null;
private Tree091 precursor=null; /*(i.e., leftSubTree)*/
private Tree091 successor=null; /*(i.e., rightSubTree)*/
public Tree091(char[] word,
List091 list,
Tree091 precursor, /*(i.e., lexicographically < word)*/
Tree091 successor) /*(i.e., lexicographically > word)*/
{ if (word==null) return;
if (word.length<1) return;
if (list==null) return;
this.word=word;
this.list=list;
this.precursor=precursor;
this.successor=successor;
}
int line,
int position,
boolean iterative)
{ if (word==null) return;
if (word.length<1) return;
if (line<0) return;
if (position<0) return;
if (iterative)
else
}
int line,
int position)
{
/* students to complete */
}
int line,
int position)
{
/* students to complete */
}
private int compare(char[] array1,
char[] array2)
{ if (array1==null) return -2;
if (array2==null) return -2;
int length1=array1.length;
if (length1==0) return -2;
int length2=array2.length;
if (length2==0) return -2;
int minLength=length1<length2?length1:length2;
for (int i=0; i<minLength; i++)
{ if (array1[i]<array2[i]) return -1;
if (array1[i]>array2[i]) return 1;
}
return length1==length2 ? 0 : length1<length2 ? -1 : 1;
}
public boolean contained(char[] word)
{ int compare=compare(this.word,word);
if (compare==1&&this.precursor!=null)
return this.precursor.contained(word);
if (compare==0) return true;
if (compare==-1&&this.successor!=null)
return this.successor.contained(word);
return false;
}
public int listNodeCount(boolean iterative)
{
/* students to complete */
}
public int maximumWordLength(int currentLength)
{
/* students to complete */
}
public String toString()
{ String string="";
if (this.precursor!=null) string+=this.precursor;
string+="\""+(new String(this.word))+"\""+this.list;
if (this.successor!=null) string+=this.successor;
return string;
}
public int treeNodeCount()
{
/* students to complete */
}
public String wordArray(int length)
{
/* students to complete */
}
}```