Question

In: Computer Science

In C++ language. void printTreeIO(Tnode *n)(3): recursive function that prints out the data in the tree...

In C++ language.

void printTreeIO(Tnode *n)(3): recursive function that prints out the data in the tree in order

Solutions

Expert Solution

source code:

#include <iostream>
using namespace std;

/* A binary tree node has data, pointer to left child
and a pointer to right child */
struct Node
{
   int data;
   struct Node* left, *right;
   Node(int data)
   {
       this->data = data;
       left = right = NULL;
   }
};


/* Given a binary tree, print its nodes in inorder*/
void printInorder(struct Node* node)
{
   if (node == NULL)
       return;

   /* first recur on left child */
   printInorder(node->left);

   /* then print the data of node */
   cout << node->data << " ";

   /* now recur on right child */
   printInorder(node->right);
}

/* Driver program to test above functions*/
int main()
{
   struct Node *root = new Node(1);
   root->left           = new Node(2);
   root->right       = new Node(3);
   root->left->left   = new Node(4);
   root->left->right = new Node(5);

   cout << "\nInorder traversal of binary tree is \n";
   printInorder(root);

   return 0;
}


Related Solutions

In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {  ...
In c++ Rewrite the following recursive method into an iterative function.  void mystery (int n) {    cout<<"? ";    if (n > 0)      mystery (n - 1); }
IN PYTHON 3 LANGUAGE Define a recursive function named immutify; it is passed any data structure...
IN PYTHON 3 LANGUAGE Define a recursive function named immutify; it is passed any data structure that contains int, str, tuple, list, set, frozenset, and dict values (including nested versions of any of these data structures as an argument). It returns an immutable equivalent data structure (one that could be used for values in a set or keys in a dict). The types int, str, and frozenset are already immutable. Convert a set to a frozenset; convert all the values...
In c++ language, write a void function GetYear that prompts for and inputs the year the...
In c++ language, write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater than...
C++ AVL tree My AVL tree function void inorder(AVLNode* t) { if (t == NULL) return;...
C++ AVL tree My AVL tree function void inorder(AVLNode* t) { if (t == NULL) return; inorder(t->left); cout << t->data.cancer_rate << " "; inorder(t->right); } void inorder() { inorder(root); } Will Print my cancer city rate Inorder example) 3.1 5.8 19.8 My question is how can I add a decreasing index to this function example) 3) 3.1 2)5.8 1)19.8
(In c++ language) 1A) Write a void function GetYear that prompts for and inputs the year...
(In c++ language) 1A) Write a void function GetYear that prompts for and inputs the year the operator was born (type int) from standard input. The function returns the user’s birth year through the parameter list (use pass by reference) unless the user enters an invalid year, in which case a BadYear exception is thrown. To test for a bad year, think about the range of acceptable years. It must be 4 digits (i.e. 1982) and it cannot be greater...
For a Binary Search Tree in C++ void setHeight(TNode *n)(10): This method sets the heights of...
For a Binary Search Tree in C++ void setHeight(TNode *n)(10): This method sets the heights of the nodes in a tree. Once a node is inserted, only the node’s ancestors can have their height changed. Thus you should set the height of the node being inserted (to 1) and then adjust the heights of the node’s parent, grandparent, etc. up until either the height of the node doesn’t change or you hit the root.
Use a recursive tree method to compute a tight asymptotic upper bound for recurrence function T(n)=...
Use a recursive tree method to compute a tight asymptotic upper bound for recurrence function T(n)= 2T(n/5)+3n . then use substitution method to verify your answer.
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as...
Write a C function, for example void sumOfPrime(int n), that takes a positive int n as a parameter, put all prime numbers less than n into an array, and print out the array and the sum of the numbers in that array. You can use the isPrime function above to save time.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
C++ Write a recursive function that computes and returns the product of the first n >=1...
C++ Write a recursive function that computes and returns the product of the first n >=1 real numbers in an array.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT