Main.java
public class Main {
public static void main(String [] args){
MyBST b=new MyBST();
b.insert(5);
b.insert(18);
b.insert(0);
b.insert(16);
b.insert(29);
b.insert(10);
b.insert(17);
b.insert(23);
b.insert(28);
b.insert(21);
b.insert(22);
b.insert(3);
b.insert(2);
b.insert(4);
b.insert(1);
b.insert(-4);
b.insert(-3);
System.out.println("Printing inOrder:");
b.printInOrder();
System.out.println("Printing preOrder:");
b.printPreOrder();
System.out.println("Printing postOrder:");
b.printPostOrder();
System.out.println(b.lookup(2));
b.delete(5);
b.delete(0);
b.delete(-4);
System.out.println("Printing inOrder:");
b.printInOrder();
}
}
MyBST.java
public class MyBST implements BST{
MyTreeNode root;
public MyBST(){
root=null;
}
public void insert(Comparable x) {
if(root!=null){
root.insert(x);
}
else{
root=new
MyTreeNode(x);
}
}
public void printPreOrder() {
root.printPreOrder();
}
public void printInOrder() {
root.printInOrder();
}
public void printPostOrder() {
root.printPostOrder();
}
@Override
public boolean lookup(Comparable x) {
if(root==null){
return
false;
}
return root.lookup(x);
}
@Override
public void delete(Comparable x) {
if(root==null){
return;
}
root.delete(x);
}
}
BST.java
public interface BST<T extends Comparable <T>> {
public void insert(T x);
public void delete(T x);
public boolean lookup(T x);
public void printPreOrder();
public void printInOrder();
public void printPostOrder();
}
MyTreeNode.java
public class MyTreeNode<T extends Comparable<T>>
{
public T data;
public MyTreeNode<T> leftChild;
public MyTreeNode<T> rightChild;
public MyTreeNode<T> parent;
public MyTreeNode(T data){
this.data=data;
leftChild=null;
rightChild=null;
parent=null;
}
public void insert(T x){
MyTreeNode newnode=new
MyTreeNode(x);
if(x.compareTo(data)>0){
if(rightChild==null){
rightChild=newnode;
newnode.parent=this;
}
if(rightChild!=null){
rightChild.insert(x);
}
}
if(x.compareTo(data)<0){
if(leftChild==null){
leftChild=newnode;
newnode.parent=this;
}
if(leftChild!=null){
leftChild.insert(x);
}
}
}
public boolean lookup(T x){
if(data.compareTo(x)==0){
return
true;
}
else
if((x.compareTo(data)>0)&&(rightChild!=null)){
return
rightChild.lookup(x);
}
else
if((x.compareTo(data)<0)&&(leftChild!=null)){
return
leftChild.lookup(x);
}
else{
return false;
}
}
public void delete(T x){
//System.out.println("I am
called!");
if(!lookup(x)){
//System.out.println("The item you want to delete is not in the
tree.");
return;
}
//System.out.println("In
between");
if(lookup(x)){
if(data.compareTo(x)==0){
if(parent==null){
if((leftChild==null)&&(rightChild==null)){
this.data=null;
}
if(((leftChild==null)&&(rightChild!=null))){
rightChild.parent=null;
}
if(((leftChild!=null)&&(rightChild==null))){
leftChild.parent=null;
}
if(((leftChild!=null)&&(rightChild!=null))){
MyTreeNode
tempnode=rightChild;
if(rightChild.leftChild!=null){
tempnode=rightChild.leftChild;
while(tempnode.leftChild!=null){
tempnode=tempnode.leftChild;
}
}
Comparable
theswitch=tempnode.data;
delete((T)
theswitch);
this.data=(T) theswitch;
}
}
//if i have no children
else
if((leftChild==null)&&(rightChild==null)){
if(data.compareTo(parent.data)<0){
parent.leftChild=null;
//System.out.println("parent
of left child to null!");
}
if(data.compareTo(parent.data)>0){
parent.rightChild=null;
//System.out.println("parent of right child to null!");
}
}
//if I am the left baby
else
if(parent.leftChild.data.compareTo(x)==0){
if((leftChild!=null)&&(rightChild==null)){
parent.leftChild=leftChild;
}
if((leftChild==null)&&(rightChild!=null)){
parent.leftChild=rightChild;
}
if((leftChild!=null)&&(rightChild!=null)){
MyTreeNode
tempnode=rightChild;
if(rightChild.leftChild!=null){
tempnode=rightChild.leftChild;
while(tempnode.leftChild!=null){
tempnode=tempnode.leftChild;
}
}
Comparable
theswitch=tempnode.data;
delete((T)
theswitch);
this.data=(T) theswitch;
}
}
//i am the right baby
else
if(parent.rightChild.data.compareTo(x)==0){
if(((leftChild==null)&&(rightChild!=null))){
parent.rightChild=rightChild;
}
if((leftChild!=null)&&(rightChild==null)){
parent.rightChild=leftChild;
}
if((leftChild!=null)&&(rightChild!=null)){
MyTreeNode
tempnode=rightChild;
if(rightChild.leftChild!=null){
tempnode=rightChild.leftChild;
while(tempnode.leftChild!=null){
tempnode=tempnode.leftChild;
}
}
Comparable
theswitch=tempnode.data;
delete((T)
theswitch);
this.data=(T) theswitch;
}
}
}
if(data.compareTo(x)<0){
//System.out.println("Right deleting");
rightChild.delete(x);
}
if(data.compareTo(x)>0){
//System.out.println("Left deleting");
leftChild.delete(x);
}
}
}
public void printInOrder(){
if(leftChild !=null){
leftChild.printInOrder();
}
System.out.println(data);
if(rightChild!=null){
rightChild.printInOrder();
}
}
public void printPreOrder(){
System.out.println(data);
if(leftChild !=null){
leftChild.printPreOrder();
}
if(rightChild!=null){
rightChild.printPreOrder();
}
}
public void printPostOrder(){
if(leftChild !=null){
leftChild.printPostOrder();
}
if(rightChild!=null){
rightChild.printPostOrder();
}
System.out.println(data);
}
}
IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...
using java to write,show me the output. please write some common. You CAN NOT use inbuild functions for Tree ADT operations. using code below to finsih public class Main { public static void main(String[] args) { BinaryTree tree = new BinaryTree(); tree.root = new Node(1); tree.root.left = new Node(2); tree.root.right = new Node(3); tree.root.left.left = new Node(4); tree.root.left.right = new Node(5); tree.root.right.left = new Node(6); tree.root.right.right = new Node(7); tree.root.left.left.left = new Node(8); tree.root.left.left .right= new Node(9);...
In this assignment, you will add several methods to the Binary Search Tree. You should have completed the following three methods in the lab: public void insert(Key key, Value value) public Value get(Key key) public void inorder(Node root) For this assignment, you will implement the following: public void remove(Node root, Key key) public Key getMin(Node n) public Key getMax(Node n) public int height(Node n) The main method contains the statements to check whether your implementation works. You need to change...
Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate insert operation in binary search tree. Please fill in the missing part for the insert method to make the program work. The expected output should be as follows. 20 30 40 50 60 70 80 Part B: Find Lowest Common Ancestor (LCA) of a Binary Search Tree. According to WikiPedia definition , The lowest common ancestor is defined between two nodes v and...
C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree methods (bst.cpp) for the binary search tree provided in the header file. Test your implementation with the included test. bst.h bst_test.cpp Note: Your implementation must correspond to declarations in the header file, and pass the test. Do not modify these two. I will compile your code against these. If the compilation fails, you will get down vote. bst.h #ifndef BINARY_SEARCH_TREE_H #define BINARY_SEARCH_TREE_H #include <string>...
*****************************In Java***************************************In Java***********************************In Java************************* In this problem, you will implement various algorithms operating on binary search trees. We have provided with you a standard implementation of a generic BST in BinarySearchTree.java. Note that this class is an abstract class, which means that some of its methods are not implemented. In previous assignments, you have implemented interfaces which specified methods that you needed to write. Very similarly, an abstract class is a class with some unimplemented methods (it can be thought...
in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree You will also need to implement a Node class. This class will not be tested, but is needed to implement the BST. Your BST must implement the following methods. You are free to implement additional helper methods. It is recommended you create your own helper methods Constructor: Creates an Empty Tree String Method: Returns the string "Empty Tree" for an empty tree. Otherwise, returns...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
Need this in C++ Goals: Your task is to implement a binary search tree of linked lists of movies. Tree nodes will contain a letter of the alphabet and a linked list. The linked list will be an alphabetically sorted list of movies which start with that letter. MovieTree() ➔ Constructor: Initialize any member variables of the class to default ~MovieTree() ➔ Destructor: Free all memory that was allocated void printMovieInventory() ➔ Print every movie in the data structure in...
From the code below with Binary Search Tree recurrence T(n)=? use the recursion method and substitution method to solve the recurrence. Find the tightest bound g(n) for T(n) you can for which T(n)= O(g(n)). Explain your answer and use recursion tree method. void insert(int data) { struct node *tempNode = (struct node*) malloc(sizeof(struct node)); struct node *current; struct node *parent; tempNode->data = data; tempNode->leftChild = NULL; tempNode->rightChild = NULL; //if tree is empty if(root == NULL) { root = tempNode;...
C++ Binary Search Tree question. I heed help with the level 2 question please, as level 1 is already completed. I will rate the answer a 100% thumbs up. I really appreciate the help!. Thank you! searching.cpp #include <getopt.h> #include <iostream> #include <sstream> #include <stdlib.h> #include <unistd.h> using namespace std; // global variable for tree operations // use to control tree maintenance operations enum Mode { simple, randomised, avl } mode; // tree type // returns size of tree //...