折半查找数的构建:
1. 头文件:主要的数据结构:
(1)bsearch_type.h
#ifndef __BSEARCH_TYPE_H__
#define __BSEARCH_TYPE_H__
/*** inlcude files ***/
#include
enum dir{ L=0, R=!L, NLR};
typedef struct node_t {
unsigned int low;
unsigned int high;
unsigned int mid;
enum dir dir;
unsigned int level;
struct node_t * pre;
struct node_t * left;
struct node_t * right;
} node;
typedef node* BST;
typedef struct list_node_t {
BST ptr;
struct list_node_t * next;
} list_node;
typedef list_node * list;
typedef list_node * leaf;
typedef struct path_node_t {
unsigned int n;
struct path_node_t * next;
} path_node;
typedef path_node * path;
#endif
(2) bserach.h //折半查找树的构建的相关函数的声明
#ifndef __BSEARCH_H__
#define __BSEARCH_H__
/******* include files ********************/
#include "bsearch_type.h"
#include
#include
#include
/** create the Binary Search Tree ***/
BST createBST(int len);
/** destroy the Binary Search Tree ****/
int destroyBST(BST* root);
void preOrderTravelBST(BST root);
void midOrderTravelBST(BST root);
void postOrderTravelBST(BST root);
list getLeaves(BST root, list head);
path getPath(leaf l, path p);
int getPaths(list head, path a[],int* size);
void showPath(path p);
void showMBlocks(path p);
void showBlocks(path p);
void showSets(path p);
#endif
2.折半查找树的构建函数的实现: bsearch.c
#include "bsearch.h"
int addChildren(BST p) {
if(p==NULL) {return 0;}
int count = 0;
int lcount = 0;
int rcount = 0;
int mid = p->mid;
int low = p->low;
int high = p->high;
//int dir = p->dir;
int level = p->level;
/*
//if(p->pre == NULL) p->pre = p;
//printf("levele:%d, mid=%d, pre=%d, dir=%d, low=%d, mid=%d,high=%d,dir=%d\n", \
// level, mid, (p->pre == NULL)? p->mid:p->pre->mid, dir, low, mid, high, dir);
*/
node * tmp;
// the left child;
if(low <= mid -1) {
tmp = (node*) malloc( sizeof(node) );
assert(tmp != NULL);
tmp->low = low;
tmp->high = mid-1;
tmp->mid = (low+mid-1)>>1;
tmp->dir = L;
tmp->level = level + 1;
tmp->pre = p;
tmp->left = NULL;
tmp->right = NULL;
p->left = tmp;
count++;
}
// the right child;
if(high >= mid+1 ) {
tmp = (node*) malloc(sizeof(node));
tmp->low = mid+1;
tmp->high = high;
tmp->mid = (mid+1 + high)>>1;
tmp->dir = R;
tmp->level = level+1;
tmp->pre = p;
tmp->left = NULL;
tmp->right = NULL;
p->right = tmp;
count++;
}
if(p->left) lcount = addChildren(p->left);
if(p->right) rcount = addChildren(p->right);
return count + lcount + rcount;
}
BST createBST( int len) {
assert(len>=1);
int low,mid, high;
int count = 0;
BST root;
low = 0 ;
high = len -1;
mid = (low + high)>>1;
root = (BST) malloc(sizeof(node));
assert(root != NULL);
count ++;
if(NULL != root) {
root->low = low;
root->high = high;
root->mid = mid;
root->dir = NLR;
root->level = 0;
root->pre = NULL;
if(len == 1) {
root->left = NULL;
root->right = NULL;
return root;
}
count += addChildren(root);
}
//printf("len=%d,count=%d\n", len, count);
assert(count == len);
return root;
}
void deleteBST(BST root ) {
if(root) {
if(root->left) {
deleteBST(root->left);
root->left=NULL;
}
if(root->right){
deleteBST(root->right);
root->right = NULL;
}
//printf("\n%d: ",root->mid);
if(root->pre) root->pre = NULL;
//printf("%p: %p,%p,%p ", root, root->pre, root->left, root->right);
free(root);
root=NULL;
//printf("%p ", root);
}
}
int destroyBST(BST* T) {
BST root = *T;
if(root == NULL) return 1;
deleteBST(root);
*T = NULL;
return 0;
}
void printNode(node * n) {
assert(n);
printf("%d ", n->mid);
}
void preOrderTravelBST(BST root) {
if(root) {
printNode(root);
if(root->left) preOrderTravelBST(root->left);
if(root->right) preOrderTravelBST(root->right);
}
}
void midOrderTravelBST(BST root) {
if(root) {
if(root->left) midOrderTravelBST(root->left);
printNode(root);
if(root->right) midOrderTravelBST(root->right);
}
}
void postOrderTravelBST(BST root) {
if(root) {
if(root->left) postOrderTravelBST(root->left);
if(root->right) postOrderTravelBST(root->right);
printNode(root);
}
}
list getLeaves(BST root, list head) {
if(root) {
if(root->left==NULL && root->right == NULL) {
list tmp = (list) malloc(sizeof(list_node));
assert(tmp);
if(tmp == NULL) exit(1);
tmp->ptr = root;
tmp->next = head;
head = tmp;
}
if(root->left) {
head=getLeaves(root->left, head);
}
if(root->right) {
head=getLeaves(root->right, head);
}
}
return head;
}
path getPath(leaf l, path head) {
if(l) {
node* ptr = l->ptr;
while(ptr) {
path tmp = (path) malloc(sizeof(path_node));
assert(tmp);
if(tmp == NULL) exit(1);
tmp->n = ptr->mid;
tmp->next = head;
head = tmp;
ptr = ptr->pre;
}
}
return head;
}
int getPaths(list head, path a[],int* size) {
list ptr = head;
int i = 0;
path p = NULL;
while(ptr) {
p = getPath(ptr,p);
a[i++] = p;
p = NULL;
ptr = ptr->next;
}
*size = i;
return *size;
}
void showPath(path p) {
while(p) {
printf("%d, " , p->n);
p = p->next;
}
}
void showMBlocks(path p){
while(p) {
printf("m%d, " , p->n >> 2 );
p = p->next;
}
}
void showBlocks(path p){
while(p) {
printf("c%d, " , (p->n >> 2) % 16);
p = p->next;
}
}
void showSets(path p){
while(p) {
printf("f%d, " , (p->n >> 3) % 8);
p = p->next;
}
}
3. 测试程序:main.c
#include
#include
#include "bsearch.h"
#define MAX 1000
int main(int argc, char* argv[])
{
path a[MAX];
BST root = createBST(12);
printf("BST Created....\n");
printf("\nmidOrderTravel: ");
midOrderTravelBST(root);
printf("\nleaves:");
list head = NULL;
head = getLeaves(root, head);
list L = head;
while(L) {
printf("%d ", L->ptr->mid);
L = L->next;
}
printf("\n");
int size,n;
n = getPaths(head, a, &size);
assert(size == n);
int i;
for(i=0; i