代码拉取完成,页面将自动刷新
/*
** A binary search tree implemented with a static array. The
** array size can be adjusted only by changing the #define and
** recompiling the module. 静态数组实现二叉树
*/
#include "tree.h"
#include <assert.h>
#include <stdio.h>
#define TREE_SIZE 100 /* Max # of values in the tree */
#define ARRAY_SIZE ( TREE_SIZE + 1 )
/*
** The array that holds the values in the tree.
*/
static TREE_TYPE tree[ ARRAY_SIZE ] = {0};
static int left_child(int current){
return current * 2;
}
static int right_child(int current){
return current * 2 + 1;
}
void insert(TREE_TYPE value){
assert(value!= 0);
/*
** Start with the root node.
*/
int current = 1;
while(tree[current] ! = 0){
if(value < tree[current]){
current = left_child(current);
}else{
//And make sure we don't have a duplicate value!
assert(value != tree[current]);
current = right_child(current);
}
assert( current < ARRAY_SIZE );
}
tree[ current ] = value;
}
TREE_TYPE *find(TREE_TYPE value){
int current = 1;
while( current < ARRAY_SIZE && tree[current] != value){
if(value < tree[current]){
current = left_child(current);
}else{
current = right_child(current);
}
}
if(current < ARRAY_SIZE){
//返回指向目标值的指针
return tree + current;
}else{
//未找到
return 0;
}
}
static void do_pre_order_traverse(int current, void(*callback)(TREE_TYPE value)){
if( current < ARRAY_SIZE && tree[ current ] != 0 ){
callback( tree[ current ] );
do_pre_order_traverse( left_child( current ),
callback );
do_pre_order_traverse( right_child( current ),
callback );
}
}
//从根节点1开始前序遍历
void pre_oder_traverse(void(*callback)(TREE_TYPE value)){
do_pre_order_traverse(1, callback);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。