The world’s Largest Sharp Brain Virtual Experts Marketplace Just a click Away
Levels Tought:
Elementary,Middle School,High School,College,University,PHD
| Teaching Since: | May 2017 |
| Last Sign in: | 399 Weeks Ago |
| Questions Answered: | 66690 |
| Tutorials Posted: | 66688 |
MCS,PHD
Argosy University/ Phoniex University/
Nov-2005 - Oct-2011
Professor
Phoniex University
Oct-2001 - Nov-2016
#include
#include
#include "bintree.h"
Â
using namespace std;
Â
struct bnode {
int data;
bnode *left;
bnode *right;
};
Â
BinTree NewBinTree()
{
srand(time(NULL));
return 0;
}
Â
void InsertRandom(BinTree& root, int val)
{
if (root == 0) {
root = new bnode;
root->data = val;
root->left = root->right = 0;
return;
}
Â
int r = rand() % 2;
Â
if (r == 0)
InsertRandom(root->left, val);
else
InsertRandom(root->right, val);
}
Â
void PrintNested(BinTree& root)
{
if (root == 0) {
cout
}
else {
cout << "( " << root->data
PrintNested(root->left); cout
PrintNested(root->right); cout
}
}
Â
void InOrder(BinTree& root)
{
if (root == 0)
return;
Â
InOrder(root->left);
cout << root->data
InOrder(root->right);
}
Â
BinTree MirrorImage(BinTree t)
{
// FOR YOU TO DO: Implement the following recursive algorithm to create a mirror image of a binary tree:
// If t is empty, return empty tree (0)
// Else t = (root, L, R) where L = left subtree, R = right subtree.
// Return tree t2 = (root, MirrorImage(R), MirrorImage(L))
Â
BinTree t2; // mirror image
Â
// Your code here.
Â
Â
Â
Â
Â
Â
Â
return t2;
}
Â
int NumTrees[100] = {1,1}; //For memoization.
Â
int NumBinTrees(int n)
{
// Implement the recursion:
// NumBinTrees(0) = 1
// NumBinTrees(n) = Sum(k=0 to n-1) B(k)*B(n-1-k) for n > 0;
// Use memoization.
Â
// Your code here:
Â
Â
Â
Â
Â
Â
Â
}#include <iostream>
Â
#include <cstdlib>
#include "bintree.h"
Â
using namespace std;
Â
struct bnode {
   int data;
   bnode *left;
   bnode *right;
};
Â
BinTree NewBinTree()
{
   srand(time(NULL));
   return 0;
}
Â
void InsertRandom(BinTree& root, int val)
{
   if (root == 0) {
       root = new bnode;
       root->data = val;
       root->left = root->right = 0;
       return;
   }
Â
   int r = rand() % 2;
Â
   if (r == 0)
       InsertRandom(root->left, val);
   else
       InsertRandom(root->right, val);
}
Â
void PrintNested(BinTree& root)
{
   if (root == 0) {
       cout << "[ ]";
   }
   else {
       cout << "( " << root->data << ", ";
       PrintNested(root->left); cout << ", ";
       PrintNested(root->right); cout << " )";
   }
}
Â
void InOrder(BinTree& root)
{
   if (root == 0)
       return;
Â
   InOrder(root->left);
   cout << root->data << " ";
   InOrder(root->right);
}
Â
BinTree MirrorImage(BinTree t)
{
   // FOR YOU TO DO: Implement the following recursive algorithm to create a mirror image of a binary          tree:
   //     If t is empty, return empty tree (0)
   //     Else t = (root, L, R) where L = left subtree, R = right subtree.
   //     Return tree t2 = (root, MirrorImage(R), MirrorImage(L))
Â
   BinTree t2; // mirror image
Â
   // Your code here.
Â
Â
Â
Â
Â
Â
Â
   return t2;
 }
Â
int NumTrees[100] = {1,1}; // For memoization.
Â
int NumBinTrees(int n)
{
   // Implement the recursion:
   //      NumBinTrees(0) = 1
   //      NumBinTrees(n) = Sum(k=0 to n-1) B(k)*B(n-1-k) for n > 0;
   // Use memoization.
Â
   // Your code here:
Â
Â
Â
Â
Â
Â
Â
}
Hel-----------lo -----------Sir-----------/Ma-----------dam----------- Â----------- Th-----------ank----------- Yo-----------u f-----------or -----------usi-----------ng -----------our----------- we-----------bsi-----------te -----------and----------- ac-----------qui-----------sit-----------ion----------- of----------- my----------- po-----------ste-----------d s-----------olu-----------tio-----------n. -----------Ple-----------ase----------- pi-----------ng -----------me -----------on -----------cha-----------t I----------- am----------- on-----------lin-----------e o-----------r i-----------nbo-----------x m-----------e a----------- me-----------ssa-----------ge -----------I w-----------ill-----------