我很难想象我的程序将如何插入元素。这是老师给我们的代码:
int arr[] = { 3, -2, 11, 7, 12, 1, 4, 5, 33, 13 };
int n = 10;
int cnt = 0;
typedef struct node*po;
struct node {
int data;
po left;
po right;
};
po ibd(int n) {
po holder;
if (n>0) {
int nl = n / 2;
int nr = n - nl - 1;
holder = new node;
holder->data = arr[cnt++];
holder->left = ibd(nl);
holder->right = ibd(nr);
return holder;
}
else {
return NULL;
}
}
遗憾的是,我无法理解和想象它是如何将元素放入树中的。据我了解,它使用递归分治算法将数组分成两部分并添加元素,但是我无法理解哪个元素成为根。有人可以帮我想象一下插入所有内容后树的外观吗?