Question

Given a binary search tree and a value k, implement a function to find the node...

Given a binary search tree and a value k, implement a function to find the node in the binary search tree whose value is closest to k.

Write the program in Java

Syntax: int lookup(Node node)

0 0
Add a comment Improve this question Transcribed image text
Answer #1

// class to store a value

class Val{

int value;

}

// Function to find node with minimum absolute

// difference with given K

// min_diff -. minimum difference till now

// min_diff_data -. node having minimum absolute

// difference with K

void lookupUtil(Node node, int k, Val min_diff,

Val min_diff_data)

{

if (node == NULL)

return ;

// If k itself is present

if (node.data == k)

{

min_diff_data.value = k;

return;

}

// update min_diff and min_diff_data by checking

// current node value

if (min_diff.value > Math.abs(node.data - k))

{

min_diff.value = Math.abs(node.data - k);

min_diff_data.value = node.data;

}

// if k is less than node.data then move in

// left subtree else in right subtree

if (k < node.data)

lookupUtil(node.left, k, min_diff, min_diff_data);

else

lookupUtil(node.right, k, min_diff, min_diff_data);

}

int lookup(Node root, int k)

{

// Initialize minimum difference

Val min_diff = new Val();

min_diff.value = Integer.INT_MAX

Val min_diff_data = new Val();

min_diff_data.value = -1;

// Find value of min_diff_data (Closest data

// in tree with k)

maxDiffUtil(root, k, min_diff, min_diff_data);

return min_diff_data.value;

}

Add a comment
Know the answer?
Add Answer to:
Given a binary search tree and a value k, implement a function to find the node...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Public IntBTNode find(int item) This function will attempt to find the item in the binary search ...

    write java function: public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item if the item is stored in the tree. If the item is not in the tree, the function will return nu public IntBTNode find(int item) This function will attempt to find the item in the binary search tree. It will return a reference to the node that contains Item...

  • Recall that in a binary search tree, at every node, all elements to the left of...

    Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...

  • IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity...

    IN JAVA 2 A Binary Search Tree The goal of this lab is to gain familiarity with simple binary search trees. 1. Begin this lab by implementing a simple class that represents a "node” in a binary search tree, as follows. public class MyTreeNode<t extends Comparable<T>> { public T data; public MyTreeNode<T> leftchild; public MyTreeNode<T> rightChild; public MyTreeNode<T> parent; 2. Have the second member of your pair type in the code for the simple binary search tree interface. public interface...

  • This is binary search tree problem. The program reads the text file, and creates a binary...

    This is binary search tree problem. The program reads the text file, and creates a binary search tree based on the words in the file. I can create the tree but I also have to store 'the order of insertion' in each node.   For example, if text includes "the" 3 times and it is the 1st, 5th, and 9th word in the file, in binary search tree, one node should have string value "the" and array list{1, 5, 9}. How...

  • in python 11.1 Binary Search Tree In this assignment, you will implement a Binary Search Tree...

    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...

  • 1. Construct a Binary Search Tree 50 Write code to implement a BST. Implement an add) method and ...

    C++ program 1. Construct a Binary Search Tree 50 Write code to implement a BST. Implement an add) method and a remove) method. Use the following input to construct a BST: 50,20,75,98,80,31,150, 39, 23,11,77 20 75 31 98 0 (150 Hint on removing If the node to be removed is: a leaf node a node with a left child only a node with a right child only a node with both children Then take this action replace with null replace...

  • Java : This function is to search through a binary tree left and right and return...

    Java : This function is to search through a binary tree left and right and return a count of the nodes above depth k. This is what I have so far, but I'm getting a Null pointer exception. public class MyIntSET {    private Node root;    private static class Node {        public final int key;        public Node left, right;        public Node(int key) { this.key = key; }    }    public int sizeAboveDepth(int...

  • C++ (Using Binary Search Trees) other methods will result in downvote Implement the binary search tree...

    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>...

  • Binary Search Tree Part A: The code attached in this document is a sample code to demonstrate ins...

    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++ Let's say sub_root is a node in a given binary search tree. Write a code...

    c++ Let's say sub_root is a node in a given binary search tree. Write a code segment to find the immediate successor of the sub_root

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT