
Explanation of Solution
C++ program for preorder iterator:
- The Example 6.69 explains how C++ use iterator () function different from Java.
- The program explains the use of pointers, references, inner classes and operator overloading.
- The tree node take the input of integer datatype.
//Defining the header files
#include <stack>
#include <iostream.h>
//Using namespace std
using namespace std;
//Defining the class tree_node
class tree_node
{
//Defining the left node variable
tree_node* left;
//Defining the right node variable
tree_node* right;
//Defining the public variable val
public:int val;
//Defining the constructor
tree_node(tree_node *l, tree_node *r, int v)
{
//assigning the value for left and right node
left = l; right = r; val = v;
}
//Defining another constructor with one parameter
tree_node(int v)
{
//Assigning value to tree node and val
left = 0; right = 0; val = v;
}
//Defining the inner class
class iterator
{
//Defining the variable with reference
stack<tree_node*> *s;
tree_node *cur;
//Defining the constructor for iterator as public
public:iterator(tree_node& n)
{
//Defining a stack to store the tree
s = new stack<tree_node*>();
//Assigning current value to n
cur = &n;
//Checking current position
if (cur)
{
//If current position in right side then push the element to the stack
if (cur->right) s->push(cur->right);
//If current position in left side then push the element to the stack
if (cur->left) s->push(cur->left);
}
}
//Destructor for iterator
~iterator()
{
//Deleting the stack
delete s;
}
//Checking current position with pointer
bool operator!=(void *p) const
{
//Return statement if current!=0
return (cur != 0);
}
//Use of reference
iterator& operator++()
{
//if condition for checking the stack is empty or not
if (s->empty())
{
//if stack is empty then it assign the current position to 0
cur = 0;
}
//If stack is not empty
else
{
//The current position value points to top of the stack
cur = s->top();
//use of pop() method
s->pop();
if (cur)
{
//Pushing the right element to stack
if (cur->right) s->push(cur->right);
//Pushing the right element to stack
if (cur->left) s->push(cur->left);
}
}
//returning the object
return *this;
}
//operator method using reference
int& operator*() const
{
//it return the current position as val
return cur->val;
}
//operator method pointers
int* operator->() const
{
//it return the current position as val
return &(cur->val);
}
};
//Defining the begin method of iterator which uses reference method
iterator& begin()
{
//Creating a new pointer i
iterator *i = new iterator(*this);
//Returing the pointer i
return *i;
}
//End method
void *end()
{
//which return 0
return 0;
}
};
Explanation:
- The program creates a class “tree_node” which is used to get the left and right nodes of the tree.
- The “tree_node” class used constructors to create the tree.
- Then the program creates an inner class with the name of “iterator”, which incorporates the reference, operator overloading, and pointers method.
- It creates a stack and pushing the elements to the stack based on current position of the variable.
Want to see more full solutions like this?
Chapter 6 Solutions
Programming Language Pragmatics, Fourth Edition
- using r language for integration theta = integral 0 to infinity (x^4)*e^(-x^2)/2 dx (1) use the density function of standard normal distribution N(0,1) f(x) = 1/sqrt(2pi) * e^(-x^2)/2 -infinity <x<infinity as importance function and obtain an estimate theta 1 for theta set m=100 for the estimate whatt is the estimate theta 1? (2)use the density function of gamma (r=5 λ=1/2)distribution f(x)=λ^r/Γ(r) x^(r-1)e^(-λx) x>=0 as importance function and obtain an estimate theta 2 for theta set m=1000 fir the estimate what is the estimate theta2? (3) use simulation (repeat 1000 times) to estimate the variance of the estimates theta1 and theta 2 which one has smaller variance?arrow_forwardusing r language A continuous random variable X has density function f(x)=1/56(3x^2+4x^3+5x^4).0<=x<=2 (1) secify the density g of the random variable Y you find for the acceptance rejection method. (2) what is the value of c you choose to use for the acceptance rejection method (3) use the acceptance rejection method to generate a random sample of size 1000 from the distribution of X .graph the density histogram of the sample and compare it with the density function f(x)arrow_forwardusing r language a continuous random variable X has density function f(x)=1/4x^3e^-(pi/2)^4,x>=0 derive the probability inverse transformation F^(-1)x where F(x) is the cdf of the random variable Xarrow_forward
- using r language in an accelerated failure test, components are operated under extreme conditions so that a substantial number will fail in a rather short time. in such a test involving two types of microships 600 chips manufactured by an existing process were tested and 125 of them failed then 800 chips manufactured by a new process were tested and 130 of them failed what is the 90%confidence interval for the difference between the proportions of failure for chips manufactured by two processes? using r languagearrow_forwardI want a picture of the tools and the pictures used Cisco Packet Tracer Smart Home Automation:o Connect a temperature sensor and a fan to a home gateway.o Configure the home gateway so that the fan is activated when the temperature exceedsa set threshold (e.g., 30°C).2. WiFi Network Configuration:o Set up a wireless LAN with a unique SSID.o Enable WPA2 encryption to secure the WiFi network.o Implement MAC address filtering to allow only specific clients to connect.3. WLC Configuration:o Deploy at least two wireless access points connected to a Wireless LAN Controller(WLC).o Configure the WLC to manage the APs, broadcast the configured SSID, and applyconsistent security settings across all APs.arrow_forwardA. What will be printed executing the code above?B. What is the simplest way to set a variable of the class Full_Date to January 26 2020?C. Are there any empty constructors in this class Full_Date?a. If there is(are) in which code line(s)?b. If there is not, how would an empty constructor be? (create the code lines for it)D. Can the command std::cout << d1.m << std::endl; be included after line 28 withoutcausing an error?a. If it can, what will be printed?b. If it cannot, how could this command be fixed?arrow_forward
- Cisco Packet Tracer Smart Home Automation:o Connect a temperature sensor and a fan to a home gateway.o Configure the home gateway so that the fan is activated when the temperature exceedsa set threshold (e.g., 30°C).2. WiFi Network Configuration:o Set up a wireless LAN with a unique SSID.o Enable WPA2 encryption to secure the WiFi network.o Implement MAC address filtering to allow only specific clients to connect.3. WLC Configuration:o Deploy at least two wireless access points connected to a Wireless LAN Controller(WLC).o Configure the WLC to manage the APs, broadcast the configured SSID, and applyconsistent security settings across all APs.arrow_forwardTransform the TM below that accepts words over the alphabet Σ= {a, b} with an even number of a's and b's in order that the output tape head is positioned over the first letter of the input, if the word is accepted, and all letters a should be replaced by the letter x. For example, for the input aabbaa the tape and head at the end should be: [x]xbbxx z/z,R b/b,R F ① a/a,R b/b,R a/a, R a/a,R b/b.R K a/a,R L b/b,Rarrow_forwardGiven the C++ code below, create a TM that performs the same operation, i.e., given an input over the alphabet Σ= {a, b} it prints the number of letters b in binary. 1 #include 2 #include 3 4- int main() { std::cout > str; for (char c : str) { if (c == 'b') count++; 5 std::string str; 6 int count = 0; 7 char buffer [1000]; 8 9 10 11- 12 13 14 } 15 16- 17 18 19 } 20 21 22} std::string binary while (count > 0) { binary = std::to_string(count % 2) + binary; count /= 2; std::cout << binary << std::endl; return 0;arrow_forward
- Considering the CFG described below, answer the following questions. Σ = {a, b} • NT = {S} Productions: P1 S⇒aSa P2 P3 SbSb S⇒ a P4 S⇒ b A. List one sequence of productions that can accept the word abaaaba; B. Give three 5-letter words that can be accepted by this CFG; C. Create a Pushdown automaton capable of accepting the language accepted by this CFG.arrow_forwardGiven the FSA below, answer the following questions. b 1 3 a a b b с 2 A. Write a RegEx that is equivalent to this FSA as it is; B. Write a RegEx that is equivalent to this FSA removing the states and edges corresponding to the letter c. Note: To both items feel free to use any method you want, including analyzing which words are accepted by the FSA, to generate your RegEx.arrow_forward3) Finite State Automata Given the FSA below, answer the following questions. a b a b 0 1 2 b b 3 A. Give three 4-letter words that can be accepted by this FSA; B. Give three 4-letter words that cannot be accepted by this FSA; C. How could you describe the words accepted by this FSA? D. Is this FSA deterministic or non-deterministic?arrow_forward
- Database System ConceptsComputer ScienceISBN:9780078022159Author:Abraham Silberschatz Professor, Henry F. Korth, S. SudarshanPublisher:McGraw-Hill EducationStarting Out with Python (4th Edition)Computer ScienceISBN:9780134444321Author:Tony GaddisPublisher:PEARSONDigital Fundamentals (11th Edition)Computer ScienceISBN:9780132737968Author:Thomas L. FloydPublisher:PEARSON
- C How to Program (8th Edition)Computer ScienceISBN:9780133976892Author:Paul J. Deitel, Harvey DeitelPublisher:PEARSONDatabase Systems: Design, Implementation, & Manag...Computer ScienceISBN:9781337627900Author:Carlos Coronel, Steven MorrisPublisher:Cengage LearningProgrammable Logic ControllersComputer ScienceISBN:9780073373843Author:Frank D. PetruzellaPublisher:McGraw-Hill Education





