I am trying to create a binary tree from in order and post order lists. I have two structures, Tree and TreeNode. I call Tree and then TreeNode. I keep getting overload and cannot convert TreeNode to Tree errors. I can't figure out how to add a node with my function.
Can't return the proper structure back from addnode function.
C program to create a binary tree. Need to follow the given structures. Need to call Tree structure first and than TreeNode. The add node function does not work because I can add the node and load the value. Keep getting function overload and cant convert Tree to TreeNode structures errors.
#include "pch.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node
{
int data;
struct node * left;
struct node * right;
} TreeNode;
typedef struct trnode
{
TreeNode * root;
} Tree;
//Prototypes for functions.
Tree * buildTree(int * in, int * post, int size);
TreeNode * addNode(int data);
int search(int inArray[], int strt, int end, int value);
Tree * buildUtil(int inArray[], int postArray[], int inStart, int inEnd, int* pIndex);
Tree * buildUtil(int inArray[], int postArray[], int inStart, int inEnd, int* pIndex)
{
int indexvalue;
TreeNode ln;
// Base case only one node
if (inStart > inEnd)
return NULL;
/* Pick current node from Postorder traversal using
postIndex and decrement postIndex */
TreeNode * node = addNode(postArray[*pIndex]);
(*pIndex)--;
indexvalue = *pIndex;
//If this node has no children then return
if (inStart == inEnd)
**//error cannot convert from TreeNode to Tree**
return node;
// Else find the index of this node in Inorder traversal
int iIndex = search(inArray, inStart, inEnd, indexvalue);
// Using index in Inorder traversal, construct left and right
//ERROR CANNOT CONVERT from TREE to node
node-> right = buildUtil(inArray, postArray, iIndex + 1, inEnd, pIndex);
node ->left = buildUtil(inArray, postArray, inStart, iIndex - 1, pIndex);
//ERROR CANT CONVERT FROM TREENODE TO TREE
return node;
}
// buildtree functioni calls helper function buildUtil which is recursive
Tree * buildTree(int * inArray, int * postArray, int size)
{
Tree *tr;
int preindex = size - 1;
tr->root;
return buildUtil(inArray, postArray, 0, preindex, &preindex);
}
/* Function to find index of value in arr[start...end]
The function assume List items that value is postsent in in[] */
int search(int inArray[], int strt, int end, int value)
{
int i;
for (i = strt; i <= end; i++) {
if (inArray[i] == value)
break;
}
return i;
}
TreeNode addNode(int data)
{
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
node-> data = data;
node->left = NULL;
node->right = NULL;
// no suitable constructor exists to convert from TreeNode to node
return (node);
}
int main()
{
// values of in and post lists. I can successfully read both arrays in from my read function
// in { 4, 8, 2, 5, 1, 6, 3, 7 };
// post { 8, 4, 5, 2, 6, 7, 3, 1 };
int *in = NULL;
int *post = NULL;
int insize;
int postsize;
insize = 8;
postsize = 8;
Tree * tr = buildTree(in, post, insize);
}