Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Q)Implementing a Binary Search Tree? Plese do this qustion using JAVA langue I provied to you starter code to print and add code so do
Q)Implementing a Binary Search Tree? Plese do this qustion using JAVA langue I provied to you starter code to print and add code so do it and make it print and Add and search .
*BSTTester.java
public class BSTTester { /** * @param args */ public static void main(String[] args) { BinarySearchTreeroot = new BinarySearchTree (); root.Add("c"); root.Add("b"); root.Add("a"); root.Add("A"); root.Print(); System.out.println(root.Search("c")); System.out.println(root.Search("d")); } } *BinarySearchTree.java
public class BinarySearchTree{ private T Item; private BinarySearchTree rightChild; private BinarySearchTree leftChild; public void Add(T t) { if(this.Item==null) this.Item=t; else if(t.compareTo(this.Item) < 0) { if(this.leftChild==null) this.leftChild = new BinarySearchTree (); this.leftChild.Add(t); } else { if(this.rightChild==null) this.rightChild = new BinarySearchTree (); this.rightChild.Add(t); } } public void Print() { if(this.leftChild!=null) leftChild.Print(); System.out.println(this.Item); if(this.rightChild!=null) rightChild.Print(); } public boolean Search(T t) { boolean found = false; if(this.Item.equals(t)) found=true; else if(this.rightChild!=null && this.rightChild.Search(t)) found=true; else if(this.leftChild!=null && this.leftChild.Search(t)) found=true; return found; } }
Q)Implementing a Binary Search Tree? Plese do this qustion using JAVA langue I provied to you starter code to print and add code so do it and make it print and Add and search .
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started