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

The algorithm for traversing a binary search tree and print elements between k1 and k2 is as below
print_range (TreeRoot r, key k1, key k2):
   if (root is null) return
   if (k2 < k1) return
  
   if (k1 < r.value): // recurse into left subtree
       print_range (r.left, k1, k2)
      
   // Finished recursing left subtree and printing any elements required, check this root node itself  
   if (r.value between k1 and k2):
       print r.value
      
   if (k2 > r.value):   // recurse into right subtree
       print_range (r.right, k1, k2)
      
   return
  
Since the algorithm traverses each node to be printed once (only), if K is the number of elements to be printed, the time taken is O(K). The tree depth is also required to be traversed and the time for that is O(n) where n is the total number of elements. Time for this algorithm is therefore O(K + log n)
  


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
C++ Instantiate a binary search tree object and create such tree using elements of the sequence...
C++ Instantiate a binary search tree object and create such tree using elements of the sequence 8,3,10, 1,6,9, 14, 4,7, 13 with 8 as root of the tree. Find maximum and minimum elements of the tree, successor(10) and predecessor(13), print the inorder, postorder and preorder traversal of the tree.
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...
Given an array of foods, create a binary search tree. Then, make a copy of that...
Given an array of foods, create a binary search tree. Then, make a copy of that BST and balance it. Language is C++. Vectors are not allowed. The balance function definition: void balance(BST treeObj); and then when writing the function: void BST::balance(BST treeObj) where BST is a class. The function will be called in main like: originalTree.balance(balancedTree);
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT