示例#1
0
// *& is reference to pointer, similar to double pointer(**)
BSTNode* build_bst(ListNode *&head, int start, int end)
{
    // terminate condition
    if (start > end) return NULL;

    int mid = start + (end - start) / 2;
    
    // traverse to the left first
    BSTNode *leftChild = build_bst(head, start, mid-1);
    BSTNode *parent = new BSTNode(head->m_Data);
    head = head->m_Next;
    BSTNode *rightChild = build_bst(head, mid+1, end);
    parent->left = leftChild;
    parent->right = rightChild;

    return parent;

}
示例#2
0
BSTNode* convert_sorted_list(ListNode *head)
{
    int len = 0;
    ListNode *curr = head;
    // find the length of the list
    while(curr != NULL) {
        curr = curr->m_Next;
        len++;
    }
        
    return build_bst(head, 0, len-1); //both start and end is inclusive
}
示例#3
0
void test()
{
    node_t* root = build_bst(30);

    node_t* head = tree_to_list(root);

    while (head != NULL)
    {
        printf("%d  ", head->data);
        head = head->right;
    }
    printf("\n");
}
示例#4
0
void ahnentafel_fill(struct ahnentafel_t* bs, iter_t iter, void* opaque)
{
  for (int i = 0; i < bs->size; i++)
    bs->array[i] = *(iter(opaque));
  build_bst(bs);
}