hello, I'm trying to build an insert item method for a array based implementation of a binary search tree. I have figured out the base comparison to compare two values and place the value that is being compared to the left or right of the other node. However, I'm having trouble figuring out how to structure it in such a way where it will iteratively compare past the root node. Can someone help? Thanks.

Here is my code:

#include <iostream>
#include <fstream>
#include <string>
#include "BST_Checker.h"

int main(void)
{
    using namespace std;
	BST_Checker Tree1;
	
	char word3 = 'm';
	char word4 = 'u';
	char word5 = 'l';
	char word6 = 't';
	char word7 = 'a';
	char word8 = 'n';

	Tree1.InsertItem(word3);
	Tree1.InsertItem(word4);
	Tree1.InsertItem(word5);
	Tree1.InsertItem(word6);
	Tree1.InsertItem(word7);
}

#ifndef _bst_checker
#define _bst_checker


class BST_Checker
{
public:
	BST_Checker();
	void InsertItem(char tree_node);

private:
	int index_count;
	char tree[100][100];
	int root;
	char compare;
};

#endif

#include "BST_Checker.h"
#include <iostream>
#include <cstddef>
#include <string>

using namespace std;

BST_Checker::BST_Checker()
{
	index_count = 0;
	root = 0;
}


void BST_Checker::InsertItem(char tree_node)
{
		int i = 0;
		int row = 0;

		//exception for root node condition
		if(root == 0){
		tree[root][0] = tree_node; //set tree_node as root value
		root++; //increment root so condition no longer met
		cout << tree[0][0] << endl;
		}
	
	//left node condition
	if(tree_node < tree[i][0])
	{
	i = ((2*row) + 1);
	cout << i << endl;
	tree[i][0] = tree_node;
	cout << tree[i][0] << endl;
	}

	//right node condition
	if(tree_node > tree[i][0])
	{
	i = ((2*row) + 2);
	cout << i << endl;
	tree[i][0] = tree_node;
	cout << tree[i][0] << endl;
	}
		
}

right now any char value that is passed past the first three get written over in the array. I need it to make successive comparisons so it will further propagate the tree.
Thanks!

I figured this one out and have modified it to read an array of chars. I can post the solution if anyone is having a similar problem and is interested in seeing the solution.

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.