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)?
c++ How do I get numbers and words before I press the enter? For example, if...
c++ How do I get numbers and words before I press the enter? For example, if i type cat 12 Rabbit 27 and then click enter, i want to stop receiving input.
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 α...
"Please can I get a feedback on this discussion post below" Can I get it in...
"Please can I get a feedback on this discussion post below" Can I get it in 2 hours please .thanks Tesla is a company recently in the news for a conflict of interest between the SEC and the CEO Elon Musk due to an interest to bring the company private by the CEO who shared on Twitter his short term plans. Production issues, a number of layoffs and the increasing demand for the electric vehicles were expressed by the CEO...
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)    }
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT