/* search_tree.h */

#ifndef _SEARCH_TREE_H
#define _SEARCH_TREE_H struct tree_node;
typedef struct tree_node *position;
typedef struct tree_node *search_tree; search_tree make_empty(search_tree t);
position find(int x, search_tree t);
position find_min(search_tree t);
position find_max(search_tree t);
search_tree insert_tree(int x, search_tree t);
search_tree delete_tree(int x, search_tree t);
int retrieve(position p);
void preorder_print_search_tree(search_tree t);
void inorder_print_search_tree(search_tree t);
void postorder_print_search_tree(search_tree t);
#endif
/* search_tree.c */

#include "search_tree.h"
#include <stdio.h>
#include <stdlib.h> struct tree_node
{
int element;
search_tree left;
search_tree right;
}; search_tree
make_empty(search_tree t)
{
if(t != NULL)
{
make_empty(t->left);
make_empty(t->right);
free(t);
}
return NULL;
} position find(int x, search_tree t)
{
if(t == NULL)
{
return NULL;
}
if(x < t->element)
return find(x, t->left);
else if(x > t->element)
return find(x, t->right);
else
return t;
} position
find_min(search_tree t)
{
if(t == NULL)
return NULL;
else if(t->left == NULL)
return t;
else
return find_min(t->left);
} position
find_max(search_tree t)
{
if(t == NULL)
return NULL;
else if(t->right == NULL)
return t;
else
return find_max(t->right);
} search_tree
insert_tree(int x, search_tree t)
{
if(t == NULL)
{
/* create and return a one-node tree */
t = malloc(sizeof(struct tree_node));
if(t == NULL)
{
printf("out of space!!!\n");
exit(1);
}
else
{
t->element = x;
t->left = NULL;
t->right = NULL;
}
}
else if(x < t->element)
t->left = insert_tree(x, t->left);
else if(x > t->element)
t->right = insert_tree(x, t->right);;
/* else x is in the tree already; we'll do nothing */ return t; /* do not forget this line */
} search_tree
delete_tree(int x, search_tree t)
{
position tmpcell; if(t == NULL)
{
printf("element not found!\n");
exit(1);
}
else if(x < t->element) /* go left */
t->left = delete_tree(x, t->left);
else if(x > t->element) /* go right */
t->right = delete_tree(x, t->right);
else if(t->left && t->right) /* two children */
{
/* replace with smallest in right subtree */
tmpcell = find_min(t->right);
t->element = tmpcell->element;
t->right = delete_tree(t->element, t->right);
}
else /* one or zero children */
{
tmpcell = t;
if(t->left == NULL) /* also handles 0 children */
t = t->right;
else if(t->right == NULL)
t = t->left;
free(tmpcell);
}
return t;
} int
retrieve(position p)
{
return p->element;
} void
preorder_print_search_tree(search_tree t)
{
    if(t != NULL)     {
        printf("%d ", t->element);
        preorder_print_search_tree(t->left);
        preorder_print_search_tree(t->right);
    }
}
void
inorder_print_search_tree(search_tree t)
{
    if(t != NULL)
    {
        inorder_print_search_tree(t->left);
        printf("%d ", t->element);
        inorder_print_search_tree(t->right);
    }
}
void
postorder_print_search_tree(search_tree t)
{
    if(t != NULL)
    {
        postorder_print_search_tree(t->left);
        postorder_print_search_tree(t->right);
        printf("%d ", t->element);
    }
}


/* search_tree_test.c */

#include "search_tree.h"
#include <stdio.h> int
main(void)
{
search_tree t = NULL;
position pmin, pmax;
int min, max; t = make_empty(t);
printf("insert 10 to search_tree\n");
t = insert_tree(10, t);
printf("insert 4 to search_tree\n");
t = insert_tree(4, t);
printf("insert 6 to search_tree\n");
t = insert_tree(6, t);
printf("insert 18 to search_tree\n");
t = insert_tree(18, t);
printf("insert 12 to search_tree\n");
t = insert_tree(12, t); pmin = find_min(t);
pmax = find_max(t); min = retrieve(pmin);
max = retrieve(pmax); printf("\npreorder traversal the search tree is :");
preorder_print_search_tree(t);
printf("\ninorder traversal the search tree is :");
inorder_print_search_tree(t);
printf("\npostorder traversal the search tree is :");
postorder_print_search_tree(t);
printf("\nmin = %d\nmax = %d\n", min, max); }

测试结果:

二叉查找树实现实例(C语言)的更多相关文章

  1. Redis:安装、配置、操作和简单代码实例(C语言Client端)

    Redis:安装.配置.操作和简单代码实例(C语言Client端) - hj19870806的专栏 - 博客频道 - CSDN.NET Redis:安装.配置.操作和简单代码实例(C语言Client端 ...

  2. 实例15_C语言绘制万年历

    实例说明:

  3. 简单的数据库连接池实例(java语言)

    1.概述 频繁的创建和销毁数据库连接消耗非常多的系统资源,创建一个池子, 管理一定数量的连接,用的时候去池中取,用完了放回池中,这时比较通用的做法. 2.关键字 LinkedList  synchro ...

  4. socket实例C语言:一个简单的聊天程序

    我们老师让写一个简单的聊天软件,并且实现不同机子之间的通信,我用的是SOCKET编程.不废话多说了,先附上代码: 服务器端server.c #include <stdio.h> #incl ...

  5. C 语言实例

    C 语言实例 C 语言实例 - 输出 "Hello, World!" C 语言实例 - 输出整数 C 语言实例 - 两个数字相加 C 语言实例 - 两个浮点数相乘 C 语言实例 - ...

  6. C 语言实例 - 判断字母

    C 语言实例 - 判断字母 C 语言实例 C 语言实例 用户输入一个字符,判断该字符是否为一个字母. 实例 #include <stdio.h> int main() { char c; ...

  7. C 语言实例 - 判断奇数/偶数

    C 语言实例 - 判断奇数/偶数 C 语言实例 C 语言实例 以下实例判断用户输入的整数是奇数还是偶数. 实例 #include <stdio.h> int main() { int nu ...

  8. C 语言实例 - 判断元音/辅音

    C 语言实例 - 判断元音/辅音 C 语言实例 C 语言实例 判断输入的字母是元音,还是辅音. 英语有26个字母,元音只包括 a.e.i.o.u 这五个字母,其余的都为辅音.y是半元音.半辅音字母,但 ...

  9. C 语言实例 - 将字符串写入文件

    C 语言实例 - 将字符串写入文件 C 语言实例 C 语言实例 将字符串写入文件. 实例 #include <stdio.h> #include <stdlib.h> /* e ...

随机推荐

  1. matlab运行过程中出现找不到指定模块问题解决

    对于matlab08版本以前的解决方法: 修改改环境变量: 新建变量名:BLAS_VERSION 变量值:D:\matlab7\bin\win32\atlas_Athlon.dll 在你的安装文件夹里 ...

  2. 常见 core dump 原因分析signal 11 - SIGSEGV

    signal 6 - SIGABRT free 多次 char *p = malloc(100); free(p); free(p); fclose 多次 // fclose 内部调用 free FI ...

  3. 《TCP/IP具体解释卷2:实现》笔记--IP:网际协议

    本章介绍IP分组的结构和主要的IP处理过程,包含输入,转发和输出. 下图显示了IP层常见的组织形式. 在之前的文章中.我们看到了网络接口怎样把到达的IP分组放到IP输入队列ipintrq中去,并怎样调 ...

  4. java获取路径(转)

    1.利用System.getProperty()函数获取当前路径:System.out.println(System.getProperty("user.dir"));//user ...

  5. 【BZOJ】【2946】【POI2000】公共串

    后缀数组 好感动,复习了下后缀数组居然写出来了……(感谢ykz大神) 求最长公共子串……WA了一发是因为:[不同字符串之间要用不同的特殊字符隔开]否则就会匹配到相同→_→比如都是aaa结尾,如果用相同 ...

  6. 1、cocos2d-x环境安装

     1 所需软件 2 安装python-2.7.8.amd64.msi 注意将当中的有一步设置,Add python.exe to path 设置python的环境变量 3 解压cocos2d-x- ...

  7. Linux获取当前时间

    代码(可以把clock_gettime换成time(NULL)) void getNowTime() { timespec time; clock_gettime(CLOCK_REALTIME, &a ...

  8. Steps to configure a FileShare Quorum Witness for Windows Failover Cluster

    Step 1: Step 2: Step 3: Step 4: You must use the wizard to create the file share. Step 5: to make su ...

  9. Anagrams leetcode java

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

  10. HDU 4585 Shaolin (STL)

    没想到map还有排序功能,默认按照键值从小到大排序 #include <cstdio> #include <iostream> #include <cstring> ...