Question

[15 pts.] Consider a version of a binary search tree (BST) for sorted maps with integer...

  1. [15 pts.] Consider a version of a binary search tree (BST) for sorted maps with integer keys that stores additional information as data members of each node w of the tree to account for the smallest and largest integer keys in the subtree rooted at w. Suppose that we can access this information using w.min, for the smallest integer key, and w.max, for the largest. Algorithm 4 provides the pseudocode for the constructor of a node in a binary tree with the additional information.

Algorithm 4 Node(k,v,left,right,parent)

1: w ← new node

2: w.keyk

3: w.valuev

4: w.leftleft

5: w.rightright

6: w.parentparent

7: w.mink

8: w.maxk

9: return w

(One can adapt the find operation to account for this information and potentially stop the search early. Algorithm 5 provides the pseudocode for the new implementation of the find function.)

Algorithm 5 find(T,k)

1: wT.root

2: while w 6= NULL and k 6= w.key do

3:                      if k < w.min or k > w.max then return NULL

4:                 if k < w.key then

5:                        ww.left

6:              else

7:                        ww.right

8: return w

(Note that the data structure must maintain the invariant w.minw.keyw.max. Hence, both the insert and delete operations must be properly adapted.)

(Your task is to complete the pseudocode for the insert function, started for you in the partial pseudocode given in Algorithm 6, by writing the missing statements.)

(NOTE: The missing statements you are asked to write for the insert function in Algorithm 6 only require simple accesses or changes to node w’s data members or appropriate reassignment of the variable w itself.)

Algorithm 6 insert(T,k,v)

1: wT.root 2: while w 6= NULL and k

6= w.key do

3: 4:

if k < w.min then

5: 6:

if k > w.max then

7:

if k < w.key then

8:

if w.left = NULL then

9: 10:

x ← Node(k,v,NULL,NULL,w)

11: 12:

return x

13:

else

14:

if w.right = NULL then

15: 16:

x ← Node(k,v,NULL,NULL,w)

17:

return x

18:

19: if w 6= NULL then

20:

21: return w

Homework Answers

Answer #1

IF YOU HAVE ANY DOUBTS COMMENT BELOW I WILL BE THERE TO HELP YOU

ANSWER:

EXPLANATION:

I AM GIVING YOU THE ANSWERS FOR THE BLANKS

line 4: w.min = k // updating smallest key rooted under w

line 6: w.max = k // updating largest key rooted under w

line 10: w.left = x // updating left node of w as x

line 12: w = w.left // going left subtree

line 16: w.right = x // updating right node of w as x

line 18: w = w.right // going right subtree

line 20: w.value = v // updating value of node incase inserted key is duplicate

RATE THUMBSUP PLEASE

Know the answer?
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
Similar Questions
Consider a binary search tree where each tree node v has a field v.sum which stores...
Consider a binary search tree where each tree node v has a field v.sum which stores the sum of all the keys in the subtree rooted at v. We wish to add an operation SumLE(K) to this binary search tree which returns the sum of all the keys in the tree whose values are less than or equal to K. (a) Describe an algorithm, SumLE(K), which returns the sum of all the keys in the tree whose values are less...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
‏What is the output of the Euler tour in the normal binary search tree if the...
‏What is the output of the Euler tour in the normal binary search tree if the key insert order is 5 , 2 , 8 , 5 , 9 , 5 , 1 , 3 , 4 , 2 , 8 ? All keys equal to the node should be the right subtree of that node. ____________________________________________________________ ‏Construct the binary max - heap for the keys given below. Once all the keys are inserted, perform the remove maximum operation, and...
The one missing piece was inserting into a binary search tree; we'll take care of that...
The one missing piece was inserting into a binary search tree; we'll take care of that today and write the insert function, as well as a height function. Both functions will be implemented in the "bst.h" header file, and tested using a provided main program in "main.cpp". Step one is to implement the insert function --- go ahead and modify "bst.h", adding the necessary code to (1) allocate a new node, and (b) link it into the tree. Once you...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
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...
Consider this binary search tree: 14 / \ 2 16 / \ 1 5 / 4...
Consider this binary search tree: 14 / \ 2 16 / \ 1 5 / 4 Suppose we remove the node with value 2. What will be the new tree? 14 / \ 4 16 / \ 1 5 14 / \ 5 16 / \ 1 4 4 / \ 5 16 / / 1 14 14 / \ Null 16 / \ 1 5 / 4
I have written working code for this problem however it is not producing an output and...
I have written working code for this problem however it is not producing an output and it states is a wrong answer. Please just fix my code below and make it work for the sample test cases and all test cases. You are given a pointer to the root of a binary search tree and values to be inserted into the tree. Insert the values into their appropriate position in the binary search tree and return the root of the...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions...
This is my current code and im having trouble inserting the findMin, findMax, and Search functions in main to output them. #include<iostream> using namespace std; /*Define the class for BST*/ class BST_TREE {    /*Structure of the tree*/    struct tree    {        int value;        tree* left;        tree* right;    };    tree* root;    /*Method to clear the tree*/    tree* Empty(tree* t)    {        if (t == NULL)       ...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted...
Here is a modification of the BST program that includes a recursive find method: BinarySearchTree2C.java (posted below) Implement the following methods using recursion: int depth() // returns the length of the deepest path from root to any leaf int node_count() // returns the number of nodes in the tree void insert(int n) // inserts value n into the tree BinarySearchTree2C clone() // returns a clone (deep copy) of the tree Add code to the main method to test these methods....