怎样编写一个程序,把一个有序整数数组放到二叉树中?
来源:互联网 发布:手机网络制式 编辑:程序博客网 时间:2024/06/06 17:06
分析:本题考察二叉搜索树的建树方法,简单的递归结构。
关于树的算法设计一定要联想到递归,因为树本身就是递归的定义。
#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;
}
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 怎样编写一个程序,把一个有序整数数组放到二叉树中(按行输出)
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 5、怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- 86 怎样编写一个程序,把一个有序整数数组放到二叉树中
- 编写一个程序,把一个有序整数数组放到二叉树中。
- 编写一个程序,把一个有序整数数组放到二叉树中
- 微软等数据结构+算法面试100题(5)--怎样编写一个程序,把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 把一个有序整数数组放到二叉树中
- 怎样编写一个程序,把一个有序整数数组放到二叉树中? 编写实现链表排序的一种算法。说明为什么你会选择用这样的方法?
- 题目:把一个有序整数数组放到二叉树。
- Oracle undo 表空间管理
- IOS开发中,在一个应用程序中启动另外一个应用
- LinearLayout中将按钮右对齐遇到的问题
- 客户端引用WCF实质
- QT按键背景图片 LineEdit Label网络
- 怎样编写一个程序,把一个有序整数数组放到二叉树中?
- jwplayer 部署方案1
- bootstrap源码分析系列:一,文件结构和开发环境
- 号码归属地显示应用
- 2014年规划
- Qt表格 QTableWidget
- 年报物化视图建图语句
- dynamic-insert和dynamic-update
- Maven内置属性及使用