How to write a program , Put an ordered array of integers into a binary tree ?
analysis : This paper investigates the construction method of binary search tree , Simple recursive structure .
The algorithm design of tree must be associated with recursion , Because the tree itself is the definition of recursion .

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

struct btree {
    struct btree *left;
    struct btree *right;
    int value;
};

void create_btree(struct btree **rt, int *arr, int r, int l)
{
    int pos;
    struct btree *root;
    if (r > l) {
        *rt = NULL;
        return;
    }
    pos = (r + l) / 2;
    root = (struct btree *)malloc(sizeof(struct btree));
    assert(root != NULL);
    root->value = arr[pos];
    *rt = root;
    create_btree(&(root->left), arr, r, pos - 1);
    create_btree(&(root->right), arr, pos + 1, l);
}

int A[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
/*
 *                5
 *            3            8
 *
 */

void display_btree(struct btree *root)
{

    if (root == NULL) {
        return;
    }
    display_btree(root->left);
    printf("%d ", root->value);
    display_btree(root->right);
}
int main()
{
    struct btree *root = NULL;
    create_btree(&root, A, 0, 9);
    printf("----------------------\n");
    display_btree(root);
    printf("\n----------------------\n");
    return 0;
}

Technology
©2020 ioDraw All rights reserved
Vue The difference between single page and multi page 0.96OLED display -4 Line SPI explain R In language Axis() Detailed explanation of function parameters JAVA Snake games ( Source code + notes )C++ vector Simulation Implementation of class javascript Adding and deleting form information CCF A series of questions --2016 year 4 Month 1 Calculation of break point postman Interface test get timestamp and MD5 encryption 【 Recommended benefits 】c++ use easyx Making pixel birds , Simple hands on games TCP/IP There are so many loopholes in the agreement ?