Please help me with these, thank you!

Comment down for any queries
Please give a thumb up
____________________________
Code:
#include <bits/stdc++.h>
using namespace std;
struct BSTNode {
int value;
BSTNode *left, *right;
};
void add(BSTNode* &root, int val) {
BSTNode *newNode = new BSTNode();
newNode->value = val;
newNode->left = NULL;
newNode->right = NULL;
if(root == NULL) {
root = newNode;
return;
}
BSTNode *node = root;
while(true) {
if(val < node->value) {
if(node->left == NULL) {
node->left = newNode;
return;
} else {
node = node->left;
}
} else {
if(node->right == NULL) {
node->right = newNode;
return;
} else {
node = node->right;
}
}
}
return;
}
void preOrder(BSTNode *root) {
stack<BSTNode*> st;
cout << "Preorder traversal: ";
st.push(root);
while(!st.empty()) {
BSTNode* node = st.top();
st.pop();
cout<<node->value<<" ";
if(node->right != NULL)
st.push(node->right);
if(node->left != NULL)
st.push(node->left);
}
cout << endl;
}
void inOrder(BSTNode *root) {
stack<pair<BSTNode*,bool>> st;
cout << "Inorder traversal: ";
st.push({root,false});
while(!st.empty()) {
BSTNode* node = st.top().first;
if(st.top().second == false) {
st.pop();
st.push({node,true});
if(node->left != NULL)
st.push({node->left,false});
} else {
cout << node->value << " ";
st.pop();
if(node->right != NULL)
st.push({node->right,false});
}
}
cout << endl;
}
void postOrder(BSTNode *root) {
stack<pair<BSTNode*,int>> st;
cout << "Postorder traversal: ";
st.push({root,0});
while(!st.empty()) {
BSTNode* node = st.top().first;
if(st.top().second == 0) {
st.pop();
st.push({node,1});
if(node->left != NULL)
st.push({node->left,0});
} else if(st.top().second == 1) {
st.pop();
st.push({node,2});
if(node->right != NULL)
st.push({node->right,0});
} else {
st.pop();
cout << node->value << " ";
}
}
cout << endl;
}
int countLeaves(BSTNode *root) {
stack<BSTNode*> st;
int cnt = 0;
st.push(root);
while(!st.empty()) {
BSTNode* node = st.top();
st.pop();
if(node->left == NULL && node->right == NULL)
cnt++;
else {
if(node->left != NULL)
st.push(node->left);
if(node->right != NULL)
st.push(node->right);
}
}
return cnt;
}
int countNonLeaves(BSTNode *root) {
stack<BSTNode*> st;
int cnt = 0;
st.push(root);
while(!st.empty()) {
BSTNode* node = st.top();
st.pop();
if(node->left != NULL || node->right != NULL) {
cnt++;
if(node->left != NULL)
st.push(node->left);
if(node->right != NULL)
st.push(node->right);
}
}
return cnt;
}
int main() {
BSTNode *root = NULL;
for(int i = 0; i < 10; i++) {
int x;
cin >> x;
add(root,x);
}
preOrder(root);
inOrder(root);
postOrder(root);
cout << "Number of leaves: " << countLeaves(root)
<< endl;
cout << "Number of non-leaves: " <<
countNonLeaves(root) << endl;
}
Output:

____________________________
Comment down for any queries
Please give a thumb up
Please help me with these, thank you! 3. **25.3 (Implement inorder traversal without using recursion) Implement...
JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective: javafx GUI program for BST Animation. BSTAnimation.java, AbstractTree.java and Tree.java. Modify the BSTAnimation.java (1) Add Show Preoreder and Show Postorder button. Insert these two buttons next to the Show Inorder button to display tree in selected order. (2) Currently removing node method is to replace the removed node by the highest node on left-subtree. Modify the method by using lowest node on the right-subtree instead....
C++ EXERCISE (DATA STRUCTURES). I just need a code for some functions that are missing. Please help me figure out. Thanks. C++ BST implementation (using a struct) Enter the code below, and then compile and run the program. After the program runs successfully, add the following functions: postorder() This function is similar to the inorder() and preorder() functions, but demonstrates postorder tree traversal. displayParentsWithTwo() This function is similar to the displayParents WithOne() function, but displays nodes having only two children....
[Python]
Construct Tree Using Inorder and Preorder
Given Preorder and Inorder traversal of a binary tree, create
the binary tree associated with the traversals.You just need to
construct the tree and return the root.
Note: Assume binary tree contains only unique elements.
Input format :
Line 1 : n (Total number of nodes in binary tree)
Line 2 : Pre order traversal
Line 3 : Inorder Traversal
Output Format :
Elements are printed level wise, each level in new line...
Please I need help ASAP Java Programing: Binary Search Tree Fully implement the BST class in Listing 25.4 (on page 961 of the 11th Edition of the text). Design and write a (main) driver program to completely test every method in the BST class to ensure the class meets all its requirements. You should read the Listing 25.5: TestBST.java for an idea of what your program should look like. Listing 25.4 BST.java public class BST> extends AbstractTree { protected TreeNode...
using java to write,show me the output. please write some
common.
You CAN NOT use inbuild functions for Tree ADT operations.
using code below to finsih
public class Main
{
public static void main(String[] args) {
BinaryTree tree = new
BinaryTree();
tree.root = new Node(1);
tree.root.left = new Node(2);
tree.root.right = new Node(3);
tree.root.left.left = new Node(4);
tree.root.left.right = new Node(5);
tree.root.right.left = new Node(6);
tree.root.right.right = new Node(7);
tree.root.left.left.left = new Node(8);
tree.root.left.left .right= new Node(9);...
Here you'll write the inorder traversal function in the header file "bst.h". Notice that the public inorder function calls a private recursive function _ inorder to do the actual traversal. This public-private strategy is the correct way to implement recursive functions, where the public function kicks off the recursion and the private function does the actual work. The public function is written for you, your job is to implement the private _ inorder function. he main program has been written...
You will be given several strings full of different keyboard
characters, some of which are letters of the alphabet.
Write a java program that creates a binary tree that uses
only the alphabetical characters (a-z, A-Z) as the value within the
leaf nodes using recursion and preorder traversal. All
other values within the tree (either the root node or internal
nodes) will be null. You may create any methods you see fit, so
long as the final binary tree is...
Hello guys can you t help me with writing a c++ program using classes to for doing a program about BINARY SEARCH TREES (BST) which would be capable of these things : 1.insert data to the current place in the tree with the ability to add same data many times in the tree 2.delete data from the tree 3.list the data according to *Preorder*-*Inorder*-Postorder* ways 4.count the data in the tree in two ways , first with counting the data that...
1) Extend the Binary Search Tree ADT to include
a public method leafCount that returns the number of leaf nodes in
the tree.
2) Extend the Binary Search Tree ADT to include a
public method singleParent-Count that returns the number of nodes
in the tree that have only one child.
3) The Binary search tree ADT is extended to
include a boolean method similarTrees that receives references to
two binary trees and determines whether the shapes of the trees are...
Can you help me write a Python 3.7 code for this question? Write a program using functions and mainline logic which prompts the user to enter a number, then generates that number of random integers and stores them in a list. It should then display the following data to back to the user: The list of integers The lowest number in the list The highest number in the list The total sum of all the numbers in the list The...