Question

In: Computer Science

Given a binary search tree T with n elements. For any two keys k1 and k2...

Given a binary search tree T with n elements. For any two keys k1 and k2 such that k1 < k2, there is an algorithm to print all elements x in T such that k1 ≤x ≤k2 in O(K + log n) time on average, where K is the number of the elements printed out.

Solutions

Expert Solution

Solution

Required algorithm is given below. This algorithm is executed starting from the root node recursively until the condition is false.

Algorithm

if currentNode is not NULL

if k1 < key in currentNode
Left subtree of the currentNode may have some elements in the range between k1 to k2    
Consider the root of the left subtree as current node and follow the algorithm from step 1

if k1 < key in currentNode AND k2>= key
Print the key in currentNode

   if key <= k2 in currentNode
Right subtree of the currentNode may have some elements in the range between k1 to k2. Consider the root of the right subtree as current node and follow the algorithm from step 1

This algorithm requires O(K) to perform the inorder traversal and print K keys. We may have to traverse to the depth of the tree if more number of keys satisfying the condition are found which are proportional to the depth of the tree.   Since the average depth is O(log N), this gives an O(K + log N) average bound.


Related Solutions

Given a binary search tree T with n elements. For any two keys k1 and k2...
Given a binary search tree T with n elements. For any two keys k1 and k2 such that k1 < k2, there is an algorithm to print all elements x in T such that k1 ≤x ≤k2 in O(K + log n) time on average, where K is the number of the elements printed out.
Write the binary tree representation for the Binary Search for also 17 elements and give the...
Write the binary tree representation for the Binary Search for also 17 elements and give the worst-case
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree...
Prerequisite Knowledge Understand binary search tree structure Understand binary search tree operations Understand binary search tree worst case and best case time. Learning Outcomes Describe AVL tree structure Trace and implement AVL tree operations Explain and prove AVL tree performance
Binary Tree Create a binary search tree using the given numbers in the order they’re presented....
Binary Tree Create a binary search tree using the given numbers in the order they’re presented. State if the resulting tree is FULL and/or BALANCED. 37, 20, 18, 56, 40, 42, 12, 5, 6, 77, 20, 54
​Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Define a tree. Distinguish between a tree and a binary tree. Distinguish between a binary tree and a binary search tree.
Beginning with an empty binary search tree, what binary search tree is formed when you insert...
Beginning with an empty binary search tree, what binary search tree is formed when you insert the following values in the given order – consider there alphabetical position for comparison. a. W, T, N, J, E, B, A b. W, T, N, A, B, E, J c. A, B, W, J, N, T, E d. B, T, E, A, N, W, J Alphabetical positions: A-1, B-2, E-5, J-10, N-14,T-20,W-23
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the...
Lab 5: Binary Search Tree Implement operations for a Binary Search Tree class starting from the template provided under the PolyLearn assignment, using the class TreeNode that is also provided. You may (should) implement helper methods that make your code easier to write, read, and understand. You will also need to write test cases of your own as you develop the methods. You may use iterative and/or recursive functions in your implementation. The following starter files are available . •...
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
Let T be a binary tree with n positions that is realized with an array representation...
Let T be a binary tree with n positions that is realized with an array representation A, and let f() be the level numbering function of the positions of T, as given in Section 8.3.2. Give pseudocode descriptions of each of the methods root, parent, left, right, isExternal, and isRoot.
A binary search tree can be built with a traditional insertion method given a list of...
A binary search tree can be built with a traditional insertion method given a list of integers. Binary search trees (BSTs) are binary trees where the data is ordered such that nodes in the subtree to the left of a given node are smaller than or equal to the node, and the right subtree will contain nodes with values greater than the given node. With a built binary search tree, one can traverse the tree to print each node’s data...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT