Find the median in a given BST with n nodes. The code should take O(H) time, where is the height of the BST. You can assume that the size of the tree is an odd number.
Class Node{
int data;
int size;//size of the subtree rooted at this node, including thi node.
Node left, right;
}
class BST{
Node root;
Int median(){
Int answer =0;
Return answer;//this contains the median in T
}
}//end class BST
IDEA:
1)first we will find inorder traversal of the BST.
2)then count the total number of nodes.
3)If nodes is even: then
median = (middle node+ very next node) /2
4)If nodes is odd : then
median = (very next node to middle node) node.
CODE:
Class Node{
int data;
int size;//size of the subtree rooted at this node, including thi node.
Node left, right;
}
class BST{
Node root;
static int counNodes(Node root)
{
Node current, pre;
int count = 0;
if (root == null)
return count;
current = root;
while (current != null)
{
if (current.left == null)
{
// Count node if its left is NULL
count++;
// Move to its right
current = current.right;
}
else
{
/* Find the inorder predecessor of current */
pre = current.left;
while (pre.right != null &&
pre.right != current)
pre = pre.right;
/* Make current as right child of its
inorder predecessor */
if(pre.right == null)
{
pre.right = current;
current = current.left;
}
else
{
pre.right = null;
// Increment count if the current
// node is to be visited
count++;
current = current.right;
} /* End of if condition pre->right == NULL */
} /* End of if condition current->left == NULL*/
} /* End of while */
return count;
}
Int median(node root){
Int answer =0;
if (root == null)
return 0;
int count = counNodes(root);
int currCount = 0;
Node current = root, pre = null, prev = null;
while (current != null)
{
if (current.left == null)
{
// count current node
currCount++;
// check if current node is the median
// Odd case
if (count % 2 != 0 && currCount == (count+1)/2) {
answer=prev.data;
return answer;
}
// Even case
else if (count % 2 == 0 && currCount == (count/2)+1)
{ answer= (prev.data + current.data)/2;
return answer;
} // Update prev for even no. of nodes
prev = current;
//Move to the right
current = current.right;
}
else
{
/* Find the inorder predecessor of current */
pre = current.left;
while (pre.right != null && pre.right != current)
pre = pre.right;
/* Make current as right child of its inorder predecessor */
if (pre.right == null)
{
pre.right = current;
current = current.left;
}
/* Revert the changes made in if part to restore the original
tree i.e., fix the right child of predecssor */
else
{
pre.right = null;
prev = pre;
// Count current node
currCount++;
// Check if the current node is the median
if (count % 2 != 0 && currCount == (count+1)/2 )
{ answer= current.data;
return answer;
}
else if (count%2==0 && currCount == (count/2)+1)
{ answer= (prev.data+current.data)/2;
return answer;
}
// update prev node for the case of even
// no. of nodes
prev = current;
current = current.right;
} /* End of if condition pre->right == NULL */
} /* End of if condition current->left == NULL*/
} /* End of while */
return -1;
}
}//end class BST
Find the median in a given BST with n nodes. The code should take O(H) time,...
Question B1 You are given the following Java classes: public class Queue { private static class Node { Object object; Node next; Node () { object = null; next = null; } Node (Object object, Node next) { this.object = object; this.next = next; private Node header; private int size = 0; // size shows the no of elements in queue public Object dequeue () { if (size == 0 ) { return null; else { Object remove_object = header.object;...
Add a public member function to the BST class below that returns the size of the tree—the number of the nodes in the tree. For simplicity, the class contains only the key and not a value. You may add any helper functions you find necessary. (Hint: think recursion) template <typename T> struct Node { T key; Node<T>* left; Node<T>* parent; Node<T>* right; }; template <typename T> class BST { private: Node<T>* root; public: BST():...
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...
package hw3; import java.util.LinkedList; /* *********************************************************************** * A simple BST with int keys and no values * * Complete each function below. * Write each function as a separate recursive definition (do not use more than one helper per function). * Depth of root==0. * Height of leaf==0. * Size of empty tree==0. * Height of empty tree=-1. * * TODO: complete the functions in this file. * DO NOT change the Node class. * DO NOT change the name...
In Java: Given the following binary tree class, write a recursive method called size() which will find the number of nodes in the subtree rooted at the current node: class myBinaryTree{ int myValue; myBinaryTree left; myBinaryTree right; myBinaryTree(int inValue) {myValue = inValue;} public int size(){ <CODE WRITTEN HERE> } }
The code is in JAVA
public class CheckBST {
//method to implement
public static boolean isValidBST(TreeNode root) {
}
public static void main(String[] args) {
TreeNode a = new TreeNode(1);
TreeNode b = new TreeNode(2);
TreeNode c = new TreeNode(3);
a.left = b;
a.right = c;
System.out.println(isValidBST(a));
TreeNode d = new TreeNode(2);
TreeNode e = new TreeNode(1);
TreeNode f = new TreeNode(3);
d.left = e;
d.right = f;
System.out.println(isValidBST(d));
}
}
TreeNode.java
class TreeNode {
int val;
TreeNode left;
TreeNode...
Would appreciate the answer in the Java coding language please
and thank you!
10d 10h left Java 7 1. Check the Structure Autocomplete Ready 1 > import java.io.*;... 10 ALL A binary tree uses a multi-node data structure where each node may have 0 to 2 child nodes, and has one stored value, its node number in this case. A tree may either be: 11 class Result { * Complete the 'isValid' function below. • An empty tree, the root...
In need of JAVA CODE FOR THIS OBJECTIVE: Modify the BST class I gave you as follows: In the case in which the deleted node has two children, write the code in two ways. a) always replace the deleted node with the largest node on the left. b) count how many deletions have been done, and for even deletions replace the deleted node with the largest node on the left, for odd deletions, replace the deleted node with the smallest...
You should now be able to edit the IntTree class. Implement each of the functions labeled with You are not allowed to use any kind of loop in your solutions. You may not modify the Node class in any way You may not modify the function headers of any of the functions already present in the file. You may not add any fields to the IntTree class. You may not change or remove the line that reads “package hw2;”...
Language: C++ ○ For this question I task you with creating an Iterative Search function that works with the existing binary search tree of type string. This function will be inputted a variable of type string and will search for it within the existing and pre-declared binary tree. ○ The function appears just above the main, and is simply a placeholder. It can be altered as you wish. ○ Within the Main I have a simple program to prompt for...