Question: Topic: Tree Traversal for Binary Search Tree Policy: 1 ) Each lab is due at the end of each allocated Lab Section. The TA will
Topic: Tree Traversal for Binary Search Tree
Policy: Each lab is due at the end of each allocated Lab Section. The TA will be available at
HUM to help you for the labhomework
Exception: if you plan to do the lab in advance, you must submit your lab answer before the
start of your allocated lab section.
Instruction:
A java file is provided, please finish the required methods. You will be writing code in
the BST class to add more functionalities to it
Questions:
Write a method, public boolean search Comparable e BNode b that searches
for a key, e in a binary search tree. Think recursively. If root is null, or if root.data is
same as e the result is obvious. Otherwise, decide if you should search in the left subtree
or the right subtree, and this is where recursion comes in
Write a method, public void inorderscanBNode b that prints the inorder traversal of
the binary search tree.
Write a method, public void preorderscanBNode b that prints the preorder traversal of
the binary search tree.
Write a method, public void postorderscanBNode b that prints the postorder traversal
of the binary search tree. Example Code: public class labCS
public static void mainString args
Integer a;
Integer a;
Integer a;
Integer a;
Integer a;
Integer a;
Integer a;
Integer a;
Integer a;
Integer a;
Integer a;
BST bstnew BSTnew BNodea;
bstinserta;
bstinserta;
bstinserta;
bstinserta;
bstinserta;
bstinserta;
bstinserta;
bstinserta;
bstinserta;
bstinserta;
System.out.printlninorder Scanshould give ;
bstinorderscan;
System.out.println;
System.out.printlnpreorder Scanshould give ;
bstpreorderscan;
System.out.println;
System.out.printlnpostorder Scanshould give ;
bstpostorderscan;
System.out.println;
System.out.printlnHeight of the tree: bstheight;
System.out.println;
System.out.printEnter an int to search for: ;
java.util.Scanner sc new java.util.ScannerSystemin;
int pscnextInt;
ifbstsearchp bstroot
System.out.printlnFound;
else
System.out.printlnNot Found";
Class of Binary Search Tree, finish the methods in this class
class BST
BNode root;
public BSTBNode r
rootr;
public BST
rootnull;
public void insertComparable einsert node with data e
if rootnull root new BNodee;
else
if rootgetDatacompareToInteger e
rootdata is bigger, insert e into left subtree
insertNodee root;
else
rootdata is smaller, insert e into right subtree
insertNodee root;
public void insertNodeComparable e BNode b
if bgetDatacompareToInteger e
ifbleftnull
bsetLeftnew BNodee;
else
insertNodee bleft;
else
ifbrightnull
bsetRightnew BNodee;
else
insertNodee bright;
public int heightBNode bheight of the tree, this has been finished for you
ifbnull
return ;
ifbleft null && bright null
return ;
int lheightheightbleft;
int rheightheightbright;
return Math.maxlheight rheight;
public int height
return heightthisroot;
public boolean search Comparable e BNode b
return false;
public void inorderscanBNode binorder scan of the tree
public void inorderscan
this.inorderscanthisroot;
public void preorderscanBNode bpreorder scan of the tree
public void preorderscan
this.preorderscanthisroot;
public void postorderscanBNode bpostorder scan of the tree
public void postorderscan
this.postorderscanthisroot;
Binary Node
class BNode
public Comparable data;
public BNode left;
public BNode right;
public BNodeComparable data
this.datadata;
this.leftnull;
this.rightnull;
public BNodeComparable data, BNode l BNode r
this.datadata;
leftl;
rightr;
public void setDataComparable data
this.datadata;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
