Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Lab 6, 7 Trees, Binary search trees Lab 6 Task 1 We define the following class Node class Node{ int data; Node left; Node right;
Lab 6, 7 Trees, Binary search trees Lab 6 Task 1 We define the following class Node class Node\{ int data; Node left; Node right; public Node(int data) \{ this.data=data; left = null; right = null; \} public String toString { retum datat nn; \} Task 2 Define the class BST public class Bst \{ Node root; public Bsto \{ root = null; \} Task 3 Complete the iterative implementation below to insert a key in a BST: public void insert(int x ) \{ Node n= new Node(x); \} Task 4 Complete the recursive implementation below to insert a key in a BST: private void insert R (Node starh int x){ 3 public void insertRec(int x){ insertR(root, x ); \} Task 5 (Done) Complete the implementation below to search for the minimum in a BST: private int minimum(Node root) \{ int min= root data; while (root.left != nuli) min= root.left.data; root = root.left; \} return min; \} Task 6 (Done) Complete the implementation below to delete an element in a BST: private Node delete_Recursive(Node root, int key)\{ if (root = null) return root; if (key // traverse left subtree root.left = delete_Recursive(root.left, key); else if (key > root.data) //traverse right subtree root.right = delete_Recursive(root.right, key); else \{ I/ node contains only one child if (root.left = null) return root.right; else if (root.right = null) return root.left; root.data = minimum(root.right); root,right = delete_Recursive(root.right, root.data); \} retum root; 3 void delete(int key) \{ root = delete_Recursive(root, key)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access with AI-Powered 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