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...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write...
In java, Finding the maximum value in a BST (Binary Search Tree). Students need to write the code.
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,...
Develop a Java application to implement a binary tree data structure. A tree data structure starts...
Develop a Java application to implement a binary tree data structure. A tree data structure starts from the top node, called the root. Each node in the tree has a set of children, which are also nodes of the tree and can have children of their own, and so on. This keeps on going till we get to the bottom of the tree, to the “leaf” nodes. Each node in the tree, except for the root, has a parent. A...
​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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT