evilsithgirl 0 Newbie Poster

Hello. Here is my homework problem: I have a set of numbers for which i have the inorder and preorder traversal. I need to construct a binary tree from those. I have most of the code written i just need some help with a specific part. I have heavily commented my code so its easy to follow and will make the part i need help with red and bold.

#include <iostream>
#include <algorithm>
//#include <fstream>

using namespace std;

struct node
{
    int data;
    node* pLeft;
    node* pRight;
    // Constructor
    node(int value) : data(value), pLeft(0), pRight(0) {}
};

//inorder - A pointer to the inorder array
//preorder- A pointer to the preorder array
//left    - Leftmost index into the inorder array
//right   - Rightmost index into the inorder array
//curr    - Index into current preorder array
node* buildtree(int* inorder, int* preorder, int left, int right, int& curr )
{
	int index;
//1.  If left == right then return NULL.
	if (left == right)
		return NULL;
//2.  Create a new temp node with the same value as preorder[curr].
	node* temp;
	temp->data = preorder[curr];
//3.  If left+1 == right return this new node we just created.
	if ((left+1) == right)
		return temp;
[B]//4.  Find the index of the value preorder[curr] in the inorder array. 
// I am not sure how to find the position of this number in the inorder array.  this is where some tips would be helpful. [/B]

/*5.  If the index calculated in step 4 is greater than left...
  5a. Increment curr.
  5b. Set pLeft value of node created in step 2 equal to value returned from
      buildtree(inorder,preorder,left,index from step 4,curr)*/	
	if(index > left)
	{
		right = index;
		curr++;
		pLeft = buildtree(inorder, preorder, left, right, curr);
	}
/*6.  If the index calculated in step 4 (plus 1) is less than right...
  6a. Increment curr.
  6b. Set pRight value of node created in step 2 equal to value returned from
      buildtree(inorder,preorder,index from step 4 (plus 1),right,curr)*/
	else if((index +1) < right)
	{
			curr++;
			left = (index +1);
			pRight = buildtree(inorder, preorder, left, right, curr);
	}
//7.  Return node from step 2.
	return temp;
}

void InorderTraverse(node* root)
{
    if( root->pLeft ) InorderTraverse(root->pLeft);
    cout << root->data << ' ';
    if( root->pRight ) InorderTraverse(root->pRight);
}

void PreorderTraverse(node* root)
{
    cout << root->data << ' ';
    if( root->pLeft ) PreorderTraverse(root->pLeft);
    if( root->pRight ) PreorderTraverse(root->pRight);
}

int main()
{
    int inorder[] = { 23, 1, 15, 9, 6, 22, -19 };
    int preorder[]= { 6, 15, 1, 23, 9, -19, 22 };
    int count = 0;

    // Build the binary tree
    node* root = buildtree(inorder,preorder,0,7,count);

    // Test the traversals to see if they match the above arrays
    cout << "Inorder Traverse : ";
    InorderTraverse(root);
    cout << "\nPreorder Traverse: ";
    PreorderTraverse(root);

    return 0;
}

Notes: I am still making this code so if you see any other errors please point them out. Any hints or suggestions on other ways to solve this problem would also be helpful.
Just a refresher:
Inorder traversal is: Left Node -> Parent -> Right node
Preorder traversal is: Parent -> left node -> right node

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.