Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please help Below is my code in java. class BinarySearchTree { class Node { int key; Node left, right; public Node(int item) { key =

Please help

image text in transcribed

Below is my code in java.

class BinarySearchTree {

class Node {

int key;

Node left, right;

public Node(int item) {

key = item;

left = right = null;

} }

// Root of BST

Node root;

// Constructor

BinarySearchTree() {

root = null;

}

// This method mainly calls insertRec()

void insert(int key) {

root = insertRec(root, key);

}

/* A recursive function to insert a new key in BST */

Node insertRec(Node root, int key) {

/* If the tree is empty, return a new node */

if (root == null) {

root = new Node(key);

return root;

}

/* Otherwise, recur down the tree */

if (key

root.left = insertRec(root.left, key);

else if (key > root.key)

root.right = insertRec(root.right, key);

/* return the (unchanged) node pointer */

return root;

}

// This method mainly calls InorderRec()

void inorder() {

inorderRec(root);

}

// A utility function to do inorder traversal of BST

void inorderRec(Node root) {

if (root != null) {

inorderRec(root.left);

System.out.println(root.key);

inorderRec(root.right);

}

}

// Driver Program to test above functions

public static void main(String[] args) {

BinarySearchTree tree = new BinarySearchTree();

tree.insert(50);

tree.insert(30);

tree.insert(20);

tree.insert(40);

tree.insert(70);

tree.insert(60);

tree.insert(80);

// print inorder traversal of the BST

tree.inorder(); } }

l. Implement Search T data in Bin to search for a node having value of key data in a Binary Search Tree. See the algorithm presented below. TREE-SEARCH(x, k) 1 if x NIL or k x.key return x 3 if k x. key 4 return TREE-SEARCH (x.left, k) 5 else return TREE-SEARCH(x.right, k) 2. Implement Delete (T data) in Binary SearchTreejava to search for a node having value of key data in a Binary Search Tree. See the two algorithms presented below. TREE-DELETE (T, z) 1 if left NIL 2 TRANSPLANT(T, z, z. right 3 elseif z right NIL 4 TRANSPLANT(T, z, z. left) 5 else y B TREE-MINIMUM(z.right) TRANSPLANT(T,y,y.right) y. right z. right y right p y 10 TRANSPLANT(T, z, y) 12 y.left.p CS303 Lab Assignment 6 of 7 TRANSPLANT(T, u, v) 1 if u.p NIL Troot v 3 elseif u u.p left 5 else u.p. right v 6 if v NIL u.P 3. Exercise your Binary Search Tree by selectively inserting and removing items, and using In-order-Traversal at each step to show the tree's contents at each step At the maximum, your tree should contain at least ten objects

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Databases On The Web Designing And Programming For Network Access

Authors: Patricia Ju

1st Edition

1558515100, 978-1558515109

More Books

Students also viewed these Databases questions

Question

1. What is the predominant purpose test? 2. Why is this important?

Answered: 1 week ago

Question

How many states in India?

Answered: 1 week ago

Question

HOW IS MARKETING CHANGING WITH ARTIFITIAL INTELIGENCE

Answered: 1 week ago

Question

Different types of Grading?

Answered: 1 week ago

Question

Explain the functions of financial management.

Answered: 1 week ago