Question

Exercise 3: Multi-way Trees A way to reduce the height of tree and ensure balance is...

Exercise 3: Multi-way Trees

A way to reduce the height of tree and ensure balance is to allow multiple children of nodes. In your class you learned 2-3 trees which allows up to 2 keys in a node, and the number of children is equal to the number of keys + 1. B-trees extend this concept to any arbitrary number of keys (usually number of keys is even and number of children (equal to number of keys+1) is odd).

Assume we want to design a 5-way B-Tree. This will mean that there can be maximum 4 keys in a node, and if the number of keys becomes 5, we can split it into two (the same way we split 2-3 tree when number of keys becomes 3).

Design a 5-way B-tree. Starting with an empty tree, insert the following keys in order.
2, 3, 5, 7, 10, 50, 22, 44, 45, 55, 66, 68, 70, 17, 6, 21, 67

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Exercise 1: Pre-sorting

Consider the problem of finding the distance between the two closest numbers in an array of n numbers. (The distance between two numbers x and y is computed as |x − y|.)

a. Design a presorting-based algorithm and implement it in your preferred language.

b. Solve the above problem using brute-force approach. Implement in your preferred language.

c. Compare the efficiency of the above two algorithms.

Homework Answers

Answer #1

1)  For B tree we just have to insert keys and when children become N we split it into two halves.

End result:

Procees:

2)  

a) Just sort the array using in log(n) time with mergesort or inbuild function.

#include<bits/stdc++.h>
#include<numeric>
using namespace std;
void merge(int *v,int s,int e)
{
    int a[100];
    int mid=(s+e)/2;
    int j=s,k=s,i=mid+1;
    while(j<=mid && i<=e)
    {
        if(v[i]<v[j])
        {
            //ans.push_back(v[i]);
            a[k++]=v[i++];
            //i++;
        }
        else
        {
            a[k++]=v[j++];
            //j++;
        }
    }
    while(i<=e)
    {
        a[k++]=v[i++];
        //i++;
    }
    while(j<=mid)
    {
        a[k++]=v[j++];
        //j++;
    }
    for(int i=s;i<=e;i++)
    {
        v[i]=a[i];
    }
}
void mergesort(int v[],int s,int e)
{
    if(s>=e)return;
    int mid=(s+e)/2;
    mergesort(v,s,mid);
    mergesort(v,mid+1,e);
    merge(v,s,e);
}
int main()
{
        int n;
        cin>>n;
        int v[n];
        for(int i=0;i<n;i++)
        {
                cin>>v[i];        // taking array as input
        }
        mergesort(v,0,n-1); // sorting array
        int mn=INT_MAX; //declaring a variable mn which will be our answer INT_MAX max possible value.
        for(int i=1;i<n;i++)
        {
                mn=min(v[i]-v[i-1],mn); // comparing new value with minimum value till this point.
        }
        cout<<mn<<endl; // answer
        return 0;
}

n log(n) + n = n log(n)

this takes n log(n) time.

b)

In this, we iterate over an array in a nested loop to find the distance

#include<bits/stdc++.h>
#include<numeric>
using namespace std;
int main()
{
        int n;
        cin>>n;
        int v[n];
        for(int i=0;i<n;i++)
        {
                cin>>v[i];        
        }
        int mn=INT_MAX;
        for(int i=0;i<n;i++)
        {
                for(int j=0;j<n;j++)
                {
                        mn=min(mn,abs(v[i]-v[j]));
                }
        }
        cout<<mn<<endl;
        return 0;
}

This is  .

C) algorithm in A takes time and algorithm in B take time.

Please Upvote.

if you face any problem comment below.

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
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...
1) A study of 150 apple trees showed that the average number of apples per tree...
1) A study of 150 apple trees showed that the average number of apples per tree was 2000. The standard deviation of the population is 100. Which of the following is the 80% confidence interval for the mean number of apples for all trees? a) (1986.6, 2013.4) b) (1988.5, 2011.5) c) (1989.5, 2010.5) d) (1993.1, 2006.9) 2) If a population has a standard deviation of 16, what is the minimum number of samples that need to be averaged in order...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
Application Exercise: Researchers gave a family planning survey was given to the head(s) of household in...
Application Exercise: Researchers gave a family planning survey was given to the head(s) of household in a local community. In particular, one question asked the head(s) of households to indicate if they think the community has a great, some, or no need of family planning counseling. In addition, they were asked to indicate how many children are in the household. Researchers want to know if the number of children in the household is impacted by family planning counseling. What can...
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....
Evaluate C(11,4). C(11,4) equals = Six cards are marked with the numbers 1, 2, 3, 4,...
Evaluate C(11,4). C(11,4) equals = Six cards are marked with the numbers 1, 2, 3, 4, 5, and 6 , then shuffled, and three cards are drawn. a. How many different three -card combinations are possible? b. How many three -card hands contain a number less than three? Use a tree diagram for the following. a. Find the number of ways 2 letters can be chosen from the set {U, V, W, X} if order is important and repetition is...
1. If you want to study the effect of hormonal changes in adolescent boys, your population...
1. If you want to study the effect of hormonal changes in adolescent boys, your population would be Group of answer choices All adolescents All the people in the world All males All adolescent males 2. Which of the following is most likely to be measured categorically? Group of answer choices Weight gain in first year college students Deterioration in driving performance under the influence of alcohol Level of authoritarianism in a sample of public accountants Species of dog appearing...
A study of fox rabies in a country gave the following information about different regions and...
A study of fox rabies in a country gave the following information about different regions and the occurrence of rabies in each region. A random sample of n1 = 16 locations in region I gave the following information about the number of cases of fox rabies near that location. x1:    Region I Data 1 8 8 8 6 8 8 1 3 3 3 2 5 1 4 6 A second random sample of n2 = 15 locations in...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following...
1. Vim commands: a. How do you auto indent your program? b. Explain what the following commands do: dd, y3, p, :set cindent (1 pt) VIM exercises These exercises on the computer need to be repeated by each student in the pair. This is to ensure that both students understand how to get around in Linux!!! For this part of the lab, you will create a .vimrc file that will help you develop your C++ programs using VIM. First, we...
Do the following problems. 1. Each of three barrels from a manufacturing line are classified as...
Do the following problems. 1. Each of three barrels from a manufacturing line are classified as either above (a) or below (b) the target weight. Provide the ordered sample space. 2. The heat on each of two soldered parts is measured and labeled as either low (l), medium (m), or high (h). State the number of elements in the ordered sample space. 3. Consider the set of Beatles songs with a primary writer as either Paul McCartney (P) or John...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT