Example #1
0
//---------------------------------------------------------------------------
// arrayToBSTreeHelper
// Recursive helper function. Calls itself on the left and right sides of the
// array once a NodeData object has been located, stored, and inserted into
// a BST.
void BinTree::arrayToBSTreeHelper(NodeData* tempArray[], const Node* current, int low, int high)
{
    if (low > high) // terminate
        current = NULL;
    else
    {
        int root = (low + high) / 2; // location of data to insert into BST
        NodeData* temp;
        temp = tempArray[root]; // temp now holds data that is to be removed from array
        tempArray[root] = NULL; // empty out tempArray
        insert(temp); // insert temp into BST
        // call same function on left array
        arrayToBSTreeHelper(tempArray, current, low, root - 1);
        // call same function on right array
        arrayToBSTreeHelper(tempArray, current, root + 1, high);
    }
}
Example #2
0
	BinTree::Node* BinTree::arrayToBSTreeHelper(NodeData* TreeArray[], int low, int high, Node* &current) 
	{

		if (low > high) {
			return NULL;
		} else {
			current = new Node;     // exception is thrown if memory is not allocated
			current->data = TreeArray[(low + high) / 2];
			TreeArray[(low + high) / 2] = NULL;

			if (low == high) {
				current->left = NULL;
				current->right = NULL;
				return current;
			} else {
				int mid = (low + high) / 2;
				current->right = arrayToBSTreeHelper(TreeArray, mid + 1, high, current->right);
				current->left = arrayToBSTreeHelper(TreeArray, low, mid - 1, current->left);
				return current;
			}
		}
	}
Example #3
0
//---------------------------------------------------------------------------
// arrayToBSTree
// Builds a BST from tempArray. Creates an empty array of size 100 to begin
// with.
void BinTree::arrayToBSTree(NodeData* tempArray[])
{
    int high = 0;
    int low = 0;
    for(int i = 0; i < 100; i++) // how many indexes are empty?
    {
        if (tempArray[i] != NULL)
            high++;
        else
            tempArray[i] = NULL;
    }
    // recursive helper function performs the calculations
    arrayToBSTreeHelper(tempArray, root, low, high-1);
}
Example #4
0
////////////////////////////////////////////////////////////////////////////////////////////////////
// arrayToBSTree: Recreates the a tree based on the array of NodeData
// Pre Conditions: Tree and array are properly inialized
// Post Conditions: Tree is returned
////////////////////////////////////////////////////////////////////////////////////////////////////
	void BinTree::arrayToBSTree(NodeData* TreeArray[])
	{
		int high = getArraySize(TreeArray);
		int low = 0;
		root = arrayToBSTreeHelper(TreeArray, low, high, root);
	}