简体   繁体   中英

Binary Search Tree from Pre order array

I'm trying a standard interview question- reconstructing a Binary Search Tree from a pre order array. Here's my code

class bst{
node* root
public:
static node* bstFromPreOrderUtil(int* arr, int* index, int key, int min, int max, int size){
        if(*index >= size) return nullptr;
        node* r = nullptr;
        if(key> min && key < max){
            r = new node(key);
            *index = *index + 1;
        }
        if(*index < size){
            r->left = bstFromPreOrderUtil(arr, index, arr[*index], min, key, size);
            r->right = bstFromPreOrderUtil(arr, index, arr[*index], key, max, size);
        }
        return r;

    }
static node* bstFromPreOrder(int* arr, int size){
     int index=0;
     return bstFromPreOrderUtil(arr, &index, arr[0], INT_MIN, INT_MAX, size);

    }
};

The definition of the struct node is here

struct node{
    node():data(0), left(nullptr), right(nullptr){}
    node(int data):data(data), left(nullptr), right(nullptr){}
    int data;
    node* left;
    node* right;
};

This is my calling code,

int arr[] = {15, 13, 3, 11, 21, 19, 29};

 node* root = bst::bstFromPreOrder(arr, 7);

I keep getting the following error at the point where the function makes the recursive call

EXC_BAD_ACCESS (code=2, address=0x7fff5ab9cff8))

Any help appreciated.

Move the

if(*index < size){
    r->left = bstFromPreOrderUtil(arr, index, arr[*index], min, key, size);
    r->right = bstFromPreOrderUtil(arr, index, arr[*index], key, max, size);
}

inside the first if block. You have to copy paste correctly ;-)

The answer depends on the type of binary tree.

If it's about a binary search tree classically built, then to scan the array and insert each key in a binary tree builds exactly the original tree.

You could use a routine for insertion such as:

Node * insert_in_bst(Node *& r, int key)
{
  if (r == nullptr) // an empty tre reached?
    {
      r = new Node;
      r->key = key;
      return r;
    }

  if (key < r->key)
    return insert_in_bst(r->left, key);
  else
    return insert_in_bst(r->right, key);
}

As you can see, this algorithm only modifies the tree by replacing a null pointer by the new node; the rest of the tree remains with the same shape. As exercise, build some random trees with this algorithm, take their preorder traversals and verify that each traversal corresponds to the exact order of insertion.

Afterward, you scan the preorder array arr :

Node root = nullptr; // tree is originally empty
for (int i = 0; i < n; i++)
  insert_in_bst(r, arr[i]);

If the tree is not a binary search tree, then you can not rebuild from the preorder traversal of its keys. However, you could rebuild from a preorder traversal that contents the keys and the external nodes. The external nodes can be seen as the null pointers. A little more complicated but possible. Let me know if you are interested and I explain.

Good luck!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM