JAVA Program - public class BSTNode { BSTNode left, right; int data; public BSTN
ID: 3694063 • Letter: J
Question
JAVA Program -
public class BSTNode
{
BSTNode left, right;
int data;
public BSTNode()
{
left = null;
right = null;
data = 0;
}
public BSTNode(int n)
{
left = null;
right = null;
data = n;
}
public void setLeft(BSTNode n)
{
left = n;
}
public void setRight(BSTNode n)
{
right = n;
}
public BSTNode getLeft()
{
return left;
}
public BSTNode getRight()
{
return right;
}
public void setData(int d)
{
data = d;
}
public int getData()
{
return data;
}
}
public class BST
{
private BSTNode root;
public BST()
{
root = null;
}
public boolean isEmpty()
{
return root == null;
}
public void insert(int data)
{
root = insert(root, data);
}
private BSTNode insert(BSTNode node, int data)
{
if (node == null)
node = new BSTNode(data);
else
{
if (data <= node.getData())
node.left = insert(node.left, data);
else
node.right = insert(node.right, data);
}
return node;
}
public void delete(int k)
{
if (isEmpty())
System.out.println("Tree Empty");
else if (search(k) == false)
System.out.println("Sorry "+ k +" is not present");
else
{
root = delete(root, k);
System.out.println(k+ " deleted from the tree");
}
}
private BSTNode delete(BSTNode root, int k)
{
BSTNode p, p2, n;
if (root.getData() == k)
{
BSTNode lt, rt;
lt = root.getLeft();
rt = root.getRight();
if (lt == null && rt == null)
return null;
else if (lt == null)
{
p = rt;
return p;
}
else if (rt == null)
{
p = lt;
return p;
}
else
{
p2 = rt;
p = rt;
while (p.getLeft() != null)
p = p.getLeft();
p.setLeft(lt);
return p2;
}
}
if (k < root.getData())
{
n = delete(root.getLeft(), k);
root.setLeft(n);
}
else
{
n = delete(root.getRight(), k);
root.setRight(n);
}
return root;
}
public int countNodes()
{
return countNodes(root);
}
private int countNodes(BSTNode r)
{
if (r == null)
return 0;
else
{
int l = 1;
l += countNodes(r.getLeft());
l += countNodes(r.getRight());
return l;
}
}
public boolean search(int val)
{
return search(root, val);
}
private boolean search(BSTNode r, int val)
{
boolean found = false;
while ((r != null) && !found)
{
int rval = r.getData();
if (val < rval)
r = r.getLeft();
else if (val > rval)
r = r.getRight();
else
{
found = true;
break;
}
found = search(r, val);
}
return found;
}
public void inorder()
{
inorder(root);
}
private void inorder(BSTNode r)
{
if (r != null)
{
inorder(r.getLeft());
System.out.print(r.getData() +" ");
inorder(r.getRight());
}
}
public void preorder()
{
preorder(root);
}
private void preorder(BSTNode r)
{
if (r != null)
{
System.out.print(r.getData() +" ");
preorder(r.getLeft());
preorder(r.getRight());
}
}
public void postorder()
{
postorder(root);
}
private void postorder(BSTNode r)
{
if (r != null)
{
postorder(r.getLeft());
postorder(r.getRight());
System.out.print(r.getData() +" ");
}
}
}
Explanation / Answer
program :
import java.util.Scanner;
import java.io.*;
import java.util.StringTokenizer;
public class TestDriver2
{
public static void main(String[] args) throws IOException
{
BinarySearchTree bST = new BinarySearchTree();
Word wrd1 = new Word();
Word wrd2 = new Word();
int tracker;
String inFileName = "inwords.txt";
BufferedReader inFile = new BufferedReader (new FileReader (inFileName));
String liner = inFile.readLine();
liner = liner.toLowerCase();
System.out.println("The passage that was read from infile is : " + liner);
StringTokenizer strTokenizer = new StringTokenizer (liner, "., ");
while (strTokenizer.hasMoreTokens())
{
tracker = 0;
wrd1 = new Word(strTokenizer.nextToken(),tracker++);
}
System.out.println("BST is: " + bST);
}
}
public void addElement (Comparable element)
{
BinaryTreeNode temp = new BinaryTreeNode (element);
if (isEmpty())
root = temp;
else
{
BinaryTreeNode current = root;
boolean added = false;
while (!added)
{
if (element.compareTo(current.element) < 0)
if (current.left == null)
{
current.left = temp;
added = true;
}
else
current = current.left;
else
if (current.right == null)
{
current.right = temp;
added = true;
}
else
current = current.right;
}
}
count++;
}
public class Word
{
private String Phrase;
private int counter;
public Word()
{
Phrase = null;
counter = 0;
}
public Word(String phr, int cnt)
{
Phrase = phr;
counter = cnt;
}
public Word(String phr)
{
Phrase = phr;
counter = 0;
}
public boolean equals(Word looking)
{
boolean status;
if(Phrase.equals(looking.Phrase)&& (counter != looking.counter))
status = true;
else
status = false;
return status;
}
public int compareTo(Word comp)
{
if (comp.Phrase == ((Word) comp).Phrase)
return 0;
else if ((comp.Phrase) > ((Word) comp).Phrase)
return 1;
else
return -1;
}
public String toString()
{
String describer = Phrase + ", " + counter;
return describer;
}
public void setPhrase(String p)
{
Phrase = p;
}
public void setCount(int c)
{
counter = c;
}
public String getWord()
{
return Phrase;
}
public int getCount()
{
return counter;
}
public void incword()
{
counter++;
}
public void decword()
{
counter--;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.