001 红黑树(二)之 C语言的实现(3)

80 篇文章 0 订阅
80 篇文章 0 订阅

红黑树的测试文件(rbtree_test.c):

 1/**
 2 * C语言实现的红黑树(Red Black Tree)
 3 *
 4 * @author skywang
 5 * @date 2013/11/18
 6 */
 7#include <stdio.h>
 8#include "rbtree.h"
 9#define CHECK_INSERT 0    // "插入"动作的检测开关(0,关闭;1,打开)
10#define CHECK_DELETE 0    // "删除"动作的检测开关(0,关闭;1,打开)
11#define LENGTH(a) ( (sizeof(a)) / (sizeof(a[0])) )
12void main()
13{
14    int a[] = {10, 40, 30, 60, 90, 70, 20, 50, 80};
15    int i, ilen=LENGTH(a);
16    RBRoot *root=NULL;
17    root = create_rbtree();
18    printf("== 原始数据: ");
19    for(i=0; i<ilen; i++)
20        printf("%d ", a[i]);
21    printf("\n");
22    for(i=0; i<ilen; i++)
23    {
24        insert_rbtree(root, a[i]);
25#if CHECK_INSERT
26        printf("== 添加节点: %d\n", a[i]);
27        printf("== 树的详细信息: \n");
28        print_rbtree(root);
29        printf("\n");
30#endif
31    }
32    printf("== 前序遍历: ");
33    preorder_rbtree(root);
34    printf("\n== 中序遍历: ");
35    inorder_rbtree(root);
36    printf("\n== 后序遍历: ");
37    postorder_rbtree(root);
38    printf("\n");
39    if (rbtree_minimum(root, &i)==0)
40        printf("== 最小值: %d\n", i);
41    if (rbtree_maximum(root, &i)==0)
42        printf("== 最大值: %d\n", i);
43    printf("== 树的详细信息: \n");
44    print_rbtree(root);
45    printf("\n");
46#if CHECK_DELETE
47    for(i=0; i<ilen; i++)
48    {
49        delete_rbtree(root, a[i]);
50        printf("== 删除节点: %d\n", a[i]);
51        if (root)
52        {
53            printf("== 树的详细信息: \n");
54            print_rbtree(root);
55            printf("\n");
56        }
57    }
58#endif
59    destroy_rbtree(root);
60}

红黑树的C测试程序

前面已经给出了红黑树的测试程序(rbtree_test.c),这里就不再重复说明。下面是测试程序的运行结果:

 1== 原始数据: 10 40 30 60 90 70 20 50 80 
 2== 前序遍历: 30 10 20 60 40 50 80 70 90 
 3== 中序遍历: 10 20 30 40 50 60 70 80 90 
 4== 后序遍历: 20 10 50 40 70 90 80 60 30 
 5== 最小值: 10
 6== 最大值: 90
 7== 树的详细信息: 
 830(B) is root
 910(B) is 30's   left child
1020(R) is 10's  right child
1160(R) is 30's  right child
1240(B) is 60's   left child
1350(R) is 40's  right child
1480(B) is 60's  right child
1570(R) is 80's   left child
1690(R) is 80's  right child
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值