Question

In: Computer Science

Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports...

Show how to augment the ordinary Binary Search Tree (BST) data structure so that it supports an efficient procedure which, on input (x, k) where x is the root of a BST and k an integer, output the k-th smallest number store in the BST. Let n denote the total number of elements stored in the BST, what is the running time of your efficient procedure? How does your modification of the BST data structure affect the performance of other BST operations we discussed in class?

Solutions

Expert Solution

In the following BST, if k = 3, then the output should be 10, and if k = 5, then the output should be 14.
Using Inorder Traversal

left subtree,root rigt subtree

Node

struct Node {

    int data;

    Node *left, *right;

    Node(int x)

    {

        data = x;

        left = right = NULL;

    }

};

Node* insert(Node* root, int x)

{

    if (root == NULL)

        return new Node(x);

    if (x < root->data)

        root->left = insert(root->left, x);

    else if (x > root->data)

        root->right = insert(root->right, x);

    return root;

}

Node* kthSmallest(Node* root, int k)

{

    if (root == NULL)

        return NULL;

    int count = root->lCount + 1;

    if (count == k)

        return root;

    if (count > k)

        return kthSmallest(root->left, k);

    return kthSmallest(root->right, k - count);

}

int main()

{

    Node* root = NULL;

    int keys[] = { 20, 8, 22, 4, 12, 10, 14 };

    for (int x : keys)

        root = insert(root, x);

    int k = 4;

    Node* res = kthSmallest(root, k);

    if (res == NULL)

        cout << "There are less than k nodes in the BST";

    else

        cout << "K-th Smallest Element is " << res->data;

    return 0;

}

Ans:

K-th Smallest Element is:12


Related Solutions

• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a...
• This assignment calls for generating a Binary Search Tree (BST) and scanning it in a preorder and a Breadth First Search (BFS) way. • The program gets a parameter ? from the command line and stores ? randomly generated integers in a dynamic array. To generate a random integer between 1 and Max-Rand (a system constant): ▪ Use srand() to seed the rand() function. Then, call rand() again and again (? times) to generate ? random integers. Next, printout...
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
What is a binary search tree or BST? Discuss its characteristics. What are other types of...
What is a binary search tree or BST? Discuss its characteristics. What are other types of binary trees as well?
I was trying to implement a simple binary search tree using this given class of bst...
I was trying to implement a simple binary search tree using this given class of bst in c++ public: BST(); ~BST(); void insertKey(int newKey); bool hasKey(int searchKey); std::vector<int> inOrder(); int getHeight(); however; i am still required to use another class for the nodes as a pointer and i need to manage memory leak. in main we should ask for the numbers we need to insert in the binary search tree and also let the user end it with a letter...
Develop a BST data type that supports: insert, search, remove and then Create a visualization for...
Develop a BST data type that supports: insert, search, remove and then Create a visualization for the BST data type you developed
/** * This class implements a basic Binary Search Tree that supports insert and get operations....
/** * This class implements a basic Binary Search Tree that supports insert and get operations. In * addition, the implementation supports traversing the tree in pre-, post-, and in-order. Each node * of the tree stores a key, value pair. * * @param <K> The key type for this tree. Instances of this type are used to look up key, value * pairs in the tree. * @param <V> The value type for this tree. An instance of this...
Code using C++ A binary search tree is a data structure designed for efficient item insertion,...
Code using C++ A binary search tree is a data structure designed for efficient item insertion, deletion, and retrieval. These 3 operations share an average run time complexity of O(log(n)). Such time complexities are guaranteed whenever you are working with a balanced binary search tree. However, if you have a tree that begins leaning heavily to either its left or right side, then you can expect the performances of the insertion, deletion, and retrieval operations to degrade. As an example,...
​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 . •...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT