Question

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 AVL tree with root T, where the key value is in the range lo ≤ key ≤ hi. Your algorithm should run in O(n) time, assuming that n is the number of nodes in the AVL tree, T.
(b) [3 marks] Describe an alternative version of the RangeCount(T, lo, hi) function that runs in O(log n) time.
• You do not have to write an algorithm in pseudo code to answer part (b) of this question. We are expecting that you write a short paragraph or a short list of bullet points describing the important steps of the algorithm.
• In your answer, you may augment the AVL tree notation/operations listed above. For example, you may include additional node attributes such as T.bal indicating the balance factor of the AVL tree, or T.sum indicating the sum of all key values in the tree with root T, and possibly even T.parent that points to the parent node of current root, T. If you elect to do so, please make sure that you explain your representation.

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

SOLUTION TO PART A

THE idea is very simple travel through each node of the tree .. if the key is in between the range the rise the count...

int Range(node *T, int lo, int hi)
{
if (!T) return 0;
if (T->key == hi && T->key == lo)
return 1;
if (T->key <= hi && T->key >= lo)
return 1 + Range(T->left, lo, hi) +
Range(T->right, lo, hi);
  
else if (T->key < lo)
return Range(T->right, lo, hi);
  
else return Range(T->left, lo, hi);
}

SOLUTION TO PART B

let a=no of nodes with a key greater than lo

b= no of nodes with a key greater than hi

then c= a-b ; // gives the count of required no of nodes between the range of lo and hi

int Range1( Node* T, int x)
{
int res = 0;
while (T != NULL) {
int desc = (T->right != NULL) ? T->right->desc : -1;
if (T->key > x) {
res = res + desc + 1 + 1;
T = T->left;
}
       else if (T->key < x)
T = T->right;
else {
res = res + desc + 1;
break;
}
}
return res;
}

int Range(node*T,int lo,int hi){

int a=Range1(T,lo-1);

int b=Range1(T,hi);

return a-b;

}

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 AVL tree T has three...
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 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...

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

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

  • (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL)...

    (Please help me with Coding in Python3) AVLTree complete the following implementation of the balanced (AVL) binary search tree. Note that you should not be implementing the map-based API described in the plain (unbalanced) BSTree notebook — i.e., nodes in the AVLTree will only contain a single value. class AVLTree: class Node: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right def rotate_right(self): n = self.left self.val, n.val = n.val, self.val self.left, n.left, self.right, n.right...

  • Implement an AVL tree stored in a random access file Each node contains an integer key,...

    Implement an AVL tree stored in a random access file Each node contains an integer key, one or more fixed length character strings, a left child reference, a right child reference and a height The format of the file is shown on the next slide The methods you must implement are shown on the followingslides. Duplicate keys cannot be entered in the tree Your implementation MUST NOT load the whole tree in memory. Each operation only makes copies of the...

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

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