C code for above problem
#include<stdio.h>
#include<stdlib.h>
typedef struct btreenode
{
int data;
struct btreenode *left,*right;
}bt;
int post_index=0;
// comparator that is used to sort the array
int comparator(const void *a, const void *b)
{
return (*(int*)a-*(int*)b);
}
// function that creates a new node and returns it
bt * create_node(int data)
{
bt *root=(bt *)malloc(sizeof(bt));
root->data=data;
root->left=NULL;
root->right=NULL;
return root;
}
// function that creates a tree using given postorder and
returns the root
bt * create_tree(int *in,int *post,int start,int end)
{
if(start>end || post_index<0)
return NULL;
int value=post[post_index--];
int ind=-1;
for(int i=start;i<=end;i++)
{
if(in[i]==value)
{
ind=i;
break;
}
}
bt *root=create_node(value);
root->right=create_tree(in,post,ind+1,end);
root->left=create_tree(in,post,start,ind-1);
return root;
}
// function that returns the tree that is equivalent to give
postorder
bt * bstree(int *post,int size)
{
int in[size];
for(int i=0;i<size;i++)
in[i]=post[i];
qsort(in,size,sizeof(int),comparator);
post_index=size-1;
return create_tree(in,post,0,size-1);
}
// function that prints the tree in post order
void postorder(bt *root)
{
if(!root)
return;
postorder(root->left);
postorder(root->right);
printf("%d ",root->data);
}
// testing main method
int main()
{
int post[]={1,3,2,5,7,6,4};
int size=sizeof(post)/sizeof(int);
bt *root=bstree(post,size);
printf("Post order Traversal: ");
postorder(root);
printf("\n");
return 0;
}
Sample output
Given post order and tree's post order traversal looks same.
Mention in comments if any mistakes or errors are found. Thank you.
**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;
Write a C function with signature btbstree (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;
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...
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...
#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...
Write a recursive function (C++) that searches a binary tree that is NOT ordered like a BST. In other words, there is no ordering relationship among the parent, child or sibling node values. Use the following prototype and data structure: struct node { node *left; node *right; int val; }; // First parameter: pointer to a node // Second parameter: the value to find bool searchValue(node *, int); The function searchValue() should return true if the integer value in the...
bool binarySearch(int a[], int size, int k); Write a function called binarySearch that finds an occurrence of an integer k in an array a using binary search. The function signature is given above. Write your own implementation of binary search; do not use the search function available in the C++ standard library. Binary search assumes that the sequence of values (stored in an array in our case) is either non-decreasing or non-increasing. Assume that the array passed into binarySearch is...
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...
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. //...