Question

In: Computer Science

I have a sequence of natural numbers. How can I get the height of the resulting...

I have a sequence of natural numbers. How can I get the height of the resulting tree/ c++(send code)

 
 

Solutions

Expert Solution

Hello, Student

You can I get the height of the resulting tree using this c++ program.

Please do not forget to hit that like or thumbs-up button.

  

TreeHeight.cpp

#include<iostream>
#include <stdlib.h>

using namespace std;

typedef struct Node Node;


struct Node {
    int number;
    // left and right children
    Node* left, *right;
};
Node* make_tree(int data) {
    Node* root = (Node*) malloc (sizeof(Node));
    root->left = root->right = NULL;
    root->number = data;
    return root;
}

Node* make_node(int data) {
    // Creates a new node
    Node* node = (Node*) malloc (sizeof(Node));
    node->number = data;
    node->left = node->right = NULL;
    return node;
}

void release_tree(Node* root) {
    Node* temp = root;
    if (!temp)
        return;
    release_tree(temp->left);
    release_tree(temp->right);
    if (!temp->left && !temp->right) {
        free(temp);
        return;
    }
}

int get_height(Node* root) {
    //found the height of whole tree
    if (!root)
        return 0;
    else {
        int left_height = get_height(root->left);
        int right_height = get_height(root->right);
        if (left_height >= right_height)
            return left_height + 1;
        else
            return right_height + 1;
    }
}

int main() {

    // root node
    Node* root = make_tree(50);

    // Insert nodes or natural numbers on tree
    root->left = make_node(60);
    root->right = make_node(70);
    root->left->left = make_node(80);
    root->left->right = make_node(90);

    // calculating Height of the tree
    int height = get_height(root);
    cout<<"Height of the Resulting tree is:"<<height<<endl;

    // Making tree free from root
    release_tree(root);
    return 0;
}

Output :-  Height of the Resulting tree is: 3

Output Screenshot:-

Note :- You can chage node values or your sequence of natural numbers. to get desired height of tree using make_node function in Int main().

Again, Please like my work, it really motivates me>3 (I hope you will do that?)


Related Solutions

Between 1 and 31, m numbers have been inserted in such a way that the resulting sequence is an A.P
Between 1 and 31, m numbers have been inserted in such a way that the resulting sequence is an A.P. and the ratio of 7th and (m – 1)th numbers is 5: 9. Find the value of m.
In Excel: How do I turn a series of dates into a sequence of numbers (example...
In Excel: How do I turn a series of dates into a sequence of numbers (example 1,2, 3)?
I have extra columns with data showing up in my pivot table. How can I get...
I have extra columns with data showing up in my pivot table. How can I get rid of the extra columns?
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics...
The Fibonacci sequence is an infinite sequence of numbers that have important consequences for theoretical mathematics and applications to arrangement of flower petals, population growth of rabbits, and genetics. For each natural number n ≥ 1, the nth Fibonacci number fn is defined inductively by f1 = 1, f2 = 2, and fn+2 = fn+1 + fn (a) Compute the first 8 Fibonacci numbers f1, · · · , f8. (b) Show that for all natural numbers n, if α...
How do I assign the resulting numbers from row and column in this function: def PlacePlayer(board,player):...
How do I assign the resulting numbers from row and column in this function: def PlacePlayer(board,player): row = DieRoller(1,6) col = DieRoller(1,6) board[row][col] = 'player' return board,player To the player['row'] and player['col'] in this function in place of the 0s: def GenPlayer(level): player = {} player['name'] = GenName() player['history'] = GenHistory() #history player['attack'] = DieRoller(3,6) + DieRoller(level,4) player['defense'] = DieRoller(3,6) + DieRoller(level,4) player['health'] = DieRoller(5,6) + DieRoller(level,4) player['row'] = 0 player['col'] = 0
I have two Biophysical chem questions that I have no idea how to get the answer....
I have two Biophysical chem questions that I have no idea how to get the answer. Please help and show work. Thank you. 1. The unit millimeters of mercury (mmHg) has been replaced by the unit torr: 1mmHg is defined as the pressure at the base of a column of mercury exactly 1 mm high when its density is 13.5951 g/cm3 and the acceleration of free fall is 9.806 m/s2. What is the relation between the two units? The answer...
I know the what the answers are but I don't know how to get them. Can...
I know the what the answers are but I don't know how to get them. Can you please explain the process? Thank you. Part VII. Discontinued Operations and Earnings per Share (11 points) Todd Corporation had pre-tax income for 2017 of $2,500,000. On December 31, 2017, Boyd disposed of a component of its business that represented a strategic shift in operation. That component had a Loss on Discontinued Operations of $450,000 (pre-tax). Boyd received $1,000,000 net cash proceeds from the...
Hello I have these questions, Can I please get some assistance, I need not so long...
Hello I have these questions, Can I please get some assistance, I need not so long or short answers please How to decide what job to take. Is that a marginal decision? What is an explicit cost? What is an implicit cost? How is accounting profit calculated? How is economic profit calculated?
Consider the sequence: x0=1/6 and xn+1 = 2xn- 3xn2 | for all natural numbers n. Show:...
Consider the sequence: x0=1/6 and xn+1 = 2xn- 3xn2 | for all natural numbers n. Show: a) xn< 1/3 for all n. b) xn>0 for all n. Hint. Use induction. c) show xn isincreasing. d) calculate the limit.
Can I get a Step by Step on how to do this problem Test the claim...
Can I get a Step by Step on how to do this problem Test the claim that the proportion of people who own cats is larger than 80% at the 0.005 significance level. The null and alternative hypothesis would be: H0:p≤0.8H0:p≤0.8 H1:p>0.8H1:p>0.8 H0:μ=0.8H0:μ=0.8 H1:μ≠0.8H1:μ≠0.8 H0:p≥0.8H0:p≥0.8 H1:p<0.8H1:p<0.8 H0:p=0.8H0:p=0.8 H1:p≠0.8H1:p≠0.8 H0:μ≥0.8H0:μ≥0.8 H1:μ<0.8H1:μ<0.8 H0:μ≤0.8H0:μ≤0.8 H1:μ>0.8H1:μ>0.8 The test is: right-tailed left-tailed two-tailed Based on a sample of 700 people, 82% owned cats The test statistic is:  (to 2 decimals) The p-value is:  (to 2 decimals) Based...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT