[c]一个简单的二叉树

使用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



  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值