Question

Write a Java function named smallcount that, given the pointer to the root of a Binary...

Write a Java function named smallcount that, given the pointer to the root of a Binary Search Tree (BST) and a key K, returns the number of nodes having key values less than or equal to K. Function smallcount should visit as few nodes in the BST as possible.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
int bstsmallCount(BinNode root , int key) { if(root = = null) return 0; if((Integer)root.element() < key) return 1 + bstsmallCount(root.left(),key) + bstsmallCount(root.right(),key) 
Add a comment
Know the answer?
Add Answer to:
Write a Java function named smallcount that, given the pointer to the root of a Binary...
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
  • CODE IN JAVA** V. Given a pointer to the root of a binary tree and a...

    CODE IN JAVA** V. Given a pointer to the root of a binary tree and a pointer ‘p’ to a given node in the tree and a second pointer ‘q’ to another node in the tree write a routine which will return the total number of nodes in the tree that are on level ‘p’ and ‘q’. If they are on the same level you should multiply the answer by 3.

  • in javascrift please Given a pointer to the root of a binary tree write a routine...

    in javascrift please Given a pointer to the root of a binary tree write a routine that will delete every node in the tree that is currently a leaf (has no sons), and when finished tell you how many nodes were deleted as well as how many nodes are left in the tree.

  • in java please thanks! Given a pointer to a binary tree write a routine which will...

    in java please thanks! Given a pointer to a binary tree write a routine which will traverse the tree and return the number of nodes in the tree whose information field which is an integer is between 59 and 112 In addition return the value of the node with the largest integer and as well return a count of the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and...

  • java please IV. Given a pointer to a binary tree write a routine which will traverse...

    java please IV. Given a pointer to a binary tree write a routine which will traverse the tree and return the 59 and 112 number of nodes in the tree whose information field which is an integer is between In addition return the value of the node with the largest integer and as well return a count or the number of nodes that have no sons. For the 3rd part do not include nodes that have both right and left...

  • In C++ Given a pointer to the root of a binary search tree (has left, right,...

    In C++ Given a pointer to the root of a binary search tree (has left, right, and parent pointers as well as a data section ) write a function (or functions) which will return an STL list (you should not define this class, it’s already included) with all of the values from the tree in sorted order. Your code should run in theta(N) time. for the second part,.given a pointer to the first node of a linked list, you are...

  • Write a function int levelSearch(Node* root, int key) that takes as input the root node of...

    Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom. We have defined the following node C++ Node class for you: class Node { public:         ...

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

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

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

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