Question
1. Write nodeCount, a recursive function that counts the number of nodes in a tree rooted at node. This is a general tree, where a
1. Write nodeCount, a recursive function that counts the number of nodes in a tree rooted at node. This is a general tree, where a node can have a variable number of children. Use the following Node structure.
struct Node {
int val; vector children;
};
int nodeCount(Node *node) { }
2. Write a one-line function that returns the number of edges in a tree, using the function you defined above.
int edgeCount(Node *node) { }
3. Write leafCount, a function that returns the number of leaves in the tree rooted at the node (a Node is a leaf if all of its children are NULL). int leafCount(Node *node) {
}
4. (a) Write countFront, which is a function that, given a queue, returns the number of times the front value (the value returned when front() is called) appears in the queue. You may not create any auxiliary stack or queue. When the function returns, the queue must look the same way as it did when the function was called. If the queue is empty, return 0.
int countFront(queue& q) {
}
(b) Write countTop, similar to countFront but works with a stack. This time, you are allowed to use an auxiliary stack or queue (but not both). int countTop(stack& s) {
}
here are a few things you can do with stacks and queues:
stack s; queue q; s.push(A); q.push(A); char c = s.top(); char c = q.front(); s.pop(); q.pop(); if (s.empty()) if (q.empty()) cout << its empty << endl; cout << its empty << endl; cout << s.size() << endl; cout << q.size() << endl;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started