Question

write pseudocode for an iterative implementation of a function for counting the number of nodes in...

write pseudocode for an iterative implementation of a function for counting the number of nodes in a BST using a stack to visit in Depth First Search the nodes of the tree. Pseudocode must be code like and describe every necessary step.

Language: C++

0 0
Add a comment Improve this question Transcribed image text
Answer #1

The pseudo code with proper comments is as below:

#include <bits/stdc++.h>
using namespace std;
  
// There is a pointer to left child and right child in binary tree
struct Node
{
int data;
struct Node* left, *right;
};
  
// We can get the count of full nodes
unsigned int getwholeCount(struct Node* node)
{
// when the tree is empty
if (!node)
return 0;
queue<Node *> q;
  
//starting from the root doing level order traversal
int count = 0; // Initializing the count of full nodes
q.push(node);
while (!q.empty())
{
struct Node *temp = q.front();
q.pop();
  
if (temp->left && temp->right)
count++;
  
if (temp->left != NULL)
q.push(temp->left);
if (temp->right != NULL)
q.push(temp->right);
}
return count;
}
  
//Function helping to allocate a new node with data
struct Node* newNode(int data)
{
struct Node* node = new Node;
node->data = data;
node->left = node->right = NULL;
return (node);
}
  
// Driver program
int main(void)
{
/* 3
/ \
6 4
\ \
7 10
/ \ /
2 12 4
Creating the above Binary Tree
*/
  
struct Node *root = newNode(3);
root->left = newNode(6);
root->right = newNode(4);
root->left->right = newNode(7);
root->left->right->left = newNode(2);
root->left->right->right = newNode(12);
root->right->right = newNode(10);
root->right->right->left = newNode(4);
  
cout << getwholeCount(root);
  
return 0;
}

Add a comment
Know the answer?
Add Answer to:
write pseudocode for an iterative implementation of a function for counting the number of nodes in...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a Java function named smallcount that, given the pointer to the root of a Binary...

    Write a Java function named smallcount that, given the pointer to the root of a Binary Search Tree (BST) and a key K, returns the number of nodes having key values less than or equal to K. Function smallcount should visit as few nodes in the BST as possible.

  • C++ Write a function, singleParent, that returns the number of nodes in a binary tree that...

    C++ Write a function, singleParent, that returns the number of nodes in a binary tree that have only one child. Add this function to the class binaryTreeType and create a program to test this function. (Note: First create a binary search tree.)

  • 1. Search: In the state space shown, give the order in which the nodes are visited...

    1. Search: In the state space shown, give the order in which the nodes are visited (STOP WHEN THE FIRST GOAL IS REACHED) Goals are 5 and 8 der in which the nodes are visited (NOT the queue): no need to redraw the tree multiple times 1 5 a. Using Depth-First Search b. Using Breadth-First Search c. Using Iterative Deepening

  • 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Yo...

    please explain each line of code! ( in python ) 1. Write a recursive function that returns the sum of all even integers in a LinkedBinaryTree. Your function should take one parameter, root node. You may assume that the tree only contains integers. You may not call any methods from the LinkedBinaryTree class. Specifically, you should traverse the tree in your function def binary tree even sum (root): Returns the sum of al1 even integers in the binary tree 2....

  • C++ implementation Return the number of nodes in a binary search tree that are greater than...

    C++ implementation Return the number of nodes in a binary search tree that are greater than the amount that the user enters.

  • Write a C function for each of the following, must be between 10 - 20 lines,...

    Write a C function for each of the following, must be between 10 - 20 lines, include all assertions: Binary Search Tree Insert Binary Search Tree Remove Binary Search Tree Traversal (Iterative version with a given stack) Binary Heap Insert Binary Heap Remove

  • Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write...

    Please Write Pseudocode or Python code! Thanks! P1. b) is the following: W1. (6 marks) Write the pseudocode for removing and returning only the second element from the Stack. The top element of the Stack will remain the top element after this operation. You may assume that the Stack contains at least two items before this operation. (a) Write the algorithm as a provider implementing a Stack using a contiguous memory implementation. You can refer to the implementation given in...

  • Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores...

    Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the largest absolute value in the tree. The language is C++ Want the height #4 Coding [6 points] Consider the partial implementation of a Binary Search Tree (BST) class. For simplicity, each Node stores only the key. Add a public member function to class BST that returns the height of the...

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • Write an (efficient) pseudocode for the implementation of each of the following function prototypes (proper C...

    Write an (efficient) pseudocode for the implementation of each of the following function prototypes (proper C code will also be accepted) - void print(Q) – prints the entire queue Q from front to back. - int enqueue(Q,x) – enqueues the element x into queue Q (if unsuccessful, return -1) - int* dequeue(Q) – dequeues the next integer from front of the queue (if unsuccessful, return null) - int isEmpty(Q) – returns 1 (true) or 0 (false) depending on emptiness of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT