Question

Let T be a binary search tree. Show how to implement the following operation on T: countAlllnRange(Key lower, Key upper)...

Let T be a binary search tree. Show how to implement the following operation on T:

countAlllnRange(Key lower, Key upper): Compute and return the number of items in T with key k such that “lower ≤ k ≤ upper”.

Assumption: The ADT Node offers a method “key” which returns the key value of the respective node.

Give the running time of the operation in Big-O notation.

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

we can do this efficiently using binary tree traversal:

we use inorder traversal as the tree is binary search tree, ans inorder in a binary search tree is sorted :

Algorithm:
initialize tree
Inorder(tree)
Traverse the left subtree, i.e., call Inorder(left-subtree)
Visit the key
Traverse the right subtree, i.e., call Inorder(right-subtree)

thus given algorithm can be designed as:

initialize a variable count=0
countAlllnRange(key current_node=tree starting node,Key lower, Key upper,flag=0):
countAlllnRange(current node.left,lower,upper,flag)
if(flag==0)
if(key is greater than lower && current_node.key() is less than upper):
count++
flag=1
else if(flag==1 && current_node.key() is less than upper)
count++
else if(flag==1)
flag=2
return
else if(flag==2)
return
countAlllnRange(current node.right,lower,upper,flag)

here we take advantage of binary search tree and thus start counting when current key is greater than lower and less than upper
and end when we reach a node where key is greater than upper and return

note the value of variable flag:

when flag=0 means all keys till now are less than lower
when flag=1 means we have begin the count and if current key is less than upper keep counting
when flag=2 means return unconditionally because we have reached a key which is greater than upper


running time of this algorithm is O(n)
as in worst case all keys are within the range(lower,upper)

COMMENT DOWN FOR ANY QUERIES AND,

LEAVE A THUMBS UP IF THIS ANSWER HELPS YOU.

Add a comment
Know the answer?
Add Answer to:
Let T be a binary search tree. Show how to implement the following operation on T: countAlllnRange(Key lower, Key upper)...
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
  • Algorithms and Data Structures Let T be a binary search tree which implements a dictionary. Let...

    Algorithms and Data Structures Let T be a binary search tree which implements a dictionary. Let v be a node of T, and T_v be the subtree rooted at v. Design a recursive algorithm CountLE(v, k) which, given an input node v and a key k, returns the number of entries in T_v with key at most k.

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

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

  • Write a recursive function that returns the minimum key value in a binary search tree of...

    Write a recursive function that returns the minimum key value in a binary search tree of distinct (positive) integers. Return -1 in case the tree is empty. (b) Write a recursive function that returns the predecessor of the key value k in a binary search tree of distinct (positive) integers. This is the key value that precedes k in an inorder traversal of the tree. If k does not exist, or if k has no predecessor, your function should return...

  • In this assignment, you will add several methods to the Binary Search Tree. You should have compl...

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

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

  • 3. [5 marks] Suppose T is a binary tree that stores an integer key value in...

    3. [5 marks] Suppose T is a binary tree that stores an integer key value in each node. Assume the following notation/operations on a binary tree. » the key T.key is the root node's integer key value . the left child T.left is T's left subtree, which is possibly an empty tree (or null) the right child T.right is T's right subtree, which is possibly an empty tree (or null) (a) Write an efficient algorithm FINDMAxPrODuCT(T) in pseudocode that returns...

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

  • This is for java programming. We need a public method for our Binary Search Tree ADT...

    This is for java programming. We need a public method for our Binary Search Tree ADT that returns a reference to the information in the node with the "smallest" value in the tree. The signature of the method is public T min() a. Design an iterative version of the method. b. Design a recursive version of the method. c. Which approach is better? Explain.

  • write the method that return the kth smallest item in the binary search tree. for example...

    write the method that return the kth smallest item in the binary search tree. for example if the tree has 18 node and the user enter 13 then the method should return the 13 smallest element in the tree. I has a method that done it recursively but I want to Implement findKth nonrecursively public String findKth( String k ) { return findKth( k, root).data; } public Node findKth(int k, Node t ) {             if( t...

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