Question

Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty...

Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes:

• The key T.key is the root node’s key.

• The left child T.left is T’s left subtree, which is an AVL tree (possibly E).

• The right child T.right is T’s right subtree, which is an AVL tree (possibly E).

Describe an alternative version of the RangeCount(T, lo, hi) function that runs in O(log n) time.

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

The idea is to traverse the given binary search tree starting from root. For every node being visited, check if this node lies in range, if yes, then add 1 to result and recur for both of its children. If current node is smaller than low value of range, then recur for right child, else recur for left child.

Since you have not mentioned the language of your preference, I am providing the code in C++.

CODE

// Returns count of nodes in BST in range [low, high]

int RangeCount(node *root, int low, int high)

{

    // Base case

    if (!root) return 0;

  

    // Special Optional case for improving efficiency

    if (root->data == high && root->data == low)

        return 1;

  

    // If current node is in range, then include it in count and

    // recur for left and right children of it

    if (root->data <= high && root->data >= low)

         return 1 + RangeCount(root->left, low, high) +

                    RangeCount(root->right, low, high);

  

    // If current node is smaller than low, then recur for right

    // child

    else if (root->data < low)

         return RangeCount(root->right, low, high);

  

    // Else recur for left child

    else return RangeCount(root->left, low, high);

}

Time complexity:

Time complexity of the above program is O(log n).

Add a comment
Know the answer?
Add Answer to:
Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty...
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
  • Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three...

    Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: • The key T.key is the root node’s key. • The left child T.left is T’s left subtree, which is an AVL tree (possibly E). • The right child T.right is T’s right subtree, which is an AVL tree (possibly E). (a) [5 marks] Write a function RangeCount(T, lo, hi) to count the number of nodes in an...

  • Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three...

    Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes The key T.key is the root node's key. The left child T.left is Ts left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E). (a) 5 marsl Write a function RANGECOUNT(T, lo, hi) to count the number of nodes in an AVL tree with...

  • just need to answer the second question 3 AVL Trees Assume the following notation/operations on AVL trees....

    just need to answer the second question 3 AVL Trees Assume the following notation/operations on AVL trees. An empty AVL tree is denoted E. A non-empty AVL tree T has three attributes: . The key T.key is the root node's key. The left child T.left is T's left subtree, which is an AVL tree (possibly E). The right child T.right is T's right subtree, which is an AVL tree (possibly E) [3 marks] Describe an alternative version of the RANGECOUNT(T,...

  • 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 Trees Problem 4. Binary Trees. [15 marks total Recall that a binary tree is defined...

    Binary Trees Problem 4. Binary Trees. [15 marks total Recall that a binary tree is defined as a fintie set of nodes that is either empty or consists of a root and two disjoint binary trees T and TR, respectively, the left and right subtree of the root. Since the definition itself divides a binary tree into two smaller structures of the same type, the left and the right subtree, many problems about binary trees can be solved by applying...

  • The task here is to show a trace of the operations needed to insert objects with your (list of) keys, one by one, into an initially empty AVL tree with restoration of AVL balance (if necessary) after each insertion for Key: [74, 92, 75, 46, 60, 3, 90, 78,

    [74, 92, 75, 46, 60, 3, 90, 78, 7]The task here is to show a trace of the operations needed to insert objects with your (list of) keys, one by one, into an initially empty AVL tree with restoration of AVL balance (if necessary) after each insertion.Your submission should have the section heading 'AVL trace' followed by the coded trace of operations:􀁸 Ixx to insert key xx at the root of the previously empty AVL tree;􀁸 IxxLyy to insert key...

  • Hello. I have written the following function to remove a value from a Binary Search Tree using re...

    Hello. I have written the following function to remove a value from a Binary Search Tree using resources my professors gave and stuff I found online. The problem is I don't know how it works and I need to know how it works to finish the rest of the project. public boolean remove(T value){ if(value == null) return false; class RemoveBSTObj{ private boolean found = false; private Node removeBST(Node root, T value){ if(root == null) return root; //IF there is...

  • The first and second picture are the definition of 2-3 tree,3rd and 4th are the pre-condition...

    The first and second picture are the definition of 2-3 tree,3rd and 4th are the pre-condition and post-condition. Please use these question to solve problem 8,the last photo. 2-3 Trees: Definition Suppose that E is an ordered type, that is, a nonempty set of values that have a total order. A 2-3-tree, for type E, is a finite rooted tree T (like a binary search tree or a red-black tree) that satisfies the following 2-3 Tree Properties: (a) Every leaf...

  • ///Program needs to write in prolog/// 6. A binary tree is either empty or it is...

    ///Program needs to write in prolog/// 6. A binary tree is either empty or it is composed of a root element and two successors, which are binary trees themselves. In Prolog we represent the empty tree by the atom 'nil' and the non-empty tree by the term t(X,L,R), where X denotes the root node and L and R denote the left and right subtree, respectively. The following Prolog term represents the given binary tree below. T1 = t(a,t(b,t(d,nil,nil),t(e,nil,nil)),t(c,nil,t(f,t(g,nil, nil),nil))) d...

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