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 a recursive Tower of Hanoi program and would like to get the index numbers...
I have a recursive Tower of Hanoi program and would like to get the index numbers to be in correct sequence: 1,2,3,4,5,6,7. How can I do this? Main.java import java.io.*; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args){ /**There is a stack of N disks on the first of three poles (call them A, B and C) and your job is to move the disks from pole A to pole C without ever putting a...
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 α...
I cant not seem to get this right. I have tried and tried can I please...
I cant not seem to get this right. I have tried and tried can I please get help. Thank you! Writing a Modular Program in Java Summary In this lab, you add the input and output statements to a partially completed Java program. When completed, the user should be able to enter a year, a month, and a day to determine if the date is valid. Valid years are those that are greater than 0, valid months include the values...
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 been working on this assignment in Java programming and can not get it to...
I have been working on this assignment in Java programming and can not get it to work. This method attempts to DECODES an ENCODED string without the key.    public static void breakCodeCipher(String plainText){ char input[]plainText.toCharArray(); for(int j=0; j<25; j++){ for(int i=0; i<input.length; i++) if(input[i]>='a' && input[i]<='Z') input[i]=(char)('a'+((input[i]-'a')+25)%26); else if(input[i]>='A'&& input[i]<='Z') input[i]=(char)('A'+ ((input[i]-'A')+25)%26); } System.out.println(plainText)    }
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT