Question

In c++ language write the code that moves binary search tree elements into a stack in...

In c++ language write the code that moves binary search tree elements into a stack in post order but without printing it

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

Algorithm:

Begin Declare postorder_traversal(struct node*t,struct tree**top) if(t==NULL) then print “Empty Tree”. Return. Print “Postorder Data Using Stack :”. Call push(t,top) function to insert values. Declare a pointer store against tree structure. Initialize struct tree*store=NULL. while(t!=NULL) store=*top; if(store->v==0) then if(t->r!=NULL) then (store->v)++ push(t->r,top) if(t->l!=NULL) then (store->v)++ push(t->l,top) if(store->v==0) then cout<d t=NULL pop(top) else cout<d t=NULL pop(top) if(*top!=NULL) then t=(*top)->link End

Code for the same:

#include<iostream> #include<stdlib.h> using namespace std; struct node { int d; struct node *l,*r; }; struct tree { int v; struct node*link; struct tree*n; }; struct node*create_node(int); struct node*create_node(int value) { struct node*new_node=(struct node*)malloc(sizeof(struct node)); if(new_node!=NULL) { new_node->d=value; new_node->l=new_node->r=NULL; return new_node; } else { printf("\n Memory overflow."); return NULL; } } void push(struct node*,struct tree*); void push(struct node*node,struct tree**top) { struct tree*new_node=(struct tree*)malloc(sizeof(struct tree)); if(new_node!=NULL) { new_node->link=node; new_node->n=*top; new_node->v=0; *top=new_node; } else { cout<<"\n Memory overflow."; return ; } } void pop(struct tree**); void pop(struct tree**top) { if(*top!=NULL) { struct tree*remove=*top; *top=(*top)->n; remove->link=NULL; remove->n=NULL; remove=NULL; } } void postorder_traversal(struct node*,struct tree**); void postorder_traversal(struct node*t,struct tree**top) { if(t==NULL) { cout<<"\n Empty Tree"; return; } push(t,top); struct tree*store=NULL; while(t!=NULL) { store=*top; if(store->v==0) { if(t->r!=NULL) { (store->v)++; push(t->r,top); } if(t->l!=NULL) { (store->v)++; push(t->l,top); } if(store->v==0) { t=NULL; pop(top); } } else { t=NULL; pop(top); } if(*top!=NULL) t=(*top)->link; } } int main(){ struct node*root=NULL; struct tree*top=NULL; root = create_node(20); root->l = create_node(10); root->r = create_node(30); root->r->r = create_node(7); root->l->l = create_node(25); root->l->r = create_node(35); root->l->r->r = create_node(40); root->l->l->r = create_node(26); postorder_traversal(root,&top); return 0; } 
Add a comment
Know the answer?
Add Answer to:
In c++ language write the code that moves binary search tree elements into a stack 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
  • Needed in Java Code Create a Binary Search Tree with the following elements in the order...

    Needed in Java Code Create a Binary Search Tree with the following elements in the order mentioned below: 5, 85, 89, 3, 2, 8, 65, 92 1.Print the Pre-order of this tree 2. Print the height and the balance factor of the nodes in the order they were inserted (5, 85, 89, 3, 2, 8, 65, 92) in the form of a table with three columns and 9 rows. Use column headers “Node”, “Height”, and “Balance Factor” for the three...

  • I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if...

    I need question 9-10 answered. Thank you Question 1 iShow the resulting binary search tree if we are to insert following elements into the tree in given order, [34, 12, 23, 27,31,9,11,45, 20, 37. i) Show the resulting balanced binary search tree if we are to insert following sorted elements into the tree, [9,12,21, 23, 29, 31, 34, 45, 48, 52, 55] iii What is the pre-order traversal of the balanced binary search tree? v) What is the post-order traversal...

  • LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display...

    LANGUAGE: C++ Write a class to create the binary tree (insert, delete, search, exit) and display the output using inorder, preorder and postorder tree traversal methods.

  • For Computer Science: Write a binary tree representation for Binary Search of 20 elements and give...

    For Computer Science: Write a binary tree representation for Binary Search of 20 elements and give the worst-case

  • Write an algorithm that will check if a tree is a binary search tree. If a...

    Write an algorithm that will check if a tree is a binary search tree. If a tree is not a binary search tree, then it will repair it to make it one. (a) Write pseudo-code for both functions. (b) What is the running time for your algorithm?

  • c++ Let's say sub_root is a node in a given binary search tree. Write a code...

    c++ Let's say sub_root is a node in a given binary search tree. Write a code segment to find the immediate successor of the sub_root

  • Suppose that the following elements are added in the specified order to an empty binary search...

    Suppose that the following elements are added in the specified order to an empty binary search tree: Meg, Stewie, Peter, Joe, Lois, Brian, Quagmire, Cleveland Write the elements of the tree above in the order they would be seen by a pre-order, in-order, and post-order traversal. Type your solutions with the elements separated by spaces and/or commas, such as: One, Two, Three, Four pre-order in-order post-order

  • C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N...

    C++ ONLY Threaded Binary Search Tree Since a binary search tree with N nodes has N + 1 NULL pointers, half the space allocated in a binary search tree for pointer information is wasted. Suppose that if a node has a NULL left child, we make its left child pointer link to its inorder predecessor, and if a node has a NULL right child, we make its right child pointer link to its inorder successor. This is known as a...

  • You are given the root of a Binary Search Tree. Print the leaf elements of the...

    You are given the root of a Binary Search Tree. Print the leaf elements of the tree starting from right to left. We have defined the following node C++ Node class for you: class Node { public:         int name; Node* left = NULL;         Node* right = NULL; }; Function to code: void printLeaves(Node* root); The first input in test cases are nodes of a tree which are inserted in that order. You don't need to implement insert. You have access...

  • Recall that in a binary search tree, at every node, all elements to the left of...

    Recall that in a binary search tree, at every node, all elements to the left of the node have a smaller key, and all elements to the right of a node have a larger key. Write a program called that takes two parameters: a pointer to a binary search tree node, and an int parameter called min which will print all the elements bigger than the specified value, min. Your program should allow the user to enter data (integer) from...

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