Question

Write a C function with signature btbstree (int post, int size) that gets the post-order traversal of a binary search tree i

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

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. If not, PLEASE let me know before you rate, I’ll help you fix whatever issues. Thanks

//required method

bt *bstree(int* post, int size){

                if(size==0){

                                return NULL; //empty bst

                }

                //in a post order traversal, root value will always be at the end.

                //so we create a node with data=last value in the array, this node

                //will be the root node

                bt* root=(bt*) malloc(sizeof(bt*));

                root->data=post[size-1];

                root->left=NULL;

                root->right=NULL;          

                //looping through each remaining values in the array, from last to first

                for(int i=size-2;i>=0;i--){

                                //fetching current value

                                int value=post[i];

                                //creating a new node with this value

                                bt* newnode=(bt*) malloc(sizeof(bt*));

                                newnode->data=value;

                                newnode->left=NULL;

                                newnode->right=NULL;

                               

                                //taking reference to root

                                bt* node=root;

                                //a flag to control below loop

                                int added=0;

                                //looping until newnode is added in proper place

                                while(added==0){

                                                //if value is bigger than node's data, moving to right subtree

                                                if(value>node->data){

                                                                //if there is no right node, adding newnode as right node

                                                                if(node->right==NULL){

                                                                                node->right=newnode;

                                                                                added=1; //loop will end

                                                                }else{

                                                                                //otherwise moving to right subtree

                                                                                node=node->right;

                                                                }

                                                }else{

                                                                //if value is smaller than node's value, moving to left subtree,

                                                                //and adding to appropriate position

                                                                if(node->left==NULL){

                                                                                node->left=newnode;

                                                                                added=1; //loop will end

                                                                }else{

                                                                                node=node->left;

                                                                }

                                                }

                                }             

                }

                //returning the reference to the root

                return root;

}

Add a comment
Know the answer?
Add Answer to:
Write a C function with signature btbstree (int' post, int size) that gets the post-order traversal...
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
  • **Bonus Question** Write a C function with signature bt' bstree (int' post, int size) that gets...

    **Bonus Question** Write a C function with signature bt' bstree (int' post, int size) that gets the post-order traversal of a binary search tree in the form of an int array, reconstructs the tree and returns a pointer to the tree root as output (The second input parameter size specifies the number of elements in the tree). Note: Type bt is defined in the following way: typedef struct btreenode { struct btreenode *left; int data; struct btreenode "right; bt;

  • **Bonus Question Write a C function with signature bt' bstree (int' post, int size) that gets...

    **Bonus Question Write a C function with signature bt' bstree (int' post, int size) that gets the post-order traversal of a binary search tree in the form of an int array,reconstructs the tree and returns a pointer to the tree root as output (The second input parameter size specifies the number of elements in the tree). Note: Type bt is defined in the following way: typedef struct btreenodel struct btreenode *left; int data; struct btreenode *right; }bt; 12pt Paragraph B...

  • In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an...

    In C++ Write a function int * return_matches(int a[], int & size,TEST t) which returns an array (allocated off of the heap in the function) containing only the elements of array a that pass the “test” t. The function iterates through all the elements of a, and constructs a new array containing all the elements that pass the test. When the parameter corresponding to “size” is passed in (by reference), it contains the size of the array a. In the...

  • Please answer this programming question as fast as possible. Will upvote 45 pts Question 2 Let...

    Please answer this programming question as fast as possible. Will upvote 45 pts Question 2 Let BSTree be the following class which implements Binary Search Tree (BST). a. (5 points) Write the method public int maxElement 0 that returns the element of the tree with maximum value if the BST is not empty and returns -1 otherwise. b. (10 points) Write the method public void preOrderPrint 0 that prints out all the elements in the BST in the order of...

  • #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode...

    #include<iostream> using namespace std; class TNode { public: int val; TNode(){} TNode(int v){val = v;} TNode * left; TNode * right; TNode * parent; }; class BTree { public: //constructors and destructor BTree(); BTree(TNode *r);// initialize BTree with the root r. r is the only node. BTree(const BTree & t); // copy constructor BTree(const int *p, const int n);// similar to the copy constructor, but your input is of the array form. // input is given an array that denotes...

  • Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct...

    Coding Language: C++ Function Header: vector<vector<int>> printFromButtom(TreeNode* root) {} Definition for a binary tree node: struct TreeNode { int val; TreeNode *left; TreeNode *right; }; The Problem Complete the printFromButtom function that accepts a BST TreeNode and returns the nodes' value from left to right, level by level from leaf to root. This function will return vector<vector int which similar to a 2-D array. Function std: reverse (myvector.begin myVector en might be helpful. Definition for a binary tree node: struct...

  • Write a function int levelSearch(Node* root, int key) that takes as input the root node of...

    Write a function int levelSearch(Node* root, int key) that takes as input the root node of a Binary Search tree and a key. The function searches the key in the BST and returns the level of the found node. If the key is not found in the tree, return -1. The level starts at 0 for the root node and increases from top to bottom. We have defined the following node C++ Node class for you: class Node { public:         ...

  • 1) (10 pts) Write a recursive function that counts and returns the number of nodes in...

    1) (10 pts) Write a recursive function that counts and returns the number of nodes in a binary tree with the root root, that store an even value. Please use the struct shown and function prototype shown below. (For example, if the tree rooted at root stored 2, 3, 4, 8, 13, 18 and 20, the function should return 5, since there are five even values [2,4,8,18,20] stored in the tree. typedef struct node { int data; struct node* left;...

  • NEED HELP IN C!! Answer in C programming language. Question: Functions and .h file: Test function:...

    NEED HELP IN C!! Answer in C programming language. Question: Functions and .h file: Test function: part 1: part 2: For the assignment use the following structs for Binary Trees and Binary Search Trees. struct Binode { int value; struct Binode* left; struct BTnode* right; struct BTnode* parent; }; typedef struct Binode BTnode_t; typedef struct BST { BTnode_t* root; BST_t; Question 2 [10 points] Write a function that gets a binary tree and returns the sum of its elements. //...

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