(4) tree : tsearch tfind tdelete


1.

#include <search.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/*
 * tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on entry.
 *
 * void *tsearch(const void *key, void **rootp, int(*compar)(const void *, const void *));
 * tsearch() returns a pointer to a matching item in the tree, or to the newly added item,
 * or NULL if  there was  insufficient  memory to add the item.
 *
 * void *tfind(const void *key, const void **rootp, int(*compar)(const void *, const void *));
 * tfind() returns a pointer to the item, or NULL if no match is found.
 * If there are multiple elements that match the key, the element returned is unspecified.
 *
 * void twalk(const void *root, void(*action)(const void *nodep, const VISIT which, const int depth));
 *
 * void *tdelete(const void *key, void **rootp, int(*compar)(const void *, const void *));
 * tdelete() returns a pointer to the parent of the item deleted, or NULL if the item was not found.
 */
struct stu{
	int stu_no;
	char *name;
};

typedef struct stu stu_t;

void *root = NULL;

void *xmalloc(unsigned n) {
	void *p;
	p = malloc(n);
	if (p) return p;
	fprintf(stderr, "malloc failure, insufficient memory.\n");
	exit(1);
}

int compare(const void *pa, const void *pb) {
	return ((stu_t*)pa)->stu_no - ((stu_t*)pb)->stu_no;
}

/*
typedef	enum {
	preorder,
	postorder,
	endorder,
	leaf
} VISIT;
*/
void action(const void *nodep, const VISIT which, const int depth) {
	switch(which) {
		case preorder:
			break;
		case postorder:
			printf("No. = %6d, Name = %s\n", (*(stu_t**)nodep)->stu_no, (*(stu_t**)nodep)->name);
			break;
		case endorder:
			break;
		case leaf:
			printf("No. = %6d, Name = %s\n", (*(stu_t**)nodep)->stu_no, (*(stu_t**)nodep)->name);
			break;
	}
}

int main() {
	stu_t *ptr;
	srand(time(NULL));
	int i;
	for (i = 0; i < 12; i++) {
		ptr = (stu_t *)xmalloc(sizeof(stu_t));
		//ptr->stu_no = (int *)xmalloc(sizeof(int));
		ptr->stu_no = rand()&0xFF;
		ptr->name = (char *)xmalloc(20);
		sprintf(ptr->name, "shanghai_%d", ptr->stu_no);
		//tsearch = search + create(add)
		stu_t** rtp = (stu_t**)tsearch(ptr, &root, compare);
		if (rtp == NULL) {
			printf("malloc failure....\n");
			exit(1);
		}else{
			printf("tsearch: No. = %6d, Name = %s\n", (*rtp)->stu_no, (*rtp)->name);
		}
	}
	//add a node manually
	ptr = (stu_t *)xmalloc(sizeof(stu_t));
	ptr->stu_no = 88;
	ptr->name = (char *)xmalloc(20);
	sprintf(ptr->name, "shanghai_%d", ptr->stu_no);
	tsearch(ptr, &root, compare);

	//tfind "88"
	stu_t** rt_find = tfind(ptr, &root, compare);
	if (rt_find == NULL) {
		printf("not found.\n");
	}else{
		printf("tfind: No. = %6d, Name = %s\n", (*rt_find)->stu_no, (*rt_find)->name);
	}
	//first twalk
	puts("first twalk, there is '88'");
	twalk(root, action);
	// tdelete "88"
	//returns a pointer to the parent of the item deleted, or NULL if the item was not found.
	stu_t ** rt_delete = (stu_t **)tdelete(ptr, &root, compare);
	if (rt_delete == NULL) {
		printf("'88' is not found to delete.\n");
	}else{
		printf("tdelete successful. its parent is: No. = %6d, Name = %s\n", (*rt_delete)->stu_no, (*rt_delete)->name);
	}
	//second twalk
	puts("second twalk, there is no '88'");
	twalk(root, action);

	return 0;
}


2.

#include <search.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
/*
 * tsearch(), tfind(), and tdelete() also return NULL if rootp was NULL on entry.
 *
 * void *tsearch(const void *key, void **rootp, int(*compar)(const void *, const void *));
 * tsearch() returns a pointer to a matching item in the tree, or to the newly added item,
 * or NULL if  there was  insufficient  memory to add the item.
 *
 * void *tfind(const void *key, const void **rootp, int(*compar)(const void *, const void *));
 * tfind() returns a pointer to the item, or NULL if no match is found.
 * If there are multiple elements that match the key, the element returned is unspecified.
 *
 *
 * void *tdelete(const void *key, void **rootp, int(*compar)(const void *, const void *));
 * tdelete() returns a pointer to the parent of the item deleted, or NULL if the item was not found.
 *
 * void twalk(const void *root, void(*action)(const void *nodep, const VISIT which, const int depth));
 */

void *root = NULL;

void *xmalloc(unsigned n) {
	void *p;
	p = malloc(n);
	if (p) return p;
	fprintf(stderr, "insufficient memory\n");
	exit(1);
}

int compare(const void *pa, const void *pb) {
	if (*(int *)pa < *(int *)pb) return -1;
	if (*(int *)pa > *(int *)pb) return 1;
	return 0;
}

void action(const void *nodep, const VISIT which, const int depth) {
	int *datap;
	switch(which) {
		case preorder:
			break;
		case postorder:
			datap = *(int **)nodep;
			printf("%6d\n", *datap);
			break;
		case endorder:
			break;
		case leaf:
			datap = *(int **)nodep;
			printf("%6d\n", *datap);
			break;
	}
}

int main() {
	int i, *ptr;

	srand(time(NULL));
	for (i = 0; i < 12; i++) {
		ptr = (int *)xmalloc(sizeof(int));
		*ptr = rand()&0xff;
		//returns a pointer to a matching item in the tree, or to the newly added item,
		//or NULL if  there was  insufficient  memory to add the item.
		int **val = (int *)tsearch((void *)ptr, &root, compare);
		if (val == NULL) {
			exit(1);
		}else{
			printf("value = %d\n", **val);
		}
	}
	twalk(root, action);
	return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值