赛格。填充BST

时间:2016-03-06 21:01:00

标签: segmentation-fault binary-search-tree

我对Binary Search Trees很新,所以我的错误可能很简单/愚蠢。我正在尝试从两个有序数组中填充两个BST,但是我得到了分段错误:11当我运行.x文件时。任何指针/建议将不胜感激!我在下面列出了我的主文件。

#include "TreeNode.hpp"
#include <iostream>
using namespace std;

bool twoTheSame( TreeNode *p, TreeNode *q );
TreeNode* pConstructor();
TreeNode* qConstructor();
TreeNode* populateLeft( TreeNode *root, int array[], int left, int right );
TreeNode* populateRight( TreeNode *root, int array[], int left, int right );

int main() {
    TreeNode *pRoot = pConstructor();
    TreeNode *qRoot = qConstructor();
    if ( twoTheSame( pRoot, qRoot ) )
        cout << "p and q have at least one element in common\n";
    else
        cout << "p and q have no elements in common\n";
}



TreeNode* pConstructor() {
    int array[] = {0};
    int numElements = 0;
    int mid;
    int num;

    cout << "Enter a number for p (ordered, -1 to quit): ";
    cin >> num;

    while ( num != -1 ) {
        array[numElements] = num;
        numElements++;
        cout << "Enter another number (in order, -1 to quit): ";
        cin >> num;
    }
    mid = ( numElements - 1 ) / 2;
    TreeNode *root = new TreeNode(array[mid]);
    TreeNode *leftTree = new TreeNode();
    TreeNode *rightTree = new TreeNode();
    root->setLeft(leftTree);
    root->setRight(rightTree);
    populateLeft( leftTree, array, 0, mid-1 );
    populateRight( rightTree, array, mid+1, numElements-1 );
    return root;
}

TreeNode* qConstructor() {
    int array[] = {0};
    int numElements = 0;
    int mid;
    int num;

    cout << "Enter a number for q (ordered, -1 to quit): ";
    cin >> num;

    while ( num != -1 ) {
        array[numElements] = num;
        numElements++;
        cout << "Enter another number (in order, -1 to quit): ";
        cin >> num;
    }
    mid = ( numElements - 1 ) / 2;
    TreeNode *root = new TreeNode(array[mid]);
    TreeNode *left = new TreeNode();
    TreeNode *right = new TreeNode();
    root->setLeft(left);
    root->setRight(right);
    populateLeft( left, array, 0, mid-1 );
    populateRight( right, array, mid+1, numElements-1 );
    return root;
}

TreeNode* populateLeft( TreeNode* root, int array[], int left, int right ) {
    if ( left > right )
        return nullptr;
    if ( left == right ) {
        root->value() = array[left];
        return root;
    }
    int mid = ( left + right ) / 2;
    root->value() = array[mid];
    TreeNode *leftTree = new TreeNode();
    TreeNode *rightTree = new TreeNode();
    root->setLeft(leftTree);
    root->setRight(rightTree);
    populateRight( leftTree, array, mid+1, right );
    populateLeft( rightTree, array, left, mid-1 );
    return root;
}

TreeNode* populateRight( TreeNode *root, int array[], int left, int right ) {
    if ( left > right )
        return nullptr;
    if ( left ==right ) {
        root->value() = array[right];
        return root;
    }
    int mid = ( left + right ) / 2;
    TreeNode *leftTree = new TreeNode();
    TreeNode *rightTree = new TreeNode();
    root->setLeft(leftTree);
    root->setRight(rightTree);
    root->value() = array[mid];
    populateLeft( leftTree, array, left, mid-1 );
    populateRight( rightTree, array, mid+1, right );
    return root;
}

0 个答案:

没有答案