Question

Please help me with these, thank you!

3. **25.3 (Implement inorder traversal without using recursion) Implement the inorder method in BST using a stack instead of

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

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:

Input (stdin) 8 3 10 6 14 7 4 15 1 13 Your Output (stdout) Preorder traversal: 8 3 1 6 4 7 10 14 13 15 Inorder traversal: 1 3

____________________________
Comment down for any queries

Please give a thumb up

Add a comment
Know the answer?
Add Answer to:
Please help me with these, thank you! 3. **25.3 (Implement inorder traversal without using recursion) Implement...
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
  • JAVA CODE Topic: BST Animation For each task, submit the source code with detail comments. Objective:...

    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...

    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,...

    [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 com...

    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...

    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);...

  • You will be given several strings full of different keyboard characters, some of which are letters...

    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...

    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...

    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...

    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...

  • Use Java to implement a basic stack using an array of integers. For the stack, you...

    Use Java to implement a basic stack using an array of integers. For the stack, you will create an array of integers that holds 5 numbers. To make it easier, you can declare the array at the class level. That way you will be able to use the array in any method in your class without using parameters. Your input/output interface should look something like the following: What operation do you want to do? push What number do you want...

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
ADVERTISEMENT