void BinTree::bstreeToArrayHelper(NodeData* TreeArray[], int &index, Node* current) { if(current == NULL) { return; } bstreeToArrayHelper(TreeArray, index, current->left); TreeArray[index] = current->data; index++; bstreeToArrayHelper(TreeArray, index, current->right); }
//--------------------------------------------------------------------------- // bstreeToArrayHelper // Traverse tree inorder and store Nodedata* in tempArray. int BinTree::bstreeToArrayHelper(Node* current, NodeData* tempArray[]) { if(current == NULL) // base case return 0; // perform on left child int left = bstreeToArrayHelper(current->left, tempArray); NodeData* temp; temp = current->data; // store current's data in temp current->data = NULL; *(tempArray + left) = temp; // inset NodeData into array temp = NULL; // perform on right child int right = bstreeToArrayHelper(current->right, tempArray + left + 1); return left + right + 1; // return position }
//////////////////////////////////////////////////////////////////////////////////////////////////// // bstreeToArray: Fills in an array with data from the tree // Pre Conditions: Tree and array are properly inialized // Post Conditions: Array of NodeData is filled in //////////////////////////////////////////////////////////////////////////////////////////////////// void BinTree::bstreeToArray(NodeData* TreeArray[]) { int index = 0; bstreeToArrayHelper(TreeArray, index, root); makeEmpty(); }
//--------------------------------------------------------------------------- // bstreeToArray // Turns the BSTree into an array and destroys the BST. void BinTree::bstreeToArray(NodeData* tempArray[]) { bstreeToArrayHelper(root, tempArray); makeEmpty(); }