您的位置:首页 > 编程语言 > C语言/C++

二叉查找树(c语言实现)

2010-03-30 09:38 323 查看
二叉查找树(Binary Search Tree),或者是一棵空树,或者是具有下列性质的二叉树:

若它的左子树不空,则左子树上所有结点的值均小于它的根结点的值;

若它的右子树不空,则右子树上所有结点的值均大于它的根结点的值;

它的左、右子树也分别为二叉排序树。

二叉排序树的查找过程和次优二叉树类似,通常采取二叉链表作为二叉排序树的存储结构。中序遍历
二叉排序树可得到一个关键字的有序序列,一个无序序列可以通过构造一棵二叉排序树变成一个有序序列,构造树的过程即为对无序序列进行排序的过程。每次插入

的新的结点都是二叉排序树上新的叶子结点,在进行插入操作时,不必移动其它结点,只需改动某个结点的指针,由空变为非空即可。搜索,插入,删除的复杂度等
于树高,O(log(n)).

/***********************************bstree.h*********************************/

#ifndef _BSTREE_H

#define _BSTREE_H

#ifdef _cplusplus

extern "C"{

#endif

typedef int Value;

struct _BSTreeNode;

typedef struct _BSTreeNode BSTreeNode;

BSTreeNode* binary_search_tree_create();

void binary_search_tree_destroy(BSTreeNode* thiz);

BSTreeNode* binary_search_tree_get_value_by_value(BSTreeNode* thiz,
Value value);

BSTreeNode* binary_search_tree_get_min(BSTreeNode* thiz);

BSTreeNode* binary_search_tree_get_max(BSTreeNode* thiz);

BSTreeNode* binary_search_tree_insert(BSTreeNode* thiz, Value value);

BSTreeNode* binary_search_tree_delete(BSTreeNode* thiz, Value value);

void binary_search_tree_print(BSTreeNode* thiz);

#ifdef _cplusplus

}

#endif

#endif/*_TREE_H*/

/**********************bstree.c*************************/

#include <stdlib.h>

#include <stdio.h>

#include "bstree.h"

struct _BSTreeNode

{

Value value;

BSTreeNode* left;

BSTreeNode* right;

};

/*******************************创建一棵二叉
树**********************************/

BSTreeNode* binary_search_tree_create(Value value)

{

BSTreeNode* thiz = (BSTreeNode*)malloc(sizeof(BSTreeNode));



thiz->value = value;

thiz->left = NULL;

thiz->right = NULL;

return thiz;

}

/******************************销毁这棵二叉树*********************************/

void binary_search_tree_destroy(BSTreeNode* thiz)

{

if(thiz != NULL)

{

if(thiz->left != NULL)

{

binary_search_tree_destroy(thiz->left);

}

if(thiz->right != NULL)

{

binary_search_tree_destroy(thiz->right);

}

}

free(thiz);

thiz = NULL;

return;

}

/**********************在二叉树中查找值为value的节点*************************/

BSTreeNode* binary_search_tree_get_value_by_value(BSTreeNode* thiz,
Value value)

{

BSTreeNode* iter = thiz;

if(iter == NULL)

{

printf("value not found!/n");

return NULL;

}

else if( value < iter->value )

{

return
binary_search_tree_get_value_by_value(iter->left, value);

}

else if( value > iter->value)

{

return
binary_search_tree_get_value_by_value(iter->right, value);

}

else

{

return iter;

}

}

/*****************得到二叉树中最小值的节点(递归算法)*****************/

BSTreeNode* binary_search_tree_get_min(BSTreeNode* thiz)

{

if(thiz == NULL)

{

return NULL;

}

else if(thiz->left == NULL)

{

return thiz;

}

return binary_search_tree_get_min(thiz->left);

}

/****************得到二叉树中最大值的节点(非递归算法)******************/

BSTreeNode* binary_search_tree_get_max(BSTreeNode* thiz)

{

if(thiz != NULL)

while(thiz->right != NULL)

{

thiz = thiz->right;

}



return thiz;

}

/********************在二叉树中插入一个值为value的节点**********************/

BSTreeNode* binary_search_tree_insert(BSTreeNode* thiz, Value value)

{

if(thiz == NULL)

{

thiz = binary_search_tree_create(value);

}

else if(value < thiz->value)

thiz->left = binary_search_tree_insert(thiz->left,
value);

else if(value > thiz->value)

thiz->right =
binary_search_tree_insert(thiz->right, value);

return thiz;

}

/********************在二叉树中删除一个值为value的节点***********************/

BSTreeNode* binary_search_tree_delete(BSTreeNode* thiz, Value
value)

{

BSTreeNode* iter = thiz;

BSTreeNode* tmp = NULL;

if(iter == NULL)

printf("Element not found!/n");

else if(value < thiz->value)

thiz->left = binary_search_tree_delete(thiz->left,
value);

else if(value > thiz->value)

thiz->right =
binary_search_tree_delete(thiz->right, value);

else if(thiz->right != NULL && thiz->left != NULL)

{

iter = binary_search_tree_get_min(thiz->right);

thiz->value = iter->value;

thiz->right =
binary_search_tree_delete(thiz->right, thiz->value);

}

else

{

iter = thiz;

if(thiz->left == NULL)

thiz = thiz->right;

else if(thiz->right == NULL)

thiz = thiz->left;

free(iter);

}

return thiz;

}

/********************二叉树的打印*******************/

void binary_search_tree_print(BSTreeNode* thiz)

{

if(thiz != NULL)

{

printf("%d/t", thiz->value);

if(thiz->left != NULL)

binary_search_tree_print(thiz->left);

if(thiz->right != NULL)

binary_search_tree_print(thiz->right);

}

return;

}

/****************测试函数*****************/

#ifdef BSTREE_TEST

#include "bstree.h"

int main(int argc, char* argv[])

{

BSTreeNode* thiz = NULL;

BSTreeNode* iter = NULL;

int value = 35;

int i = 0;

thiz = binary_search_tree_create(value);

thiz = binary_search_tree_insert(thiz, 30);

thiz = binary_search_tree_insert(thiz, 29);

thiz = binary_search_tree_insert(thiz, 31);

thiz = binary_search_tree_insert(thiz, 27);

thiz = binary_search_tree_insert(thiz, 28);

thiz = binary_search_tree_insert(thiz, 26);

thiz = binary_search_tree_insert(thiz, 37);

thiz = binary_search_tree_insert(thiz, 36);

thiz = binary_search_tree_insert(thiz, 49);

thiz = binary_search_tree_insert(thiz, 45);

thiz = binary_search_tree_insert(thiz, 0);

binary_search_tree_print(thiz);

printf("/n");



iter = binary_search_tree_get_value_by_value(thiz, value);

printf("47's search %d success!/n", iter->value);

iter = binary_search_tree_get_min(thiz);

printf("min value search %d success!/n", iter->value);

iter = binary_search_tree_get_max(thiz);

printf("max value search %d success!/n", iter->value);



thiz = binary_search_tree_delete(thiz, 37);

binary_search_tree_print(thiz);

printf("/n");

thiz = binary_search_tree_delete(thiz, 26);

binary_search_tree_print(thiz);

printf("/n");

binary_search_tree_destroy(thiz);

return 0;

}

#endif/*BSTREE_TEST*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: