使用C语言练练手避免手生。
文件主要有三个: bst.h, bst.c, test_bst.c
文件bst.h
typedef enum {FALSE=0, TRUE=1} bool;
/*
* Define a node structure.
*/
typedef struct node {
int val;
struct node* left;
struct node* right;
} node_t;
/*
* Creates a new node from a given value, allocating heap memory for it.
*/
node_t* make_tree(int val);
/*
* Inserts a new value into a given binary search tree, allocating heap memory
* for it.
*/
node_t* insert(int val, node_t* cur_root);
bool find_val(int val, node_t* root);
/*
* Given a pointer to the root, frees the memory associated with an entire list.
*/
void delete_bst(node_t* root);
/* Given a pointer to the root, prints all of the values in a tree. */
// pre order visiting
void print_bst(node_t* root);
文件 bst.c
#include <stdio.h>
#include <stdlib.h>
#include "bst.h"
/*
* Creates a new node from a given value, allocating heap memory for it.
*/
node_t* make_tree(int val) {
node_t* new_tree = malloc(sizeof(node_t));
new_tree->val = val;
new_tree->left = NULL;
new_tree->right = NULL;
return new_tree;
}
/*
* Inserts a new value into a given binary search tree, allocating heap memory
* for it.
*/
node_t* insert(int val, node_t* cur_root) {
/* always return root node */
node_t* left_node;
node_t* right_node;
node_t* new_node;
node_t* root;
root = cur_root;
while(cur_root != NULL){
left_node = cur_root->left;
right_node = cur_root->right;
if(val < cur_root->val){
if(left_node == NULL){
new_node = make_tree(val);
cur_root->left = new_node;
return root;
}
else{
cur_root = cur_root->left;
}
}
else if(val > cur_root->val){
if(right_node == NULL){
new_node = make_tree(val);
cur_root->right = new_node;
return root;
}
else{
cur_root = cur_root->right;
}
}
else{
// the val has been inserted
return NULL;
}
}
//empty tree
cur_root = make_tree(val);
return cur_root;
}
bool find_val(int val, node_t* root) {
/* recursive find */
if(root != NULL){
if(val == root->val){
return TRUE;
}
else if (val < root->val){
return find_val(val, root->left);
}
else {
return find_val(val, root->right);
}
}
return FALSE;
}
/*
* Given a pointer to the root, frees the memory associated with an entire tree.
*/
void delete_bst(node_t* root) {
/* recursive delete */
if(root != NULL){
delete_bst(root->left);
delete_bst(root->right);
free(root);
root = NULL;
}
return;
}
/* Given a pointer to the root, prints all of the values in a tree. */
void print_bst(node_t* root) {
if (root != NULL) {
printf("%d ", root->val);
print_bst(root->left);
print_bst(root->right);
}
return;
}
测试文件:
test_bst.c
#include <assert.h>
#include <stdio.h>
#include "bst.h"
int main() {
/* Insert 0. */
node_t* root = make_tree(1);
node_t* cur;
print_bst(root);
printf("\n");
assert(find_val(1, root) == TRUE);
/* Insert 1. */
cur = insert(0, root);
assert(find_val(0, root) == TRUE);
print_bst(root);
printf("\n");
/* Insert 2. */
cur = insert(2, root);
assert(find_val(2, root) == TRUE);
/* Print the tree. */
print_bst(root);
printf("\n");
/* Insert 4. */
cur = insert(4, root);
assert(find_val(4, root) == TRUE);
/* Insert 3 */
cur = insert(3, root);
assert(find_val(3, root) == TRUE);
print_bst(root);
printf("\n");
/* Delete the list. */
delete_bst(root);
return 0;
}
测试结果:
输入命令:
$ gcc test_bst.c bst.c -o test
$ ./test